Android以编程方式切换AP

时间:2016-10-05 11:13:54

标签: java android android-studio android-wifi wifimanager

我需要检查所有可用的wifi,然后连接到我当前连接的另一个。

样本 - 设备连接到AP01

    private void switchAP(String  ssid, String  pass)) {

        WifiConfiguration conf = new WifiConfiguration();
        conf.SSID               = "\"" + ssid + "\"";
        if(!TextUtils.isEmpty(pass))
            conf.wepKeys[0]     = "\"" + pass + "\"";
        conf.preSharedKey       = "\"" + pass + "\"";
        conf.wepTxKeyIndex      = 0;
        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

        WifiManager wifiManager = (WifiManager) SyncActivity.this.getSystemService(Context.WIFI_SERVICE);
        wifiManager.addNetwork(conf);

//        List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
        List<ScanResult> list = wifiManager.getScanResults();
        for (ScanResult i : list) {

            Log.w(TAG, "WIFI LIST > " + i.SSID);

            if (i.SSID != null && i.SSID.equals(ssid)) {
                wifiManager.disconnect();
                wifiManager.enableNetwork(i.networkId, true); //>i.networkId Does not work - only in List<WifiConfiguration>…
                wifiManager.reconnect();
                logMsg("WiFi connection switched to " + ssid);
                break;
            }

        }
    }

然后我打电话给

switchAP("AP02", ""));

我无法连接,因为当我使用ScanResult而不是WifiConfiguration时, i.networkId 似乎不起作用。

有没有人遇到过这个? 谢谢你们。

我之所以不使用List,是因为之前设备从未连接过此SSOD。 和列表仅返回连接网络的所有预览。

1 个答案:

答案 0 :(得分:0)

您需要使用WifiConfiguration.Status.ENABLED

下面为您编写示例代码:

public void WIFI_CONNECT_WIFI (String ssid, String password)
{
    WifiManager wifi_manager = (WifiManager)getSystemService(Context.WIFI_SERVICE);

    WifiConfiguration wifi_configuration = new WifiConfiguration();
    wifi_configuration.SSID              = String.format("\"%s\"", ssid);
    wifi_configuration.preSharedKey      = String.format("\"%s\"", password);


    if (WIFI_NETWORK_EXIST (wifi_configuration) == true) {
        wifi_configuration.status   = WifiConfiguration.Status.ENABLED;
        wifi_configuration.priority = 40;
    }
    else {
        boolean result = wifi_manager.getConfiguredNetworks().add(wifi_configuration);
        if (result == false) {
            return;
        }
        wifi_configuration.status    = WifiConfiguration.Status.ENABLED;
        wifi_configuration.priority  = 40;
        wifi_configuration.networkId = wifi_manager.addNetwork(wifi_configuration);
    }

    wifi_manager.enableNetwork(wifi_configuration.networkId, true);
    wifi_manager.saveConfiguration();
}

-

public boolean WIFI_NETWORK_EXIST (WifiConfiguration wifi_configuration)
{
    WifiManager wifi_manager = (WifiManager)getSystemService(Context.WIFI_SERVICE);

    for (WifiConfiguration config : wifi_manager.getConfiguredNetworks()) {

        if (wifi_configuration.SSID.equals(config.SSID)) {
            wifi_configuration.networkId = config.networkId;
            return true;
        }
    }
    return false;
}

顺便说一下,如果有可能获得SSID为null,那么当你尝试使用null值的成员函数时,当SSID带有null值时,你总是会崩溃。

 if (i.SSID != null && i.SSID.equals(ssid)) {
     // ...
 }

预期的堆栈跟踪是:&#34; bla bla bla试图在空值上使用 String.equals(String)。&#34;

对于equals(...)比较,你应该做一个内部或不同的if比较。