> 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 カードのエミュレーションと、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 ウォレットプログラムによって異なります。一覧は決済ネットワークの担当者に確認してください。
{% endhint %}

### タップして支払う の設定を確認する

HCE サービスを に登録した後、 `AndroidManifest.xml`次に表示されます **タップして支払う** Android の設定にて。

確認するには：

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

<figure><img src="/files/08ec6aebcfaf723b8bb1f9b463c17c203fa6ffd8" alt="" width="375"><figcaption><p>Android の設定で、アプリケーションがタップして支払うの下に一覧表示されている様子。</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.
