درخواست مجوز برای دسترسی به دستگاههای Wi-Fi اطراف
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
برنامههایی که Android 13 (سطح API 33) یا بالاتر را هدف قرار میدهند و اتصالات Wi-Fi را مدیریت میکنند، باید مجوز زمان اجراNEARBY_WIFI_DEVICES درخواست کنند. این مجوز توجیه دسترسی برنامه به دستگاههای Wi-Fi مجاور را آسانتر میکند. در نسخههای قبلی Android، این برنامهها باید مجوز ACCESS_FINE_LOCATION اعلام کنند.
مجوز بخشی از گروه دستگاه های نزدیک است
مجوز NEARBY_WIFI_DEVICES بخشی از گروه مجوز دستگاههای نزدیک است. این گروه که در اندروید 12 (سطح API 31) اضافه شده است، شامل مجوزهای مربوط به بلوتوث و Ultra-wideband نیز می شود. وقتی هر ترکیبی از مجوزها را از این گروه مجوز درخواست میکنید، سیستم یک گفتگوی زمان اجرا را نشان میدهد و از کاربر میخواهد تا دسترسی برنامه شما به دستگاههای اطراف را تأیید کند. در تنظیمات سیستم، کاربر باید مجوزهای دستگاه های نزدیک را به صورت گروهی فعال و غیرفعال کند. برای مثال، کاربران نمی توانند دسترسی Wi-Fi را غیرفعال کنند اما دسترسی بلوتوث را برای یک برنامه مشخص فعال نگه دارند.
قویاً ادعا کنید که برنامه شما موقعیت فیزیکی را دریافت نمی کند
وقتی Android 13 یا بالاتر را هدف قرار میدهید، در نظر بگیرید که آیا برنامه شما اطلاعات مکان را از APIهای Wi-Fi دریافت میکند یا خیر. اگر نه، شما باید قویاً آن را تأیید کنید. برای انجام این ادعا، همانطور که در قطعه کد زیر نشان داده شده است، ویژگی usesPermissionFlags را در فایل مانیفست برنامه خود روی neverForLocation تنظیم کنید. این فرآیند مشابه فرآیندی است که وقتی ادعا می کنید اطلاعات دستگاه بلوتوث هرگز برای مکان استفاده نمی شود انجام می دهید:
نسخههای قبلی و برخی APIها به مجوز مکان نیاز دارند
چندین API Wi-Fi به مجوز ACCESS_FINE_LOCATION نیاز دارند، حتی زمانی که برنامه شما Android 13 یا بالاتر را هدف قرار میدهد. به عنوان مثال می توان به روش های زیر از کلاس WifiManager اشاره کرد:
همچنین، از آنجایی که مجوز NEARBY_WIFI_DEVICES فقط در Android 13 و بالاتر در دسترس است، باید هر گونه اعلامیهای را برای ACCESS_FINE_LOCATION نگه دارید تا سازگاری قبلی را در برنامه خود ارائه دهید. با این حال، تا زمانی که برنامه شما در غیر این صورت به اطلاعات دقیق موقعیت مکانی متکی نباشد، میتوانید حداکثر نسخه SDK این مجوز را روی 32 تنظیم کنید، همانطور که در قطعه کد زیر نشان داده شده است:
شکل 1 گردش کار دسترسی Wi-Fi را در دستگاههایی که Android نسخه 13 یا بالاتر را اجرا میکنند، برای برنامههایی که Android نسخه 13 یا بالاتر را هدف قرار میدهند، نشان میدهد. توجه داشته باشید که تا زمانی که ادعا می کنید که برنامه شما موقعیت فیزیکی را از اطلاعات دستگاه Wi-Fi دریافت نمی کند، دیگر نیازی به اعلام مجوز ACCESS_FINE_LOCATION ندارید:
شکل 1. نمودار جریان برای تعیین اینکه آیا برنامه ای که Android 13 (سطح API 33) یا بالاتر را هدف قرار می دهد می تواند به اطلاعات Wi-Fi دسترسی داشته باشد یا خیر.
شکل 2 گردش کار دسترسی Wi-Fi را در دستگاه هایی که 12 لیتر یا کمتر کار می کنند نشان می دهد. به اتکا به مجوز ACCESS_FINE_LOCATION توجه کنید.
شکل 2. نمودار جریان برای تعیین اینکه آیا برنامه ای که 12L (سطح API 32) یا کمتر را هدف قرار می دهد می تواند به اطلاعات Wi-Fi دسترسی داشته باشد یا خیر.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-08-27 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-08-27 بهوقت ساعت هماهنگ جهانی."],[],[],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."]]