Android的使用wifimanager连接到WPA-PSK安全网络 [英] Android using wifimanager to connect to WPA-PSK secured network

查看:351
本文介绍了Android的使用wifimanager连接到WPA-PSK安全网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我真正的拖网在这里的所有线程和任何地方其他地方谷歌会带我。不过还是我有连接到WPA PSK网络问题。
这里是我的code,我有我读SSID和PSK,然后一个复选框以选择,如果SSID被隐藏或不2的EditText领域。

so I have trawled true all threads in here and any where else where google would take me. But still I am having problems connecting to WPA PSK networks. Here is my code, I have 2 edittext fields from which I read SSID and PSK and then one checkbox to select if SSID is hidden or not.

    EditText mSSID = (EditText) findViewById(R.id.wifiTVssidcurrent);
    String networkSSID = mSSID.getText().toString();
    EditText mWPA = (EditText) findViewById(R.id.wifiTVwpacurrent);
    String networkWPA = mWPA.getText().toString();

    // Update text to show that connection is pending
    TextView wifiStatus = (TextView) findViewById(R.id.wifiTVconnectionstatus);
    wifiStatus.setText("Connecting to " + networkSSID);

    WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
    WifiConfiguration wc = new WifiConfiguration(); 
    wc.SSID = "\"".concat(networkSSID).concat("\""); 
    wc.preSharedKey  = "\"".concat(networkWPA).concat("\"");

    CheckBox mSSIDHidden = (CheckBox) findViewById(R.id.wifiCBhiddenssid);
    wc.hiddenSSID = false;
    if (mSSIDHidden.isChecked()) {
        wc.hiddenSSID = true;
    }
    wc.status = WifiConfiguration.Status.ENABLED;         
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); 
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); 
    wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); 
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); 
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); 
    wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    int res = wifi.addNetwork(wc); 
    Log.d("WifiPreference", "add Network returned " + res ); 
    boolean b = wifi.enableNetwork(res, true);         
    Log.d("WifiPreference", "enableNetwork returned " + b );
    boolean c = wifi.reconnect();
    Log.d("WifiPreference", "reconnect returned " + c );

我看到手机运行此之后,一个AP中设置创建的,但它没有连接。如果我尝试从设置使用手动创建的AP之后,我无法选择连接。
但是,如果我从设置中创建AP我得到的连接,我应该。

What I see in the phone after running this is that an AP is created in settings, but it is not connecting. And if I try to use the created AP manually from settings afterwards I am not able to connect either. But if I create the AP from within settings I get connection as I should.

至于把SSID和WPA PSK在我已经试过这两个\\CONCAT(networkSSID).concat(\\);和\\+ networkSSID +\\;用同样的结果。

As for putting SSID and WPA PSK in I have tried both "\"".concat(networkSSID).concat("\""); and "\""+ networkSSID +"\""; with same result.

任何提示将非常欢迎。
最好的祝福
拉塞

Any tips will be very welcome. Best regards Lasse

推荐答案

再次非常感谢瑞安提出如何读取由操作系统创建的设置。

Again big thanks to Ryan for proposing how to read the settings created by the OS.

所以,我以为我已经试过这里的一切建议的堆栈溢出。
但这个线程实际帮助我在编程方式使用WPA安全性在Android平板电脑所以非常感谢RYAN提供的提示结束设置无线网络在阅读一个AP,然后mimmick它。在我的情况下,它原来,我需要添加即使我设置的WPA我还需要添加allowedGroupCiphers WEP40和WEP104而我只有

So I thought I had tried everything suggested here on stack overflow. But this thread actually helped me in the end Setup wifi programatically using WPA Security in android tablet So big thanks to RYAN for providing the tips on reading one AP and then mimmick it. In my case it turned out that I needed to add Even though I am setting up for WPA I still needed to add allowedGroupCiphers WEP40 and WEP104 And I only had

`wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); //So I also added 
 wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);`

这篇关于Android的使用wifimanager连接到WPA-PSK安全网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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