Android - 创建个人 wifi 屏幕
Android - creating personal wifi screen
我正在尝试创建自己的 wifi 屏幕,其中会向用户显示可点击 AP 的列表。问题是,当用户单击 AP 时,设备会断开连接,然后重新连接到同一个 AP 而不是所选的 AP。这是代码:(只有开放网络部分,我正在尝试从安全网络转移到开放网络)
public void onClick(View v) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
for (WifiConfiguration configuredNetwork : configuredNetworks) {
if (currSSID().equals(configuredNetwork.SSID)) { // I took care of the removel of the "" in configuredNetwork.SSID in my code
configuredNetwork .allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiManager.disconnect();
wifiManager.enableNetwork(configuredNetwork.networkId, true);
wifiManager.reconnect();
break;
}
}
I'm trying to move from secured network to an open one
这里的配置之前必须改过,所以最好新建一个:
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + your_net_name + "\"";
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
另外你还需要先检查wifi是否开启:
if(!wifiManager.isWifiEnabled()){
wifiManager.setWifiEnabled(true);
}
您还必须在 reconnect()
:
之后使用 saveConfiguration()
将当前更改保存到 wifiManager
wifiManager.saveConfiguration();
所以,整个路一定是:
public void onClick(View v) {
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + your_net_name + "\"";
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if(!wifiManager.isWifiEnabled()){
wifiManager.setWifiEnabled(true);
}
List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
for (WifiConfiguration configuredNetwork : configuredNetworks) {
if (currSSID().equals(configuredNetwork.SSID)) { // I took care of the removel of the "" in configuredNetwork.SSID in my code
//configuredNetwork .allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); // you don't need it here
wifiManager.disconnect();
wifiManager.enableNetwork(configuredNetwork.networkId, true);
wifiManager.reconnect();
wifiManager.saveConfiguration(); // to save your new changes to the network
break;
}
}
我最终使用 ScanResult
而不是 WifiConfiguration
并创建了一个新的配置 - 与想要的扫描结果进行如下配置:
conf.SSID = "\"" + scanResult.SSID + "\"";
conf.BSSID = scanResult.BSSID;
conf.hiddenSSID = true;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
int id = wifiManager.addNetwork(conf);
wifiManager.disconnect();
wifiManager.enableNetwork(id, true);
wifiManager.reconnect();
wifiManager.saveConfiguration();
我正在尝试创建自己的 wifi 屏幕,其中会向用户显示可点击 AP 的列表。问题是,当用户单击 AP 时,设备会断开连接,然后重新连接到同一个 AP 而不是所选的 AP。这是代码:(只有开放网络部分,我正在尝试从安全网络转移到开放网络)
public void onClick(View v) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
for (WifiConfiguration configuredNetwork : configuredNetworks) {
if (currSSID().equals(configuredNetwork.SSID)) { // I took care of the removel of the "" in configuredNetwork.SSID in my code
configuredNetwork .allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiManager.disconnect();
wifiManager.enableNetwork(configuredNetwork.networkId, true);
wifiManager.reconnect();
break;
}
}
I'm trying to move from secured network to an open one
这里的配置之前必须改过,所以最好新建一个:
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + your_net_name + "\"";
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
另外你还需要先检查wifi是否开启:
if(!wifiManager.isWifiEnabled()){
wifiManager.setWifiEnabled(true);
}
您还必须在 reconnect()
:
saveConfiguration()
将当前更改保存到 wifiManager
wifiManager.saveConfiguration();
所以,整个路一定是:
public void onClick(View v) {
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + your_net_name + "\"";
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if(!wifiManager.isWifiEnabled()){
wifiManager.setWifiEnabled(true);
}
List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
for (WifiConfiguration configuredNetwork : configuredNetworks) {
if (currSSID().equals(configuredNetwork.SSID)) { // I took care of the removel of the "" in configuredNetwork.SSID in my code
//configuredNetwork .allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); // you don't need it here
wifiManager.disconnect();
wifiManager.enableNetwork(configuredNetwork.networkId, true);
wifiManager.reconnect();
wifiManager.saveConfiguration(); // to save your new changes to the network
break;
}
}
我最终使用 ScanResult
而不是 WifiConfiguration
并创建了一个新的配置 - 与想要的扫描结果进行如下配置:
conf.SSID = "\"" + scanResult.SSID + "\"";
conf.BSSID = scanResult.BSSID;
conf.hiddenSSID = true;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
int id = wifiManager.addNetwork(conf);
wifiManager.disconnect();
wifiManager.enableNetwork(id, true);
wifiManager.reconnect();
wifiManager.saveConfiguration();