使用僅限本機的 Wi-Fi 無線基地台

你可以使用僅限本機的無線基地台,讓連線至 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()