Rom*_*iri 6 android android-wifi android-6.0-marshmallow
我正在编写一个无需连接互联网即可连接到热点的应用程序。似乎在某些具有Android Marshmallow的Nexus设备上,当您连接到没有互联网请求的网络时,系统会“断开” wifi连接,并且所有请求都不会发送到热点连接。
几秒钟后,会弹出一条通知,询问您是否要保持连接状态。当答案为“是”时,请求将成功发送到热点,并且您可以获得响应。
我遇到了以下线程:https : //android.stackexchange.com/questions/131132/force-marshmallow-to-keep-a-wi-fi-without-internet-access
但是,我正在寻找一种编程方式(针对没有根目录的设备)来解决此问题。我目前正在使用以下代码连接到Wifi网络:
public void connectTo(String capabilities, String password, String ssid) {
WifiManager mWifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + ssid + "\"";
if (capabilities.contains("WPA2")) {
conf.preSharedKey = "\"" + password + "\"";
} else if (capabilities.contains("WPA")) {
conf.preSharedKey = "\"" + password + "\"";
} else if (capabilities.contains("WEP")) {
conf.wepKeys[0] = "\"" + password + "\"";
conf.wepTxKeyIndex = 0;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
} else {
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
}
mWifiManager.addNetwork(conf);
List<WifiConfiguration> list = mWifiManager.getConfiguredNetworks();
if (list == null) {
return;
}
for (WifiConfiguration i : list) {
if (i.SSID != null && i.SSID.equals("\"" + ssid + "\"")) {
mWifiManager.disconnect();
mWifiManager.enableNetwork(i.networkId, true);
mWifiManager.reconnect();
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
477 次 |
最近记录: |