Yêu cầu cấp quyền truy cập vào các thiết bị Wi-Fi ở gần
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Những ứng dụng nhắm đến Android 13 (API cấp 33) trở lên và quản lý các kết nối Wi-Fi nên yêu cầu NEARBY_WIFI_DEVICESquyền khi bắt đầu chạy. Quyền này giúp bạn dễ dàng biện minh cho việc ứng dụng truy cập vào các thiết bị Wi-Fi ở gần; trên các phiên bản Android trước đây, những ứng dụng này cần khai báo quyền ACCESS_FINE_LOCATION.
Quyền này thuộc nhóm quyền đối với các thiết bị ở gần
Quyền NEARBY_WIFI_DEVICES thuộc nhóm quyền Thiết bị ở gần. Nhóm này được thêm vào Android 12 (API cấp 31), đồng thời bao gồm các quyền liên quan đến Bluetooth và Băng tần siêu rộng. Khi bạn yêu cầu bất kỳ tổ hợp quyền nào trong nhóm quyền này, hệ thống sẽ hiện một hộp thoại thời gian chạy duy nhất và yêu cầu người dùng phê duyệt quyền truy cập của ứng dụng vào các thiết bị ở gần.
Trong phần cài đặt hệ thống, người dùng phải bật và tắt quyền truy cập Thiết bị ở gần theo nhóm; ví dụ: người dùng không thể tắt quyền truy cập Wi-Fi nhưng vẫn bật quyền truy cập Bluetooth cho một ứng dụng nhất định.
Khẳng định chắc chắn rằng ứng dụng của bạn không lấy thông tin vị trí thực tế
Khi nhắm đến Android 13 trở lên, hãy cân nhắc xem ứng dụng của bạn có bao giờ lấy thông tin vị trí từ các API Wi-Fi hay không; nếu không, bạn nên khẳng định chắc chắn rằng ứng dụng không lấy thông tin vị trí từ các API Wi-Fi. Để đưa ra khẳng định này, hãy đặt thuộc tính usesPermissionFlags thành neverForLocation trong tệp kê khai của ứng dụng, như minh hoạ trong đoạn mã sau. Quy trình này tương tự như quy trình bạn thực hiện khi khẳng định rằng thông tin về thiết bị Bluetooth không bao giờ được dùng để xác định vị trí:
Các phiên bản trước và một số API yêu cầu quyền truy cập thông tin vị trí
Một số API Wi-Fi yêu cầu quyền ACCESS_FINE_LOCATION, ngay cả khi ứng dụng của bạn nhắm đến Android 13 trở lên. Ví dụ: các phương thức sau đây trong lớp WifiManager:
Ngoài ra, vì quyền NEARBY_WIFI_DEVICES chỉ có trên Android 13 trở lên, nên bạn phải giữ mọi khai báo cho ACCESS_FINE_LOCATION để cung cấp khả năng tương thích ngược trong ứng dụng. Tuy nhiên, miễn là ứng dụng của bạn không phụ thuộc vào thông tin vị trí chính xác, bạn có thể đặt phiên bản SDK tối đa của quyền này thành 32, như minh hoạ trong đoạn mã sau:
Hình 1 cho thấy quy trình truy cập Wi-Fi trên các thiết bị chạy Android 13 trở lên, đối với những ứng dụng nhắm đến Android 13 trở lên. Xin lưu ý rằng, miễn là bạn khẳng định ứng dụng của mình không lấy thông tin vị trí thực tế từ thông tin thiết bị Wi-Fi, thì bạn không cần khai báo quyền ACCESS_FINE_LOCATION nữa:
Hình 1. Lưu đồ để xác định xem một ứng dụng nhắm đến Android 13 (API cấp 33) trở lên có thể truy cập vào thông tin Wi-Fi hay không.
Hình 2 minh hoạ quy trình truy cập Wi-Fi trên các thiết bị chạy 12L trở xuống. Lưu ý sự phụ thuộc vào quyền ACCESS_FINE_LOCATION.
Hình 2. Lưu đồ để xác định xem một ứng dụng nhắm đến 12L (API cấp 32) trở xuống có thể truy cập vào thông tin Wi-Fi hay không.
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-08-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-08-27 UTC."],[],[],null,["Apps that target Android 13 (API level 33) or higher and manage Wi-Fi connections\nshould request the\n[`NEARBY_WIFI_DEVICES`](/reference/android/Manifest.permission#NEARBY_WIFI_DEVICES)\n[runtime permission](/guide/topics/permissions/overview#runtime). This\npermission makes it easier to justify an app's access of nearby Wi-Fi devices;\non previous versions of Android, these apps needed to declare the\n[`ACCESS_FINE_LOCATION`](/reference/android/Manifest.permission#ACCESS_FINE_LOCATION)\npermission instead.\n| **Caution:** If your app tries to call a Wi-Fi API without the proper permission, a [`SecurityException`](/reference/java/lang/SecurityException) occurs.\n\nPermission is part of the nearby devices group\n\nThe `NEARBY_WIFI_DEVICES` permission is part of the **Nearby devices**\npermission group. This group, added in Android 12 (API level 31), also includes\npermissions related to Bluetooth and Ultra-wideband. When you request any\ncombination of permissions from this permission group, the system shows a single\nruntime dialog and asks the user to approve your app's access to nearby devices.\nIn system settings, the user must enable and disable the **Nearby devices**\npermissions as a group; for example, users can't disable Wi-Fi access but keep\nBluetooth access enabled for a given app.\n\nStrongly assert that your app doesn't derive physical location\n\nWhen you target Android 13 or higher, consider whether your app\never derives location information from Wi-Fi APIs; if not, you should strongly\nassert that. To make this assertion, set the `usesPermissionFlags` attribute to\n`neverForLocation` in your app's manifest file, as shown in the following code\nsnippet. This process is similar to the one you do when you\n[assert that Bluetooth device information is never used for location](/guide/topics/connectivity/bluetooth/permissions#assert-never-for-location): \n\n```xml\n\u003cmanifest ...\u003e\n \u003cuses-permission android:name=\"android.permission.NEARBY_WIFI_DEVICES\"\n android:usesPermissionFlags=\"neverForLocation\" /\u003e\n \u003capplication ...\u003e\n ...\n \u003c/application\u003e\n\u003c/manifest\u003e\n```\n\nPrevious versions and some APIs require location permission\n\nSeveral Wi-Fi APIs require the `ACCESS_FINE_LOCATION` permission, even when your\napp targets Android 13 or higher. Examples include the following\nmethods from the `WifiManager` class:\n\n- [`getScanResults()`](/reference/android/net/wifi/WifiManager#getScanResults())\n- [`startScan()`](/reference/android/net/wifi/WifiManager#startScan())\n\nAlso, because the `NEARBY_WIFI_DEVICES` permission is available only on\nAndroid 13 and higher, you should keep any declarations for\n[`ACCESS_FINE_LOCATION`](/reference/android/Manifest.permission#ACCESS_FINE_LOCATION)\nto provide backward compatibility in your app. However, as long as your app\ndoesn't otherwise rely on\n[precise location information](/training/location/permissions#accuracy), you can\nset the maximum SDK version of this permission to `32`, as shown in the\nfollowing code snippet: \n\n```xml\n\u003cmanifest ...\u003e\n \u003cuses-permission android:name=\"android.permission.ACCESS_FINE_LOCATION\"\n android:maxSdkVersion=\"32\" /\u003e\n \u003capplication ...\u003e\n ...\n \u003c/application\u003e\n\u003c/manifest\u003e\n```\n\nCheck for APIs that require the permission\n\nIf your app targets Android 13 or higher, you must declare the\n`NEARBY_WIFI_DEVICES` permission to call any of the following Wi-Fi APIs:\n\n- `WifiManager`\n - [`startLocalOnlyHotspot()`](/reference/android/net/wifi/WifiManager#startLocalOnlyHotspot(android.net.wifi.WifiManager.LocalOnlyHotspotCallback,%20android.os.Handler))\n- `WifiAwareManager`\n - [`attach(AttachCallback attachCallback,\n IdentityChangedListener identityChangedListener,\n Handler handler)`](/reference/android/net/wifi/aware/WifiAwareManager#attach(android.net.wifi.aware.AttachCallback,%20android.net.wifi.aware.IdentityChangedListener,%20android.os.Handler))\n- `WifiAwareSession`\n - [`publish()`](/reference/android/net/wifi/aware/WifiAwareSession#publish(android.net.wifi.aware.PublishConfig,%20android.net.wifi.aware.DiscoverySessionCallback,%20android.os.Handler))\n - [`subscribe()`](/reference/android/net/wifi/aware/WifiAwareSession#subscribe(android.net.wifi.aware.SubscribeConfig,%20android.net.wifi.aware.DiscoverySessionCallback,%20android.os.Handler))\n- `WifiP2pManager`\n - [`addLocalService()`](/reference/android/net/wifi/p2p/WifiP2pManager#addLocalService(android.net.wifi.p2p.WifiP2pManager.Channel,%20android.net.wifi.p2p.nsd.WifiP2pServiceInfo,%20android.net.wifi.p2p.WifiP2pManager.ActionListener))\n - [`connect()`](/reference/android/net/wifi/p2p/WifiP2pManager#connect(android.net.wifi.p2p.WifiP2pManager.Channel,%20android.net.wifi.p2p.WifiP2pConfig,%20android.net.wifi.p2p.WifiP2pManager.ActionListener))\n - [`createGroup()`](/reference/android/net/wifi/p2p/WifiP2pManager#createGroup(android.net.wifi.p2p.WifiP2pManager.Channel,%20android.net.wifi.p2p.WifiP2pConfig,%20android.net.wifi.p2p.WifiP2pManager.ActionListener))\n - [`discoverPeers()`](/reference/android/net/wifi/p2p/WifiP2pManager#discoverPeers(android.net.wifi.p2p.WifiP2pManager.Channel,%20android.net.wifi.p2p.WifiP2pManager.ActionListener))\n - [`discoverServices()`](/reference/android/net/wifi/p2p/WifiP2pManager#discoverServices(android.net.wifi.p2p.WifiP2pManager.Channel,%20android.net.wifi.p2p.WifiP2pManager.ActionListener))\n - [`requestDeviceInfo()`](/reference/android/net/wifi/p2p/WifiP2pManager#requestDeviceInfo(android.net.wifi.p2p.WifiP2pManager.Channel,%20android.net.wifi.p2p.WifiP2pManager.DeviceInfoListener))\n - [`requestGroupInfo()`](/reference/android/net/wifi/p2p/WifiP2pManager#requestGroupInfo(android.net.wifi.p2p.WifiP2pManager.Channel,%20android.net.wifi.p2p.WifiP2pManager.GroupInfoListener))\n - [`requestPeers()`](/reference/android/net/wifi/p2p/WifiP2pManager#requestPeers(android.net.wifi.p2p.WifiP2pManager.Channel,%20android.net.wifi.p2p.WifiP2pManager.PeerListListener))\n- `WifiRttManager`\n - [`startRanging()`](/reference/android/net/wifi/rtt/WifiRttManager#startRanging(android.net.wifi.rtt.RangingRequest,%20java.util.concurrent.Executor,%20android.net.wifi.rtt.RangingResultCallback))\n\nWi-Fi access workflows\n\nFigure 1 shows the Wi-Fi access workflow on devices that run\nAndroid 13 or higher, for apps that target\nAndroid 13 or higher. Note that, as long as you assert that your\napp doesn't derive physical location from Wi-Fi device information, you don't\nneed to declare the `ACCESS_FINE_LOCATION` permission anymore:\n**Figure 1.** Flow chart to determine whether an app that targets Android 13 (API level 33) or higher can access Wi-Fi information.\n\nFigure 2 shows the Wi-Fi access workflow on devices that run\n12L or lower. Note the reliance on the\n`ACCESS_FINE_LOCATION` permission.\n**Figure 2.** Flow chart to determine whether an app that targets 12L (API level 32) or lower can access Wi-Fi information."]]