以编程方式连接到Wifi不适用于Android Marshmallow吗? [英] Connect to Wifi programmatically not working for Android Marshmallow?

查看:101
本文介绍了以编程方式连接到Wifi不适用于Android Marshmallow吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

String BACKSLASH = "\"";
String NETWROK_SECURITY_WEP = "WEP";
String NETWROK_SECURITY_NONE = "NONE";
String NETWROK_SECURITY_WPA = "WPA";
String NETWROK_SECURITY_WPA2 = "WPA2";
String NETWROK_SECURITY_WPA_WPA2 = "WPA/WPA2 PSK";
String NETWROK_ADDITIONAL_SECURITY_TKIP = "TKIP";
String NETWROK_ADDITIONAL_SECURITY_AES = "AES";
String NETWROK_ADDITIONAL_SECURITY_WEP = "WEP";
String NETWROK_ADDITIONAL_SECURITY_NONE = "NONE";
int FAILED_TO_ADD_NETWORK = -1;

WifiConfiguration conf = new WifiConfiguration();
String wifiName = sSid;
conf.SSID = BACKSLASH + wifiName + BACKSLASH;
String securityType = NETWROK_SECURITY_WPA_WPA2;

if (NETWROK_SECURITY_WEP.equalsIgnoreCase(securityType)) {
    conf.wepKeys[0] = pass;
    conf.wepTxKeyIndex = 0;
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
} else if (NETWROK_SECURITY_NONE.equalsIgnoreCase(securityType)) {
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
} else if (NETWROK_SECURITY_WPA.equalsIgnoreCase(securityType)
        || NETWROK_SECURITY_WPA2.equalsIgnoreCase(securityType)
        || NETWROK_SECURITY_WPA_WPA2.equalsIgnoreCase(securityType)) {
    conf.preSharedKey = BACKSLASH + pass + BACKSLASH;
    conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
    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);
    conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
}
String wlanAdditionalSecurity = "";
if (NETWROK_ADDITIONAL_SECURITY_TKIP.equalsIgnoreCase(wlanAdditionalSecurity)) {
    conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
    conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
} else if (NETWROK_ADDITIONAL_SECURITY_AES.equalsIgnoreCase(wlanAdditionalSecurity)) {
    conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
    conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
} else if (NETWROK_ADDITIONAL_SECURITY_WEP.equalsIgnoreCase(wlanAdditionalSecurity)) {
    conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
    conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
} else if (NETWROK_ADDITIONAL_SECURITY_NONE.equalsIgnoreCase(wlanAdditionalSecurity)) {
    conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.NONE);
}
WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
int res = wifiManager.addNetwork(conf);
wifiManager.disconnect();
wifiManager.reconnect();

if (true) {

    wifiManager.enableNetwork(res, true);

    wifiManager.saveConfiguration();
    wifiManager.setWifiEnabled(true);

    new AppPreferences(mContext).setPrefrenceLong("connectTime", Calendar.getInstance().getTimeInMillis());
}
if (res != -1) {

    setFalseOther(mm);
    notifyDataSetChanged();

    Intent i = new Intent(mContext, Connect.class);
    i.putExtra("networkName", mm.getName());
    i.putExtra("networkId", mm.getId());
    i.putExtra("AdminID", mm.getUserId());
    i.putExtra("networkConnection", "true");

    mContext.startActivity(i);

} else {

}

此代码用于连接WiFi.在Android Lollipop上运行正常,但我想以编程方式为棉花糖设置配置.我已授予所有运行时权限和危险权限,但仍无济于事,无法删除保存的WiFi密码.如果有人为棉花糖做过这将是有帮助的.

This code is for connecting to WiFi. It's working fine on Android Lollipop but I want to set up configuration programmatically for Marshmallow. I've given all the run time permissions and dangerous permission but still no help and unable to remove saved WiFi passwords. If anyone has done this for Marshmallow it would be helpful.

推荐答案

好吧...我们对连接到棉花糖中wifi网络的代码进行了少量更改.我认为以下代码将为您服务...

Well...We have small changes in the code for connect to the wifi network in Marshmallow.. I think the following code will works for you...

public void connectToWifi(){
    try{
        WifiManager wifiManager = (WifiManager) super.getSystemService(android.content.Context.WIFI_SERVICE);
        WifiConfiguration wc = new WifiConfiguration();
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        wc.SSID = "\"NETWORK_NAME\"";
        wc.preSharedKey = "\"PASSWORD\"";
        wc.status = WifiConfiguration.Status.ENABLED;
        wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        int netId = wifiManager.addNetwork(wc);
        if (netId == -1) {
            netId = getExistingNetworkId(wc.SSID);
        }
        wifiManager.disconnect();
        wifiManager.enableNetwork(netId, true);
        wifiManager.reconnect();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
private int getExistingNetworkId(String SSID) {
    WifiManager wifiManager = (WifiManager) super.getSystemService(Context.WIFI_SERVICE);
    List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
    if (configuredNetworks != null) {
        for (WifiConfiguration existingConfig : configuredNetworks) {
            if (existingConfig.SSID.equals(SSID)) {
                return existingConfig.networkId;
            }
        }
    }
    return -1;
}

并在清单文件中添加权限

And add Permissions in the Manifest file

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />

这篇关于以编程方式连接到Wifi不适用于Android Marshmallow吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆