Android WiFi connection programmatically(以编程方式连接 Android WiFi)
本文介绍了以编程方式连接 Android WiFi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您知道如何在我的 Android 应用程序中通过发送密码建立 wifi 连接吗?
Do you have any idea how to establish a wifi connection with sending password in my android application?
推荐答案
将 SSID 及其密码传递给以下方法.
Pass the SSID and it's password to the following method.
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 文件中添加必要的权限.
Don't forget to add necessary permission in the Manifest file.
这篇关于以编程方式连接 Android WiFi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:以编程方式连接 Android WiFi
基础教程推荐
猜你喜欢
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01