valwifiManager=context.getSystemService(Context.WIFI_SERVICE)asWifiManagervalwifiScanReceiver=object:BroadcastReceiver(){overridefunonReceive(context:Context,intent:Intent){valsuccess=intent.getBooleanExtra(WifiManager.EXTRA_RESULTS_UPDATED,false)if(success){scanSuccess()}else{scanFailure()}}}valintentFilter=IntentFilter()intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)context.registerReceiver(wifiScanReceiver,intentFilter)valsuccess=wifiManager.startScan()if(!success){// scan failure handlingscanFailure()}....privatefunscanSuccess(){valresults=wifiManager.scanResults...usenewscanresults...}privatefunscanFailure(){// handle failure: new scan did NOT succeed// consider using old scan results: these are the OLD results!valresults=wifiManager.scanResults...potentiallyuseolderscanresults...}
Java
WifiManagerwifiManager=(WifiManager)context.getSystemService(Context.WIFI_SERVICE);BroadcastReceiverwifiScanReceiver=newBroadcastReceiver(){@OverridepublicvoidonReceive(Contextc,Intentintent){booleansuccess=intent.getBooleanExtra(WifiManager.EXTRA_RESULTS_UPDATED,false);if(success){scanSuccess();}else{// scan failure handlingscanFailure();}}};IntentFilterintentFilter=newIntentFilter();intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);context.registerReceiver(wifiScanReceiver,intentFilter);booleansuccess=wifiManager.startScan();if(!success){// scan failure handlingscanFailure();}....privatevoidscanSuccess(){List<ScanResult>results=wifiManager.getScanResults();...usenewscanresults...}privatevoidscanFailure(){// handle failure: new scan did NOT succeed// consider using old scan results: these are the OLD results!List<ScanResult>results=wifiManager.getScanResults();...potentiallyuseolderscanresults...}
[null,null,["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Wi-Fi scanning overview\n\nYou can use the Wi-Fi scanning capabilities provided by the\n[WifiManager API](/reference/android/net/wifi/WifiManager) to get a list of\nWi-Fi access points that are visible from the device.\n\nWi-Fi scanning process\n----------------------\n\nThere are three steps to the scanning process:\n\n1. **Register a broadcast listener** for\n [`SCAN_RESULTS_AVAILABLE_ACTION`](/reference/android/net/wifi/WifiManager#SCAN_RESULTS_AVAILABLE_ACTION),\n which is called when scan requests are completed, providing their\n success/failure status. For devices running Android 10 (API level 29) and higher, this\n broadcast will be sent for any full Wi-Fi scan performed on the device by\n the platform or other apps. Apps can passively listen to all scan\n completions on device by using the broadcast without issuing a scan of their\n own.\n\n2. **Request a scan** using\n [`WifiManager.startScan()`](/reference/android/net/wifi/WifiManager#startScan()).\n Make sure to check the return status of the method, since the call may fail\n for any of the following reasons:\n\n - Scan requests may be throttled because of too many scans in a short time.\n - The device is idle and scanning is disabled.\n - Wi-Fi hardware reports a scan failure.\n3. **Get scan results** using\n [`WifiManager.getScanResults()`](/reference/android/net/wifi/WifiManager#getScanResults()).\n The returned scan results are the most recently updated results, which may\n be from a previous scan if your current scan has not completed or succeeded.\n This means that you might get older scan results if you call this method\n before receiving a successful\n [`SCAN_RESULTS_AVAILABLE_ACTION`](/reference/android/net/wifi/WifiManager#SCAN_RESULTS_AVAILABLE_ACTION)\n broadcast.\n\nThe following code provides an example of how to implement these steps: \n\n### Kotlin\n\n```kotlin\nval wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager\n\nval wifiScanReceiver = object : BroadcastReceiver() {\n\n override fun onReceive(context: Context, intent: Intent) {\n val success = intent.getBooleanExtra(WifiManager.EXTRA_RESULTS_UPDATED, false)\n if (success) {\n scanSuccess()\n } else {\n scanFailure()\n }\n }\n}\n\nval intentFilter = IntentFilter()\nintentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)\ncontext.registerReceiver(wifiScanReceiver, intentFilter)\n\nval success = wifiManager.startScan()\nif (!success) {\n // scan failure handling\n scanFailure()\n}\n\n....\n\nprivate fun scanSuccess() {\n val results = wifiManager.scanResults\n ... use new scan results ...\n}\n\nprivate fun scanFailure() {\n // handle failure: new scan did NOT succeed\n // consider using old scan results: these are the OLD results!\n val results = wifiManager.scanResults\n ... potentially use older scan results ...\n}\n```\n\n### Java\n\n```java\nWifiManager wifiManager = (WifiManager)\n context.getSystemService(Context.WIFI_SERVICE);\n\nBroadcastReceiver wifiScanReceiver = new BroadcastReceiver() {\n @Override\n public void onReceive(Context c, Intent intent) {\n boolean success = intent.getBooleanExtra(\n WifiManager.EXTRA_RESULTS_UPDATED, false);\n if (success) {\n scanSuccess();\n } else {\n // scan failure handling\n scanFailure();\n }\n }\n};\n\nIntentFilter intentFilter = new IntentFilter();\nintentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);\ncontext.registerReceiver(wifiScanReceiver, intentFilter);\n\nboolean success = wifiManager.startScan();\nif (!success) {\n // scan failure handling\n scanFailure();\n}\n\n....\n\nprivate void scanSuccess() {\n List\u003cScanResult\u003e results = wifiManager.getScanResults();\n ... use new scan results ...\n}\n\nprivate void scanFailure() {\n // handle failure: new scan did NOT succeed\n // consider using old scan results: these are the OLD results!\n List\u003cScanResult\u003e results = wifiManager.getScanResults();\n ... potentially use older scan results ...\n}\n```\n\nRestrictions\n------------\n\nAndroid 8.0 (API level 26) introduced restrictions regarding permissions and the\nallowed frequency of Wi-Fi scans.\n\nTo improve network performance, security, and battery life, Android 9 (API\nlevel 28) tightened permission requirements and further limited the frequency of\nWi-Fi scans.\n\n### Permissions\n\n| **Note:** In each of the following sections that mention location permissions or location-gathering logic, keep in mind that, when your app is running in the background, [access to location](/training/location/background) should be critical to the core functionality of the app and is accompanied with proper disclosure to users.\n\n**Android 8.0 and Android 8.1:**\n\nA successful call to\n[`WifiManager.getScanResults()`](/reference/android/net/wifi/WifiManager#getScanResults())\nrequires *any one* of the following permissions:\n\n- [`ACCESS_FINE_LOCATION`](/reference/android/Manifest.permission#ACCESS_FINE_LOCATION)\n- [`ACCESS_COARSE_LOCATION`](/reference/android/Manifest.permission#ACCESS_COARSE_LOCATION)\n- [`CHANGE_WIFI_STATE`](/reference/android/Manifest.permission#CHANGE_WIFI_STATE)\n\nIf the calling app does not have any of these permissions, the call fails with a\n[`SecurityException`](/reference/java/lang/SecurityException).\n\nAlternatively, on devices running Android 8.0 (API level 26) and higher, you can\nuse the\n[`CompanionDeviceManager`](/reference/android/companion/CompanionDeviceManager)\nto perform a scan of nearby companion devices on behalf of your app without\nrequiring the location permission. For more on this option, see\n[Companion device\npairing](/develop/connectivity/bluetooth/companion-device-pairing).\n\n**Android 9:**\n\nA successful call to\n[`WifiManager.startScan()`](/reference/android/net/wifi/WifiManager#startScan())\nrequires *all* of the following conditions to be met:\n\n- Your app has the [`ACCESS_FINE_LOCATION`](/reference/android/Manifest.permission#ACCESS_FINE_LOCATION) **or** [`ACCESS_COARSE_LOCATION`](/reference/android/Manifest.permission#ACCESS_COARSE_LOCATION) permission.\n- Your app has the [`CHANGE_WIFI_STATE`](/reference/android/Manifest.permission#CHANGE_WIFI_STATE) permission.\n- Location services are enabled on the device (under **Settings \\\u003e Location**).\n\n**Android 10 (API level 29) and higher:**\n\nA successful call to\n[`WifiManager.startScan()`](/reference/android/net/wifi/WifiManager#startScan())\nrequires *all* of the following conditions to be met:\n\n- If your app is targeting Android 10 (API level 29) SDK or higher, your app has the [`ACCESS_FINE_LOCATION`](/reference/android/Manifest.permission#ACCESS_FINE_LOCATION) permission.\n- If your app is targeting SDK lower than Android 10 (API level 29), your app has the [`ACCESS_COARSE_LOCATION`](/reference/android/Manifest.permission#ACCESS_COARSE_LOCATION) or [`ACCESS_FINE_LOCATION`](/reference/android/Manifest.permission#ACCESS_FINE_LOCATION) permission.\n- Your app has the [`CHANGE_WIFI_STATE`](/reference/android/Manifest.permission#CHANGE_WIFI_STATE) permission.\n- Location services are enabled on the device (under **Settings** \\\u003e **Location**).\n\nTo successfully call\n[`WifiManager.getScanResults()`](/reference/android/net/wifi/WifiManager#getScanResults()),\nensure *all* of the following conditions are met:\n\n- If your app is targeting Android 10 (API level 29) SDK or higher, your app has the `ACCESS_FINE_LOCATION` permission.\n- If your app is targeting SDK lower than Android 10 (API level 29), your app has the `ACCESS_COARSE_LOCATION` or `ACCESS_FINE_LOCATION` permission.\n- Your app has the [`ACCESS_WIFI_STATE`](/reference/android/Manifest.permission#ACCESS_WIFI_STATE) permission.\n- Location services are enabled on the device (under **Settings** \\\u003e **Location**).\n\nIf the calling app doesn't meet all of these requirements, the call fails with\na [`SecurityException`](/reference/java/lang/SecurityException).\n\n### Throttling\n\nThe following limitations apply to the frequency of scans using\n[`WifiManager.startScan()`](/reference/android/net/wifi/WifiManager#startScan()).\n\n**Android 8.0 and Android 8.1:**\n\nEach background app can scan one time in a 30-minute period.\n\n**Android 9:**\n\nEach foreground app can scan four times in a 2-minute period. This allows for a\nburst of scans in a short time.\n\nAll background apps combined can scan one time in a 30-minute period.\n\n**Android 10 and higher:**\n\nThe same throttling limits from Android 9 apply. There is a new developer option\nto toggle the throttling off for local testing (under **Developer Options \\\u003e**\n**Networking \\\u003e Wi-Fi scan throttling**)."]]