在Wi-Fi Scan之后,在android上显示当前的Wi-Fi列表logcat

时间:2017-04-28 04:07:10

标签: android

我想要

首先,当前的Wi-Fi扫描。 第二,如果Wi-Fi扫描完成,请检查当前的Wi-Fi列表。 第三,在logcat上显示当前的Wi-Fi列表。

我的来源

WifiManager wifi;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    registerReceiver(WifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE));
    wifi.startScan();

} 

private final BroadcastReceiver WifiReceiver = new BroadcastReceiver() {
       @Override
       public void onReceive(Context context, Intent intent) {
          if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
              List<ScanResult> mScanResults = mWifiManager.getScanResults();
              for (int i = 0; i < mScanResults.size(); i++) {
               Log.d(TAG, "get current wifi lists ->" + mScanResults.get(i).toString());   
              }
          }
       }
}

我想获得当前的wifi列表。

mScanResults.get(i).toString());为空。

为什么值为空? 如何获得当前的wifi列表?

1 个答案:

答案 0 :(得分:0)

试试这个:

    WifiManager wifi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

         wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
            if (wifi.isWifiEnabled() == false)
            {
                Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled", Toast.LENGTH_LONG).show();
                wifi.setWifiEnabled(true);
            }  
     wifi.startScan();
    registerReceiver(WifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); 

    } 

private final BroadcastReceiver WifiReceiver = new BroadcastReceiver() {
       @Override
       public void onReceive(Context context, Intent intent) {
          if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
              List<ScanResult> mScanResults = mWifiManager.getScanResults();
              for (int i = 0; i < mScanResults.size(); i++) {
               Log.d(TAG, "get current wifi lists ->" + mScanResults.get(i).toString());   
              }
          }
       }
}