android - 如何在 Android 上连接到特定的 wifi SSID?
问题描述
我想使用此代码通过 SSID 连接到特定的 wifi 。
它没有连接到字符串中的 wifi 的 SSID。我是 Android 新手,如果有人告诉我这个问题会很好。(当然,不修复代码)!
public class DiscoverActTest extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String networkSSID = "DIRECT";
String networkPass = "123456789";
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
conf.wepKeys[0] = "\"" + networkPass + "\"";
conf.wepTxKeyIndex = 0;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
conf.preSharedKey = "\""+ networkPass +"\"";
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
WifiManager wifiManager = (WifiManager)getApplicationContext().getSystemService(WIFI_SERVICE);
wifiManager.addNetwork(conf);
wifiManager.setWifiEnabled(true);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
break;
}
}
}
}
解决方案
改变
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
至
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
推荐阅读
- python - 一段时间后python子进程变得空闲
- java - java Maven添加依赖
- python - 根据其他列的值创建新列的更好方法
- python - 如何在 Python 中以正确的方式对字典列表进行排序和分组
- spring - 映射实体中的重复列
- c++ - Visual Studio 找不到 assert.h
- javascript - 付费的 Chrome 扩展程序是否需要请求身份权限才能提供免费试用?
- c# - itext 7 - RTL 语言
- vmware-clarity - 带有垂直导航的树视图
- java - Java Streams API中的“forEach”是无序的原因是什么?