如何连续扫描Wi-Fi网络直到找到特定的网络?

时间:2016-06-29 00:25:47

标签: android android-wifi broadcast receiver ssid

我刚刚启动了一个必须自动连接到名为" DEVICE AP"的开放接入点的应用。

我已经获得了一个包含wifi网络的列表,但我没有得到" DEVICE AP"直到我尝试扫描三次(或多或少)。

所以,我的问题是:我怎样才能持续扫描,直到找到" DEVICE AP"?

另一方面,通过这种方式连接到我的开放网络是否正确:

 private void connectDeviceAccessPoint() {
    String networkSSID = "DEVICE AP";

    WifiConfiguration wifiConfig = new WifiConfiguration();
    wifiConfig.SSID = "\"" + networkSSID + "\"";

    wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    wifiManager.addNetwork(wifiConfig);

    List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();

    for( WifiConfiguration i : list ) {
        if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
            wifiManager.disconnect();
            wifiManager.enableNetwork(i.networkId, true);
            wifiManager.reconnect();

            //Start service to send messages to device.
            startService(new Intent(WifiActivity.this, SocketService.class));
            doBindService();

            break;
        }
    }
}

提前致谢!

0 个答案:

没有答案