我正在尝试使用Android应用程序找到我附近的所有接入点
所以我用WifiManager扫描了它们。在此过程之前,我启用了wifi并注册了BroadCasterReceiver.But它返回零大小的接入点列表。
下面是活动启动
@Override
protected void onCreate(Bundle savedInstanceState) {
WifiManager myWifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
myWifiManager.setWifiEnabled(true);
WifiReceiver receiverwifi = new WifiReceiver();
myWifiManager.startScan();
IntentFilter i = new IntentFilter();
i.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
registerReceiver(receiverwifi,i);
}
以下是BroadCasterReceiver的代码
class WifiReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
WifiManager myWifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(intent.getAction()) && myWifiManager.startScan() && myWifiManager.isWifiEnabled()){
List<ScanResult> scans= myWifiManager.getScanResults();
System.out.println("size of list is "+ scans.size());
//Remaining code
}
}
}