您可以使用仅限本地的热点,让连接到 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()
。