Wi-Fi Direct(P2P)を使用してサービスを検出する

このクラスの最初のレッスン、ネットワーク サービス ディスカバリの使用では、ローカル ネットワークに接続されたサービスを検出する方法を説明しました。ただし、Wi-Fi Direct(P2P)サービス ディスカバリを使用すると、ネットワークに接続することなく、付近のデバイスのサービスを直接検出できます。また、デバイスで実行されているサービスをアドバタイズすることもできます。これらの機能は、ローカル ネットワークやアクセス ポイントが利用できない場合でもアプリ間の通信に役立ちます。

この API セットは、前のレッスンで説明した Network Service Discovery API と目的が類似していますが、コード内での実装は大きく異なります。このレッスンでは、Wi-Fi Direct を使用して他のデバイスで利用可能なサービスを検出する方法について説明します。このレッスンは、読者が Wi-Fi Direct API に精通していることを前提としています。

マニフェストをセットアップする

Wi-Fi P2P を使用するには、CHANGE_WIFI_STATEACCESS_WIFI_STATEACCESS_FINE_LOCATIONINTERNET 権限をマニフェストに追加します。アプリが Android 13(API レベル 33)以降をターゲットとしている場合は、NEARBY_WIFI_DEVICES 権限もマニフェストに追加します。Wi-Fi Direct はインターネット接続を必要としませんが、標準の Java ソケットを使用します。Android でこれらを使用するには、リクエストされた権限が必要です。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.nsdchat"
    ...

    <uses-permission
        android:required="true"
        android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission
        android:required="true"
        android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission
        android:required="true"
        android:name="android.permission.INTERNET"/>
    <!-- If your app targets Android 13 (API level 33)
         or higher, you must declare the NEARBY_WIFI_DEVICES permission. -->
    <uses-permission
        android:name="android.permission.NEARBY_WIFI_DEVICES"
        <!-- If your app derives location information from Wi-Fi APIs,
             don't include the "usesPermissionFlags" attribute. -->
        android:usesPermissionFlags="neverForLocation" />
    <uses-permission
        android:required="true"
        android:name="android.permission.ACCESS_FINE_LOCATION"
        <!-- If any feature in your app relies on precise location information,
             don't include the "maxSdkVersion" attribute. -->
        android:maxSdkVersion="32" />
    ...

上記の権限に加えて、次の API を使用するには、位置情報モードを有効にする必要があります。

ローカル サービスを追加する

ローカル サービスを提供する場合は、サービス ディスカバリに登録する必要があります。ローカル サービスが登録されると、フレームワークはピアからのサービス ディスカバリ リクエストに自動的に応答します。

ローカル サービスを作成するには:

  1. WifiP2pServiceInfo オブジェクトを作成します。
  2. このオブジェクトにサービスに関する情報を指定します。
  3. addLocalService() を呼び出して、サービス ディスカバリ用のローカル サービスを登録します。

Kotlin

    private fun startRegistration() {
        //  Create a string map containing information about your service.
        val record: Map<String, String> = mapOf(
                "listenport" to SERVER_PORT.toString(),
                "buddyname" to "John Doe${(Math.random() * 1000).toInt()}",
                "available" to "visible"
        )

        // Service information.  Pass it an instance name, service type
        // _protocol._transportlayer , and the map containing
        // information other devices will want once they connect to this one.
        val serviceInfo =
                WifiP2pDnsSdServiceInfo.newInstance("_test", "_presence._tcp", record)

        // Add the local service, sending the service info, network channel,
        // and listener that will be used to indicate success or failure of
        // the request.
        manager.addLocalService(channel, serviceInfo, object : WifiP2pManager.ActionListener {
            override fun onSuccess() {
                // Command successful! Code isn't necessarily needed here,
                // Unless you want to update the UI or add logging statements.
            }

            override fun onFailure(arg0: Int) {
                // Command failed.  Check for P2P_UNSUPPORTED, ERROR, or BUSY
            }
        })
    }

Java

    private void startRegistration() {
        //  Create a string map containing information about your service.
        Map record = new HashMap();
        record.put("listenport", String.valueOf(SERVER_PORT));
        record.put("buddyname", "John Doe" + (int) (Math.random() * 1000));
        record.put("available", "visible");

        // Service information.  Pass it an instance name, service type
        // _protocol._transportlayer , and the map containing
        // information other devices will want once they connect to this one.
        WifiP2pDnsSdServiceInfo serviceInfo =
                WifiP2pDnsSdServiceInfo.newInstance("_test", "_presence._tcp", record);

        // Add the local service, sending the service info, network channel,
        // and listener that will be used to indicate success or failure of
        // the request.
        manager.addLocalService(channel, serviceInfo, new ActionListener() {
            @Override
            public void onSuccess() {
                // Command successful! Code isn't necessarily needed here,
                // Unless you want to update the UI or add logging statements.
            }

            @Override
            public void onFailure(int arg0) {
                // Command failed.  Check for P2P_UNSUPPORTED, ERROR, or BUSY
            }
        });
    }

周辺のサービスを検出する

Android では、コールバック メソッドを使用して利用可能なサービスをアプリに通知します。そのため、最初に行うのがコールバック メソッドです。受信レコードをリッスンする WifiP2pManager.DnsSdTxtRecordListener を作成します。このレコードは、必要に応じて他のデバイスからブロードキャストできます。メソッドを受け取ったら、デバイスのアドレスとその他の関連情報を現在のメソッドの外部のデータ構造にコピーして、後でアクセスできるようにします。次の例では、レコードにユーザーの ID が入力された「buddyname」フィールドが含まれていることを前提としています。

Kotlin

private val buddies = mutableMapOf<String, String>()
...
private fun discoverService() {
    /* Callback includes:
     * fullDomain: full domain name: e.g. "printer._ipp._tcp.local."
     * record: TXT record dta as a map of key/value pairs.
     * device: The device running the advertised service.
     */
    val txtListener = DnsSdTxtRecordListener { fullDomain, record, device ->
        Log.d(TAG, "DnsSdTxtRecord available -$record")
        record["buddyname"]?.also {
            buddies[device.deviceAddress] = it
        }
    }
}

Java

final HashMap<String, String> buddies = new HashMap<String, String>();
...
private void discoverService() {
    DnsSdTxtRecordListener txtListener = new DnsSdTxtRecordListener() {
        @Override
        /* Callback includes:
         * fullDomain: full domain name: e.g. "printer._ipp._tcp.local."
         * record: TXT record dta as a map of key/value pairs.
         * device: The device running the advertised service.
         */

        public void onDnsSdTxtRecordAvailable(
                String fullDomain, Map record, WifiP2pDevice device) {
                Log.d(TAG, "DnsSdTxtRecord available -" + record.toString());
                buddies.put(device.deviceAddress, record.get("buddyname"));
            }
        };
}

サービス情報を取得するには、WifiP2pManager.DnsSdServiceResponseListener を作成します。実際の説明と接続情報を受け取ります。上記のコード スニペットでは、Map オブジェクトを実装して、デバイスのアドレスとメンバー名とをペアに設定しました。サービス レスポンス リスナーはこれを使用して、DNS レコードを対応するサービス情報にリンクします。両方のリスナーを実装したら、setDnsSdResponseListeners() メソッドを使用して WifiP2pManager に追加します。

Kotlin

private fun discoverService() {
    ...

    val servListener = DnsSdServiceResponseListener { instanceName, registrationType, resourceType ->
        // Update the device name with the human-friendly version from
        // the DnsTxtRecord, assuming one arrived.
        resourceType.deviceName = buddies[resourceType.deviceAddress] ?: resourceType.deviceName

        // Add to the custom adapter defined specifically for showing
        // wifi devices.
        val fragment = fragmentManager
                .findFragmentById(R.id.frag_peerlist) as WiFiDirectServicesList
        (fragment.listAdapter as WiFiDevicesAdapter).apply {
            add(resourceType)
            notifyDataSetChanged()
        }

        Log.d(TAG, "onBonjourServiceAvailable $instanceName")
    }

    manager.setDnsSdResponseListeners(channel, servListener, txtListener)
    ...
}

Java

private void discoverService() {
...

    DnsSdServiceResponseListener servListener = new DnsSdServiceResponseListener() {
        @Override
        public void onDnsSdServiceAvailable(String instanceName, String registrationType,
                WifiP2pDevice resourceType) {

                // Update the device name with the human-friendly version from
                // the DnsTxtRecord, assuming one arrived.
                resourceType.deviceName = buddies
                        .containsKey(resourceType.deviceAddress) ? buddies
                        .get(resourceType.deviceAddress) : resourceType.deviceName;

                // Add to the custom adapter defined specifically for showing
                // wifi devices.
                WiFiDirectServicesList fragment = (WiFiDirectServicesList) getFragmentManager()
                        .findFragmentById(R.id.frag_peerlist);
                WiFiDevicesAdapter adapter = ((WiFiDevicesAdapter) fragment
                        .getListAdapter());

                adapter.add(resourceType);
                adapter.notifyDataSetChanged();
                Log.d(TAG, "onBonjourServiceAvailable " + instanceName);
        }
    };

    manager.setDnsSdResponseListeners(channel, servListener, txtListener);
    ...
}

次に、サービス リクエストを作成して addServiceRequest() を呼び出します。このメソッドは、成功または失敗を報告するリスナーも受け取ります。

Kotlin

        serviceRequest = WifiP2pDnsSdServiceRequest.newInstance()
        manager.addServiceRequest(
                channel,
                serviceRequest,
                object : WifiP2pManager.ActionListener {
                    override fun onSuccess() {
                        // Success!
                    }

                    override fun onFailure(code: Int) {
                        // Command failed.  Check for P2P_UNSUPPORTED, ERROR, or BUSY
                    }
                }
        )

Java

        serviceRequest = WifiP2pDnsSdServiceRequest.newInstance();
        manager.addServiceRequest(channel,
                serviceRequest,
                new ActionListener() {
                    @Override
                    public void onSuccess() {
                        // Success!
                    }

                    @Override
                    public void onFailure(int code) {
                        // Command failed.  Check for P2P_UNSUPPORTED, ERROR, or BUSY
                    }
                });

最後に、discoverServices() を呼び出します。

Kotlin

        manager.discoverServices(
                channel,
                object : WifiP2pManager.ActionListener {
                    override fun onSuccess() {
                        // Success!
                    }

                    override fun onFailure(code: Int) {
                        // Command failed. Check for P2P_UNSUPPORTED, ERROR, or BUSY
                        when (code) {
                            WifiP2pManager.P2P_UNSUPPORTED -> {
                                Log.d(TAG, "Wi-Fi Direct isn't supported on this device.")
                            }
                        }
                    }
                }
        )

Java

        manager.discoverServices(channel, new ActionListener() {

            @Override
            public void onSuccess() {
                // Success!
            }

            @Override
            public void onFailure(int code) {
                // Command failed.  Check for P2P_UNSUPPORTED, ERROR, or BUSY
                if (code == WifiP2pManager.P2P_UNSUPPORTED) {
                    Log.d(TAG, "Wi-Fi Direct isn't supported on this device.");
                else if(...)
                    ...
            }
        });

うまくいったでしょうか。問題が発生した場合は、作成した非同期呼び出しが WifiP2pManager.ActionListener を引数として受け取り、成功または失敗を示すコールバックを提供することに注意してください。問題を診断するには、onFailure() にデバッグコードを入れます。メソッドが提供するエラーコードからヒントを確認できます。考えられるエラー値とその意味は次のとおりです

P2P_UNSUPPORTED
アプリを実行しているデバイスで Wi-Fi Direct がサポートされない。
BUSY
システムがビジー状態のため、リクエストを処理できません。
ERROR
内部エラーが発生したため、オペレーションが失敗しました。