Andorid连接wifi最佳实践

概述

之前在一家人工智能公司做智能助手的开发,由于都是基于第三方硬件开发,所以联网这块就比较重要,基本都是使用wifi连接,今天就来讲一下WIFI连接;

直接上马:

 public BotNetworkConnected ConnectToNetworkWPA(final Context context, final String networkSSID, final String password) {
    isConnecting = true;
    if (TextUtils.isEmpty(networkSSID)) {
        FuncUtils.sleep(5000);
        BotAudioManager.newInstance(context).addListener(new AudioManagerListener() {
            @Override
            public void onCpmpleted() {
                if (callback != null) {
                    NetworkConnectCallbackParam callbackParam =
                            new NetworkConnectCallbackParam("networkSSID 账号为空", CODE_CONNECTED_FAILED, networkSSID, password);
                    callback.onNetworkConnected(callbackParam);
                }
                isConnecting = false;//联网结束
            }
        }).play(R.raw.net_connected_failed);
    } else {
  
        BotAudioManager.newInstance(context).addListener(new AudioManagerListener() {
            @Override
            public void onCpmpleted() {
                if (TextUtils.isEmpty(password.trim())) {// 没有密码
                    //新建wifi配置
                    WifiConfiguration config = new WifiConfiguration();
                    config.allowedAuthAlgorithms.clear();
                    config.allowedGroupCiphers.clear();
                    config.allowedKeyManagement.clear();
                    config.allowedPairwiseCiphers.clear();
                    config.allowedProtocols.clear();
                    config.SSID = "\"" + networkSSID + "\"";
                    // 没有密码
                    config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

                    wifiManager.addNetwork(config);
                    wifiManager.saveConfiguration();
                } else {// 有密码

                    //新建wifi配置
                    WifiConfiguration conf = new WifiConfiguration();
                    conf.SSID = "\"" + networkSSID + "\"";
                    conf.preSharedKey = "\"" + password + "\"";
                    conf.status = WifiConfiguration.Status.ENABLED;
                    conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
                    conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                    conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                    conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);

                    Log.e("xm", conf.SSID + " isConnecting " + conf.preSharedKey);

                    int i1 = wifiManager.addNetwork(conf);
                    BotLog.e("---" + i1);

                    Log.e("xm", conf.SSID + " after isConnecting  " + conf.preSharedKey);
                }

                //拿到所有的wifi配置信息,与之前新建的配置信息匹配,连接
                List list = wifiManager.getConfiguredNetworks();
                if (list != null) {
                    for (WifiConfiguration i : list) {
                        //连接与指定名称相同的wifi
                        if (i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
                            wifiManager.disconnect();
                            wifiManager.enableNetwork(i.networkId, true);
                            wifiManager.reconnect();
                            wifiManager.saveConfiguration();
                            break;
                        }
                    }
                }
                //等待 outTime 毫秒
                boolean isNotConnected = false;
                for (int len = 0; !isNotConnected && len <= outTime; len += 500, isNotConnected = isConnected()) {
                    FuncUtils.sleep(500);
                }
                isConnected(isNotConnected, networkSSID, password, context);

            }
        }).play(R.raw.net_connecting);
    }
    return create(context);
}

下面代码是比较重要的:

 for (int len = 0; !isNotConnected && len <= outTime; len += 500, isNotConnected = isConnected()) {
                    FuncUtils.sleep(500);
                }

outTime这个是等待时间,不能太长也不能太短,太长影响用户体验,如果联网失败,就不能自动再重新连接以前联过的WIFI了;

你可能感兴趣的:(Andorid连接wifi最佳实践)