1, increase the authority:
Because this app needs to get the phone's network information, WiFi information, so need to add the relevant permissions in the Androidmanifest.xml file, otherwise it will be reported permission deny error:
1<uses-permissionAndroid:name= "Android.permission.INTERNET"/>2 <uses-permissionAndroid:name= "Android.permission.CHANGE_WIFI_STATE"></uses-permission> 3 <uses-permissionAndroid:name= "Android.permission.ACCESS_WIFI_STATE"></uses-permission>4 <uses-permissionAndroid:name= "Android.permission.ACCESS_NETWORK_STATE"></uses-permission>
2. Get Wifimanager object:
1 wifimanager wm = (Wifimanager) getsystemservice (wifi_service); // Get WiFi service
Note: Android background runs in many service, they are systemserver on when the system starts, support the system to work properly, Getsystemservice is an important API for Android, Based on the name passed in to obtain the corresponding object, and then converted to the corresponding service object,Getsystemservice is the context of an abstract class method .
3. Start the scan:
1 wm.startscan (); // initiates a scan.
Note: This method is returned immediately , should be a new boot thread for the AP scan, so it is best to wait a while (a short time) to obtain the results.
4. Get scan results:
1 list<scanresult> scanlist = Wmanager.getscanresults (); 2 ( Scanresult scanresult:scanlist) { 3 // action on scan results 4 String mac = Scanresult.bssid; // Get the MAC address of the AP 5 String SSID = Scanresult.ssid; // Get Service Set identity (router name) 6 int rssi = scanresult.level; // wifi signal strength in dbm 7 }