您可以使用僅限本機的無線基地台,讓裝置所連上 Wi-Fi 無線基地台的應用程式能夠彼此通訊。此方法建立的網路無法存取網際網路。每個應用程式都可以針對無線基地台提出單一要求,但多個應用程式可以同時要求無線基地台。如果同時成功註冊多個應用程式,就會共用基礎熱點。無線基地台可供使用時,系統會呼叫 LocalOnlyHotspotCallback.onStarted(LocalOnlyHotspotReservation)
。
如果應用程式指定 Android 13 (API 級別 33) 以上版本,就必須要求 NEARBY_WIFI_DEVICES
使用僅限本機的無線基地台,如以下程式碼片段所示。如果應用程式指定舊版 Android,必須改為要求 ACCESS_FINE_LOCATION
。
<manifest ...> <<!-- 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: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" /> <application ...> ... </application> </manifest>
如要進一步瞭解如何使用僅限本機的無線基地台,請參閱 startLocalOnlyHotspot()
。