myWifiManager.getScanResults()返回大小为0的列表

时间:2018-01-20 17:34:16

标签: java android android-wifi

我正在尝试使用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
        }
    }
}

0 个答案:

没有答案