本課程的第一堂課是使用網路服務 探索:示範 如何探索連線至區域網路的服務。不過,使用 Wi-Fi Direct (P2P) Service Discovery 可讓您探索鄰近裝置的服務 而不必連線至網路您也可以宣傳 也就是您的裝置上所執行的應用程式。這些功能可協助您在不同應用程式之間溝通 即使在沒有區域網路或無線基地台無法使用時也不受影響
雖然這組 API 的用途與網路服務探索相近 我們在上一堂課介紹的 API,但在程式碼中實作這些 API 非常不同。 本課程將說明如何尋找其他裝置提供的服務 存取 Wi-Fi Direct。本課程假設您已熟悉 Wi-Fi Direct API。
設定資訊清單
如要使用 Wi-Fi P2P,請新增 CHANGE_WIFI_STATE
、ACCESS_WIFI_STATE
和
ACCESS_FINE_LOCATION
,
和INTERNET
授予資訊清單的權限如果應用程式指定 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 也需啟用定位模式:
新增本機服務
如果您在提供在地服務時,必須註冊該服務 服務探索。本機服務註冊完成後 會自動回應對等點的服務探索要求。
如要建立本機服務,請按照下列指示操作:
- 撰寫
WifiP2pServiceInfo
物件。 - 填入服務相關資訊。
- 呼叫
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
來監聽
。您可選擇讓其他人員播送這項紀錄
裝置。找到裝置後,請複製裝置地址和任何其他地址
所需資料結構以外的相關資訊
方法,方便日後存取。以下範例假設
紀錄中含有「夥伴名稱」] 欄位填入使用者的身分資料。
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
- 發生內部錯誤,導致作業失敗。