> For the complete documentation index, see [llms.txt](https://docs.payments.thalescloud.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.payments.thalescloud.io/nfc-wallet-sdk-android/ja/implement-nfc-wallet/make-payment/implement-contactless-payments/1.-implement-hce-service.md).

# 1. HCEサービスを実装する

## 概要

NFC Wallet SDKはAndroid Host Card Emulation（HCE）をサポートしています。これにより、NFCカードのエミュレーションと、NFCを介したPOS端末とのAPDU交換が可能になります。

詳細については、Androidのドキュメントを参照してください： [ホストベースのカードエミュレーションの概要](https://developer.android.com/develop/connectivity/nfc/hce).

続行するには、まずこのセクションで説明されているとおりにHCEサービスを実装します。

サービスを登録したら、次の項目の下に表示されることを確認します **タップ＆ペイ** Androidの設定で。

## SDKの統合

### 継承する `AsyncHCEService`

非接触決済を処理するには、次を継承してAPDU処理を有効にします `AsyncHCEService`.

どのメソッドもオーバーライドする必要はありません。

APDU処理をログ出力またはカスタマイズしたい場合にのみ、オーバーライドを使用してください。

```java
public class MyHCEService extends AsyncHCEService {

    // 'processCommandApdu' のオーバーライドは任意です
    // デジタルウォレットアプリケーションはAPDU処理時間を取得できます。
    @Override
    public byte[] processCommandApdu(byte[] inputApdu, Bundle bundle) {
        // SDKの値（常に 'null'）を返します。
        // APDU処理は非同期です。
        // SDKは応答APDUを自動的にPOS端末へ送信します。
        return super.processCommandApdu(inputApdu, bundle);
    }

    // 'onApduResponse' のオーバーライドは任意です
    // デジタルウォレットアプリケーションは応答APDUを確認またはオーバーライドできます。
    @Override
    public boolean onApduResponse(final byte[] inputApdu, final Bundle extras, final byte[] responseApdu){
        // デジタルウォレットアプリケーションは responseApdu をログ出力できます。

        // 応答を上書きしてPOS端末に返信したい場合：
        // 1. responseAPDUを変更する
        // 2. sendResponseApdu(modifiedResponseApdu);
        // 3. true を返します。これにより、応答がすでに送信済みであることをSDKに伝えます。

        // それ以外の場合は false を返し、SDKに responseApdu を送信させます。
        return false;
    }
}
```

{% hint style="info" %}
認証が必要な場合、NFC Wallet SDKはAPDU処理を一時停止します。

APDU処理は、認証が成功した、中止された、またはHCEサービスが無効化された後に再開されます。
{% endhint %}

{% hint style="danger" %}
一部のデバイスでは、次のバックグラウンド起動がブロックされます `Activity` ステップアップ認証プロンプト用の。これにより、次を呼び出すまで非接触決済がブロックされることがあります `deactivate()`.

デジタルウォレットアプリケーションでタイムアウトを実装し、次を呼び出して `deactivate()` 非接触決済のフローのブロックを解除できます。
{% endhint %}

### サービスを次に登録します `AndroidManifest.xml`

HCEサービス（次を継承するクラス）を登録します `AsyncHCEService`を次のように `HOST_APDU_SERVICE` マニフェストに記述します。

```xml
<!-- サービス名はパッケージ構成と一致している必要があります。 -->
<service android:name="com.mycompany.myapplication.myservices.MyHCEService"
  android:exported="true"
  android:label="@string/app_name"
  android:permission="android.permission.BIND_NFC_SERVICE">
  <intent-filter>
    <action android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE" />
  </intent-filter>
  <meta-data
    android:name="android.nfc.cardemulation.host_apdu_service"
    android:resource="@xml/apduservice"/>
</service>
```

{% hint style="info" %}
作成する `apduservice.xml` 次に `res/xml` サポートされるAIDを宣言します。
{% endhint %}

### サポートされるAIDを宣言する

作成する `apduservice.xml` 次に `res/xml`.

サポートするPPSE AIDと決済ネットワークのAIDを宣言します。

```xml
 <?xml version="1.0" encoding="utf-8"?>
<host-apdu-service xmlns:android="http://schemas.android.com/apk/res/android"
  android:description="@string/hce_service_description"
  android:requireDeviceUnlock="false"
  android:apduServiceBanner="@drawable/hce_banner">
  <aid-group
    android:description="@string/aid_description"
    android:category="payment">
    <!-- 必須のPPSE AID -->
    <aid-filter android:name="325041592E5359532E4444463031"/>
    <!-- MastercardのAID -->
    <aid-filter android:name="A0000000041010"/>
    <aid-filter android:name="A0000000043060"/>
    <aid-filter android:name="A0000000042010"/>
    <!-- VisaのAID -->
    <aid-filter android:name="A0000000031010"/>
    <aid-filter android:name="A0000000980840"/>
    <aid-filter android:name="A0000000032020"/>
    <aid-filter android:name="A0000000032010"/>
    
  </aid-group>
</host-apdu-service>
```

上の例では：

* 必須のPPSE AIDは次のとおりです：
  * `325041592E5359532E4444463031`
* MastercardのAIDは次のとおりです：
  * `A0000000041010`
  * `A0000000043060`
  * `A0000000042010`
* VisaのAIDは次のとおりです：
  * `A0000000031010`
  * `A0000000980840`
  * `A0000000032020`
  * `A0000000032010`

{% hint style="info" %}
AIDの一覧はNFC Walletプログラムによって異なります。決済ネットワークの担当者に一覧を確認してください。
{% endhint %}

### タップ＆ペイの設定を確認する

HCEサービスを次に登録したら `AndroidManifest.xml`、次の項目の下に表示されます **タップ＆ペイ** Androidの設定で。

確認するには：

1. 開く **設定** をデバイスで。
2. 次へ移動する **タップ＆ペイ**.
3. アプリケーションが一覧に表示されていることを確認します。

<figure><img src="/files/08ec6aebcfaf723b8bb1f9b463c17c203fa6ffd8" alt="" width="375"><figcaption><p>Androidの設定で、アプリケーションがTap and Payの下に一覧表示されている画面。</p></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.payments.thalescloud.io/nfc-wallet-sdk-android/ja/implement-nfc-wallet/make-payment/implement-contactless-payments/1.-implement-hce-service.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
