Android,自动连接到无法访问互联网的wifi网络 [英] Android, automatically connecting to wifi networks that have no internet access

查看:783
本文介绍了Android,自动连接到无法访问互联网的wifi网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调用函数

I'm calling the function WifiManager.addNetwork(WifiConfiguration) to try and add an adhoc wifi network to the device's wifi configurations. But on Android M this function returns -1, I'm assuming because it doesn't have internet access. It works fine on most other devices. Below is code snippet I'm using.

WifiConfiguration wifiConfiguration = new WifiConfiguration(); wifiConfiguration.SSID = '\"' + ssid + '\"'; wifiConfiguration.hiddenSSID = false; wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); wifiManager.addNetwork(wifiConfiguration);

WifiConfiguration wifiConfiguration = new WifiConfiguration(); wifiConfiguration.SSID = '\"' + ssid + '\"'; wifiConfiguration.hiddenSSID = false; wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); wifiManager.addNetwork(wifiConfiguration);

有什么方法可以绕过Internet连接检查并强制添加网络?

Any way to get around the internet connectivity check and force the network to be added?

推荐答案

之后,您需要启用网络:

You need to enable the network after that :

List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
    if(i.SSID != null && i.SSID.equals("\"" + ssid + "\"")) {
         wifiManager.disconnect();
         wifiManager.enableNetwork(i.networkId, true);
         wifiManager.reconnect();               
    enter code here
         break;
    }           
 }

这篇关于Android,自动连接到无法访问互联网的wifi网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆