Android 5.0 Lollipop和4.4 KitKat忽略了我的WiFi网络,enableNetwork()无效 [英] Android 5.0 Lollipop and 4.4 KitKat ignores my WiFi network, enableNetwork() is useless

查看:104
本文介绍了Android 5.0 Lollipop和4.4 KitKat忽略了我的WiFi网络,enableNetwork()无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序直接连接到充当访问点(无法访问互联网)的硬件设备.

我无法连接,因为Android 5.0会自动切换到有效的互联网连接,因此,如果我具有数据连接(3G,4G,...)或预先配置的网络,则无法连接到我的设备,因为它忽略WiFi.

I can't connect because Android 5.0 automaticcaly switch to a valid internet connection, so if I have data connection (3G, 4G, ...) or a pre-configured network I can't connect to my device because it ignores the WiFi.

那么如何强制Android使用我以编程方式激活的网络?

So how can I force Android to use the network I activated programatically?

我只是在使用:

wifiManager.enableNetwork(id, true))

其中id是我要连接的设备的网络. true参数没有用.

where id is the network of my device I want to connect to. The true parameter is useless.

使用ConnectivityManager.requestNetwork()的建议解决方案无效.

The suggested solution that use ConnectivityManager.requestNetwork() has no effect.

推荐答案

我找到了一种解决方法,可以在Lollipop上启用所需的网络:

I found a workaround to enable the desidered network on Lollipop:

WifiConfiguration在Lollipop中启用网络

现在,这是调用wifiManager.enableNetwork(id, true)之后的代码:

Now this is my code after calling wifiManager.enableNetwork(id, true):

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    List<WifiConfiguration> networks = wm.getConfiguredNetworks();
    Iterator<WifiConfiguration> iterator = networks.iterator();
    while (iterator.hasNext()) {
        WifiConfiguration wifiConfig = iterator.next();
        if (wifiConfig.SSID.replace("\"", "").equals(wc.SSID.replace("\"", "")))
            wm.enableNetwork(wifiConfig.networkId, true);
        else
            wm.disableNetwork(wifiConfig.networkId);
    }
    wm.reconnect();
}

也许秘密在于打给reconnect()的电话,我目前不知道.

maybe the secret is the call to reconnect(), I don't know at this time.

更新 不幸的是,只有在执行代码之前有效的WiFi连接处于活动状态时,此解决方法才有效.如果仅通过3G进行连接,则无法使用.

UPDATE Unfortunately, this workaround only works if a valid WiFi connection is active before executing the code. Does not works if you're connect by 3G only.

更新2015年1月19日

此代码实际上在Android 5/6.0.x上对我有效:

This code actually works for me on Android 5/6.0.x:

//bind to current thread
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkRequest.Builder request = new NetworkRequest.Builder();
    request.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
    connManager.registerNetworkCallback(request.build(), new ConnectivityManager.NetworkCallback() {
        @Override
        public void onAvailable(Network network) {
            ConnectivityManager.setProcessDefaultNetwork(network);
            //...
        }
    });
}

这篇关于Android 5.0 Lollipop和4.4 KitKat忽略了我的WiFi网络,enableNetwork()无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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