使用僅限本機的 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()
。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-27 (世界標準時間)。"],[],[],null,["You can use a local-only hotspot to enable applications on devices connected to\nthe Wi-Fi hotspot to communicate with each other. The network created by this\nmethod will not have Internet access. Each application can make a single request\nfor the hotspot, but multiple applications can request the hotspot at the same\ntime. When multiple applications have successfully registered concurrently, they\nshare the underlying hotspot.\n[`LocalOnlyHotspotCallback.onStarted(LocalOnlyHotspotReservation)`](/reference/android/net/wifi/WifiManager.LocalOnlyHotspotCallback#onStarted(android.net.wifi.WifiManager.LocalOnlyHotspotReservation))\nis called when the hotspot is ready for use.\n\nIf your app targets Android 13 (API level 33) or higher, you must request the\n[`NEARBY_WIFI_DEVICES`](/reference/android/Manifest.permission#NEARBY_WIFI_DEVICES)\nto use a local-only hotspot, as shown in the following code snippet. Apps that\ntarget an earlier version of Android must request `ACCESS_FINE_LOCATION`\ninstead. \n\n```xml\n\u003cmanifest ...\u003e\n \u003c\u003c!-- If your app targets Android 13 (API level 33)\n or higher, you must declare the NEARBY_WIFI_DEVICES permission. --\u003e\n \u003cuses-permission android:name=\"android.permission.NEARBY_WIFI_DEVICES\"\n \u003c!-- If your app derives location information from\n Wi-Fi APIs, don't include the \"usesPermissionFlags\"\n attribute. --\u003e\n android:usesPermissionFlags=\"neverForLocation\" /\u003e\n \u003cuses-permission android:name=\"android.permission.ACCESS_FINE_LOCATION\"\n \u003c!-- If any feature in your app relies on\n precise location information, don't include the\n \"maxSdkVersion\" attribute. --\u003e\n android:maxSdkVersion=\"32\" /\u003e\n \u003capplication ...\u003e\n ...\n \u003c/application\u003e\n\u003c/manifest\u003e\n```\n\nFor more details on using local-only hotspots, see\n[`startLocalOnlyHotspot()`](/reference/android/net/wifi/WifiManager#startLocalOnlyHotspot(android.net.wifi.WifiManager.LocalOnlyHotspotCallback,%20android.os.Handler))."]]