First on the renderings
In fact, the implementation is very simple, that is, various calls of WiFi manager. Here are some precautions and steps
- Jurisdiction
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
These are basic permissions, which will not be forgotten in general. The next one is important:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
The permission to access location information is generally forgotten. After all, network and location information are generally not associated with each other. Many people find that the list they get when using the getScanResult() method is empty. A large part of the reason is that the location permission is not given, but the api 23 can be used normally. Obviously, since the location permission is listed as dangerous permission after 6.0, it needs to be acquired dynamically, which is easy to forget.
2. Even if there is no problem with permissions, the obtained wireless list is still unstable.
This is because there is a time difference after startScan(), which can be optimized. My method is to use the broadcast monitor list to customize a broadcast to monitor wifi changes and wifi scan results
if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {//Judge wifi state change int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED); if (state == WifiManager.WIFI_STATE_DISABLED) { Log.i("info", "onReceive: Close"); } else if (state == WifiManager.WIFI_STATE_ENABLED) {//Scan when wifi is on Log.i("info", "onReceive: open"); wifiManager.startScan(); } } else if (action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {//Monitor the scan results, and obtain the results when available for (ScanResult sr : wifiManager.getScanResults()) { if (!sr.SSID.trim().equals("")) { wifiList.add(sr); } } handler.sendEmptyMessage(0); }