应用程序(WiFi连接)在Android 6.0棉花糖上不再工作 [英] Application (WiFi connections) doesn't work anymore on Android 6.0 Marshmallow

查看:146
本文介绍了应用程序(WiFi连接)在Android 6.0棉花糖上不再工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

升级到棉花糖后,我的应用程序停止运行,应该可以更改WiFi连接,但是现在它什么也没做.

My application stopped working once I upgraded to Marshmallow, it was supposed to be able to change the WiFi connection, but now it doesn't do anything at all.

我花了一些时间阅读有关Android 6.0的新权限模型的信息.太棒了,但是旧的应用程序应该可以继续工作...无论如何,我开始尝试实现权限的授予,但是意识到这是一个普通的权限,并且如果它是在android manifest中定义的,则不应对其进行任何权限请求: /p>

I've spent some time reading about the new permission model of the Android 6.0. Well awesome, but old apps should continue working... Anyway, I started trying to implement the granting of permission, but realized that this is a normal permission and there should be done no permission request for it if it's defined in android manifest:

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

该权限一直存在于Android Manifest中,如果我理解正确,它应该可以正常工作,因为它是普通权限".那么为什么它不起作用,有人有解决方案吗?

The permission has always been in Android Manifest, and if I understand correctly it is supposed to work because it's a "Normal permission". So why doesn't it work, does anybody have a solution?

添加与我的案例有关的代码片段:

Adding the code fragment related to my case:

protected void connectWifi() {
    if ((!connectedToAccessPoint(settings.getMainConnectionName()))
            && (accessPointIsAvailable(settings.getMainConnectionName()))) {
        ConnectionUtils.connectToWifi(this,
                settings.getMainConnectionName(),
                settings.getMainConnectionPassword());
        Toast.makeText(this,
                "Connecting to " + settings.getMainConnectionName(),
                Toast.LENGTH_LONG).show();
        handler.postDelayed(sendUpdatesToUI,
                DelayConstants.BASIC_REQUEST_SENT);
        handler.postDelayed(sendUpdatesToUI,
                DelayConstants.CHANGE_CONNECTION);
    }
}

这里是连接的技术部分:

And here the technical part for the connection:

public static void connectToWifi(Context context, String ssid, String password) {
     WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
     // setup a wifi configuration
      WifiConfiguration wc = new WifiConfiguration();
      wc.SSID = "\"" + ssid + "\"";
      wc.preSharedKey = "\""+ password + "\"";
      wc.status = WifiConfiguration.Status.ENABLED;
      wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
      wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
      wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
      wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
      wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
      wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    // connect to and enable the connection
     int netId = wifiManager.addNetwork(wc);
     wifiManager.enableNetwork(netId, true);
     wifiManager.setWifiEnabled(true);
}

如前所述,在AndroidManifest中具有CHANGE_WIFI_STATE权限,因为该应用程序在没有Android 6.0的设备上运行

In AndroidManifest as mentioned before there's that CHANGE_WIFI_STATE permission, which was there since the app was running on devices not having Android 6.0

推荐答案

所以我最终在人们评论和阅读Web的一些帮助下弄明白了.因此,毕竟这是一个权限问题.

So I eventually figured it out with some help of the people commenting and reading on the web. So it was a permissions problem after all.

为了与WifiManager一起正常工作,从Android 6.0扫描连接时,它需要访问您的位置,因此无论是精细位置还是粗略位置,我在清单文件中都添加了以下内容:

It seems in order to work well with WifiManager when scanning the Connections from Android 6.0 it needs to access your location, so that is either the fine location or the coarse location, I added the following to my Manifest file:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

我在连接之前先检查代码连接是否可用,这就是它永远无法工作的原因,由于缺少权限,它总是从网络扫描返回0结果.因此,除非您在Android中启用了位置设置,否则需要以一种标准的方式来完成实施,以请求ACCESS_COARSE_LOCATION权限,此后将不起作用.如果我每次都需要打开位置信息,它实际上会使我的应用程序变得毫无用处...

I was checking in my code whether the connection was available before connecting it, and that's why it would never work, it always returned 0 results from the network scan because the permission is missing. So in a standard way an implementation needs to be done to request the ACCESS_COARSE_LOCATION permission, afterwards it would not work, UNLESS you turned on your location setting in Android. It actually makes my app pretty useless if I need to turn on location every time...

最后,我做了一个变通方法,没有检查访问点是否可用,而只是执行了一条try-catch语句来尝试连接到它.这很丑陋,但这是新Android的唯一方法.

At the end I did a work around without checking whether the access point is available and just doing a try-catch statement to try to connect to it. It's ugly but it's the only way in new Android.

我确实喜欢新的权限模型,但显然Google在某些方面做得非常糟糕.为什么现在需要打开位置才能获取WiFi扫描结果????没有任何意义,因为没有它,它就可以在Android 6.0之前运行.我确实知道有关位置权限的信息,但是实际上必须打开位置才能扫描WiFi上可用的内容是完全错误的...

I do like the new permission model, but apparently Google has done a really bad job implementing it at some points. Why would you need now to turn on the location to be able to get the WiFi scan results???? Doesn't make any sense because it worked before Android 6.0 without that. I do understand that about the location permission, but actually having to turn on location to be able to scan what's available on WiFi is just plain wrong...

这篇关于应用程序(WiFi连接)在Android 6.0棉花糖上不再工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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