我正在構建我的Android Nougat的代碼。掃描WiFi列表返回0在Android nougat
我已經給出了清單文件中的位置和WiFi權限,但是仍然從wlan_Manager.getScanResults()獲得「0」。
我的代碼段是
wlan_Manager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wlan_switch = (Switch)findViewById(R.id.switch1);
list = (ListView)findViewById(R.id.listview);
wlan_switch.setChecked(wlan_Manager.isWifiEnabled());
wifi_list = new ArrayList<>();
adapter = new Adapter(this,R.layout.default_listview,wifi_list);
list.setAdapter(adapter);
wifireceiver = new wifi_Receiver(adapter,wifi_list);
registerReceiver(wifireceiver,new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
。
public void onClick(View view) {
Log.d("switch", "onClick: ");
if (wlan_switch.isChecked() && !wlan_Manager.isWifiEnabled())
{
Log.d(TAG, "true: ");
wlan_Manager.setWifiEnabled(true);
wifi_list.clear();
wlan_Manager.startScan();
}
else {
Log.d(TAG, "false ");
wlan_Manager.setWifiEnabled(false);
wifi_list.clear();
adapter.clear();
}
}
。
@Override
public void onReceive(Context context, Intent intent)
{
int size =0 ;
//scan_Result.clear();
Log.d("WifScanner", "onReceive");
wlan_Manager = (WifiManager)context.getApplicationContext().getSystemService(context.WIFI_SERVICE);
scan_Result = wlan_Manager.getScanResults();
size = scan_Result.size();
Log.d(TAG, "onReceive: " + size);
while (size > 0)
{
Log.d(TAG, "size : "+size);
size--;
wifi_list.add(scan_Result.get(size).SSID);
adapter.notifyDataSetChanged();
}
}
但是,當我爲較低的Android版本構建代碼時,它顯示WiFi列表。