禁用此示例并禁用 gps,我没有得到扫描结果。启用 GPS 后,我得到了结果。(在安卓 7 上测试(我必须启用 GPS 正常吗?有没有办法在没有GPS的情况下获得所有wifi的BSSID?
public void readWifis(Activity activity) {
// Get the wifi manager.
// Provides the primary API for managing all aspects of Wi-Fi connectivity.
WifiManager wifiManager = (WifiManager) activity.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
activity.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent intent) {
List<ScanResult> results = wifiManager.getScanResults();
for (ScanResult ap : results) {
Log.d("myTag", "SSID=" + ap.SSID + " MAC=" + ap.BSSID);
}
}
},
// An access point scan has completed, and results are available from the supplicant. Call getScanResults() to obtain the results.
new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
// Request a scan for access points.
wifiManager.startScan();
}
您必须启用的不是GPS,而是根据请求的位置使用GPS,Wi-Fi或移动网络的位置服务。
BSSID和SSID的问题在于,它们被视为位置信息,因此不仅需要用户授予位置权限(从Android 8.1开始(,还需要启用位置服务。