Android 6.0 wif的变动

最近项目在nexus5机子上出现崩溃,看下系统,Android 6.0,所以查看了下Google Android 6.0 的更新。立马定位到WiFi和netWoring改变这部分。Google 官方原文档是这样的。

Wi-Fi and Networking Changes

This release introduces the following behavior changes to the Wi-Fi and networking APIs.

  • Your apps can now change the state of WifiConfiguration objects only if you created these objects. You are not permitted to modify or delete WifiConfiguration objects created by the user or by other apps.
  • Previously, if an app forced the device to connect to a specific Wi-Fi network by using enableNetwork() with thedisableAllOthers=true setting, the device disconnected from other networks such as cellular data. In This release, the device no longer disconnects from such other networks. If your app’s targetSdkVersion is “20” or lower, it is pinned to the selected Wi-Fi network. If your app’s targetSdkVersion is “21” or higher, use the multinetwork APIs (such as openConnection()bindSocket(), and the new bindProcessToNetwork() method) to ensure that its network traffic is sent on the selected network.

(英语学渣的理解)

第一条是,你的app现在不能改变wificonfiguration对象的状态,只能创建它。你不允许去修改和删除用户创建或者其他app创建的wificonfiguration对象。

第二条是,以前你的app可以强制你的设备去连接指定的WiFi网络通过调用enableNetwork()方法。合适使用disableAllothers = true,来使设备断开其他网络,如蜂窝数据。但是在最新的6.0中设备不再断开其他网络,如果你的app targetSDKVersiond是20或者更低,它将自动的选择WiFi 网络,如果的app targetSDKVersiond是21或者更高使用多网络API(multinetwork api Anroid 5.0里可以查阅)去确保网络数据被发送在已经选择的网络上。

第二条没有验证,但第一是引起项目崩溃的原因所在,因为Android 6.0 中WiFiManager addNetwork(WifiConfiguration config),add 同一ssid时会返回-1,这个时候你再调用enableNetWork

将这个-1NetWorkId传进去就会系统崩溃,总连不上WiFi。

我解决办法是,addNetWork之后遍历所有的WifiManager.getConfiguredNetworks(),取出相同的

ssid的wificonfiguraion.

然后在使用WifiManager.enableNetwork(wificonfiguraion.neteworkId, true);

猜你喜欢

转载自blog.csdn.net/bobxie520/article/details/114478081