您有任何想法如何建立無線連接與發送密碼在我的Android應用程序?以編程方式安卓WiFi連接
9
A
回答
30
將SSID及其密碼傳遞給以下方法。
public void connectToAP(String ssid, String passkey) {
Log.i(TAG, "* connectToAP");
WifiConfiguration wifiConfiguration = new WifiConfiguration();
String networkSSID = ssid;
String networkPass = passkey;
Log.d(TAG, "# password " + networkPass);
for (ScanResult result : scanResultList) {
if (result.SSID.equals(networkSSID)) {
String securityMode = getScanResultSecurity(result);
if (securityMode.equalsIgnoreCase("OPEN")) {
wifiConfiguration.SSID = "\"" + networkSSID + "\"";
wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
int res = wifiManager.addNetwork(wifiConfiguration);
Log.d(TAG, "# add Network returned " + res);
boolean b = wifiManager.enableNetwork(res, true);
Log.d(TAG, "# enableNetwork returned " + b);
wifiManager.setWifiEnabled(true);
} else if (securityMode.equalsIgnoreCase("WEP")) {
wifiConfiguration.SSID = "\"" + networkSSID + "\"";
wifiConfiguration.wepKeys[0] = "\"" + networkPass + "\"";
wifiConfiguration.wepTxKeyIndex = 0;
wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
int res = wifiManager.addNetwork(wifiConfiguration);
Log.d(TAG, "### 1 ### add Network returned " + res);
boolean b = wifiManager.enableNetwork(res, true);
Log.d(TAG, "# enableNetwork returned " + b);
wifiManager.setWifiEnabled(true);
}
wifiConfiguration.SSID = "\"" + networkSSID + "\"";
wifiConfiguration.preSharedKey = "\"" + networkPass + "\"";
wifiConfiguration.hiddenSSID = true;
wifiConfiguration.status = WifiConfiguration.Status.ENABLED;
wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
int res = wifiManager.addNetwork(wifiConfiguration);
Log.d(TAG, "### 2 ### add Network returned " + res);
wifiManager.enableNetwork(res, true);
boolean changeHappen = wifiManager.saveConfiguration();
if(res != -1 && changeHappen){
Log.d(TAG, "### Change happen");
AppStaticVar.connectedSsidName = networkSSID;
}else{
Log.d(TAG, "*** Change NOT happen");
}
wifiManager.setWifiEnabled(true);
}
}
}
public String getScanResultSecurity(ScanResult scanResult) {
Log.i(TAG, "* getScanResultSecurity");
final String cap = scanResult.capabilities;
final String[] securityModes = { "WEP", "PSK", "EAP" };
for (int i = securityModes.length - 1; i >= 0; i--) {
if (cap.contains(securityModes[i])) {
return securityModes[i];
}
}
return "OPEN";
}
不要忘記在Manifest文件中添加必要的權限。
2
這裏發佈的代碼AnujAroshA真的很有幫助。但是還有一個缺失的情況:
..
} else if (securityMode.equalsIgnoreCase("PSK")) {
// PSK code
..
否則PSK碼也會針對WEP和OPEN網絡執行。
在我看來,代碼風格並不是最優的。沒有必要創建兩個Strings networkSSID和networkPass。您可以簡單地使用ssid或密碼。
建議:拆分創建wifi配置和連接方法。
(1)用於創建WifiConfiguration方法:
private WifiConfiguration createAPConfiguration(String networkSSID, String networkPasskey, String securityMode) {
WifiConfiguration wifiConfiguration = new WifiConfiguration();
wifiConfiguration.SSID = "\"" + networkSSID + "\"";
if (securityMode.equalsIgnoreCase("OPEN")) {
wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
} else if (securityMode.equalsIgnoreCase("WEP")) {
wifiConfiguration.wepKeys[0] = "\"" + networkPasskey + "\"";
wifiConfiguration.wepTxKeyIndex = 0;
wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
} else if (securityMode.equalsIgnoreCase("PSK")) {
wifiConfiguration.preSharedKey = "\"" + networkPasskey + "\"";
wifiConfiguration.hiddenSSID = true;
wifiConfiguration.status = WifiConfiguration.Status.ENABLED;
wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
} else {
Log.i(TAG, "# Unsupported security mode: "+securityMode);
return null;
}
return wifiConfiguration;
}
(2)用於AP方法連接:
public int connectToAP(String networkSSID, String networkPasskey) {
for (ScanResult result : scanResultList) {
if (result.SSID.equals(networkSSID)) {
String securityMode = getScanResultSecurity(result);
WifiConfiguration wifiConfiguration = createAPConfiguration(networkSSID, networkPasskey, securityMode);
int res = wifiManager.addNetwork(wifiConfiguration);
Log.d(TAG, "# addNetwork returned " + res);
boolean b = wifiManager.enableNetwork(res, true);
Log.d(TAG, "# enableNetwork returned " + b);
wifiManager.setWifiEnabled(true);
boolean changeHappen = wifiManager.saveConfiguration();
if (res != -1 && changeHappen) {
Log.d(TAG, "# Change happen");
connectedSsidName = networkSSID;
} else {
Log.d(TAG, "# Change NOT happen");
}
return res;
}
}
return -1;
}
(3)掃描方法100%張貼AnujAroshA。
public String getScanResultSecurity(ScanResult scanResult) {
final String cap = scanResult.capabilities;
final String[] securityModes = { "WEP", "PSK", "EAP" };
for (int i = securityModes.length - 1; i >= 0; i--) {
if (cap.contains(securityModes[i])) {
return securityModes[i];
}
}
return "OPEN";
}
4
100%Works。
通過這種方式,您可以連接到特定的Wif 此代碼爲MainActivity.java
////////////// >>> WIFI連接(WP2)< < < ////////////////
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = String.format("\"%s\"", "Wifi name");
wifiConfig.preSharedKey = String.format("\"%s\"", "Wifi password");
WifiManager wifiManager=(WifiManager)getSystemService(WIFI_SERVICE);
int netId = wifiManager.addNetwork(wifiConfig);
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
,也不要忘了,包括這個代碼添加到您的AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
相關問題
- 1. Android - 以編程方式連接到wifi
- 2. 以編程方式安卓藍牙重複連接
- 3. 獲取可用的wifi連接後以編程方式連接特定的WiFi
- 4. 安卓:如何啓用/停用WiFi或Internet連接編程
- 5. 如何以編程方式確定android是否連接到wifi?
- 6. 以編程方式在ios上連接到WiFi
- 7. iOS:以編程方式確定WiFi連接是2.4Ghz還是5Ghz
- 8. 未能以編程方式連接到Android中的特定WiFi
- 9. 如何以編程方式連接wps到wifi
- 10. 以編程方式在Google Android中連接到WiFi?
- 11. 以編程方式連接到BlackBerry的特定WiFi網絡
- 12. 以編程方式連接或檢測iOS中的某個WiFi?
- 13. 無法以編程方式連接到WIFI網絡
- 14. 以太網連接通過編程方式(安卓)(root權限的設備)
- 15. 安卓:編程開啓WiFi熱點
- 16. 如果在Android Wear應用程序中斷開連接,如何以編程方式「連接」安卓手錶?
- 17. 如何以編程方式掃描,查找並連接到開放式WiFi AP?
- 18. 以編程方式切換WIFI廣播
- 19. 以編程方式重撥PPPOE連接
- 20. 以編程方式連接到TFSPreview
- 21. 以編程方式連接兩個UIButtons
- 22. 通過編程方式連接到Wifi不適用於Android Marshmallowow?
- 23. 以編程方式檢測安裝的卷的連接類型
- 24. 如何使用Apple SDK以編程方式檢測和連接到Wifi網絡
- 25. 如何以編程方式創建wifi連接os設置對話框
- 26. WM和C#:如何以編程方式設置WiFi Ad-Hoc連接?
- 27. 如何以編程方式顯示要連接的Wifi網絡列表?
- 28. 如何判斷iPad是否以編程方式進行WiFi連接?
- 29. 如何以編程方式關閉網絡連接,藍牙和WIFI?
- 30. 如何以編程方式連接到iPhone上的WiFi? (LEAP協議)
此代碼不能編譯, WH ere定義了變量scanResultList和wifiManager? – yoprogramo
您需要獲取WifiManager wifiManager(Context.getSystemService(Context.WIFI_SERVICE))並對其進行調用(wifiMananger.getScanResults())以獲取scanResultList變量。上面的代碼假定您已經有可用網絡的列表。 – wblaschko
代碼中的AppStaticVar是什麼? –