0
我想一個Android應用程序,可以連接到WiFi設備上的按鈕單擊自動:自動WiFi聯機在Android應用
這裏我的WiFi設備
SSID: HI_LINK-DA79
Encrypt Type: WPA/WPA2 TKIP
我的應用程序無線上網顯示之前:
HI_LINK-DA79 Secured with WPA/WPA2
Button.OnClickListener buttonConnectOnClickListener = new Button.OnClickListener() {
public void onClick(View v) {
/*********************************************/
int test=0;
String networkSSID = "HI-LINK_DA79";
String networkPass = "12345678";
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\""; //ssid must be in quotes
conf.wepKeys[0] = "\""+ networkPass +"\"";
conf.wepTxKeyIndex = 0;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
conf.preSharedKey = "\""+ networkPass +"\"";
WifiManager wifiManager = (WifiManager)getSystemService(WIFI_SERVICE);
int r1 = wifiManager.addNetwork(conf);
Log.d("Client", "add Network returned " + r1);
boolean b = wifiManager.enableNetwork(r1, true);
Log.d("Client", "enableNetwork returned " + b);
boolean d= wifiManager.reconnect();
Log.d("Client", "wifiManager.reconnect() returned " + d);
WifiInfo info = wifiManager.getConnectionInfo();
Log.i("Client", "ipaddr: " +info.getIpAddress());
Log.i("Client", "SSID: " +info.getSSID());
Log.i("Client", "MAC: " +info.getMacAddress());
Log.i("Client", "NetId: " +info.getNetworkId());
}
這裏是logcat的消息看起來不錯,但其未連接,運行此之後,我可以看到
04-20 18:36:13.790: D/Client(24203): add Network returned 2
04-20 18:36:13.810: D/Client(24203): enableNetwork returned true
04-20 18:36:13.810: D/Client(24203): wifiManager.reconnect() returned true
04-20 18:36:13.810: I/Client(24203): ipaddr: 0
04-20 18:36:13.810: I/Client(24203): SSID: HI-LINK_DA79
04-20 18:36:13.810: I/Client(24203): MAC: 8c:77:16:8c:a7:36
04-20 18:36:13.810: I/Client(24203): NetId: -1
我沒有錯誤,應該接通後期待,但無線上網顯示「保存」
HI_LINK- DA71保存,用WPA/WPA2保護
爲什麼它沒有連接?在配置中還有什麼我必須做的嗎?
在此先感謝
我上面的代碼更新,並與'conf.allowedGroupCiphers.set嘗試( WifiConfiguration.GroupCipher.TKIP);'嘗試仍然沒有連接'Log.i(「Client」,「NetId:」+ info.getNetworkId()); '這是返回-1看起來像問題在這裏 – ashok449
這裏是我的Manifest.xml'<使用權限android:name =「android.permission.INTERNET」> uses-permission> <使用權限android:name = 「android.permission.ACCESS_NETWORK_STATE」> uses-permission> uses-permission> uses-permission> uses-permission>' –
ashok449
@ ashok449看到我編輯的答案。 –