After connecting to WiFi programmatically, disconnects a few seconds later

匿名 (未验证) 提交于 2019-12-03 01:18:02

问题:

I am having an issue with switching wifi networks on the users behalf. We have an IoT device that we need to connect to in order to set it up. Using the WifiManager and ConnectivityManager, I can make the connection and even make a REST call to it, but it changes back after about 10 seconds. I don’t understand why. There are a couple of strange log lines from the device that I am sure are related but I don’t know how to fix, mainly:

02-12 15:36:13.441 E/WifiConfigManager: UID 10356 does not have permission to update configuration "REDACTED"NONE 02-12 15:36:13.442 I/WifiStateMachine: connectToUserSelectNetwork Allowing uid 10356 with insufficient permissions to connect=90 ... 02-12 15:36:13.470 E/WifiVendorHal: stopRssiMonitoring(l.2156) failed {.code = ERROR_NOT_AVAILABLE, .description = } 02-12 15:36:13.470 W/WifiConfigManager: Looking up network with invalid networkId -1 ... 02-12 15:36:16.882 D/WifiStateMachine: NETWORK_STATUS_UNWANTED_VALIDATION_FAILED ... 02-12 15:36:17.041 D/WifiPermissionsUtil: Denied: no location permission 

There is a lot about permissions in there, but I think I am asking for them all:

<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> 

And I ask for the RuntimePermission for location since I am testing this on a device with SDK 27. What’s more is that it does let me connect, but just changes it’s mind few seconds later.

回答1:

The issue is caused when you try to scan for networks with WifiManager.startScan() since Android has changed its permission model. You require to check for the runtime permission to access the coarse/fine location and you also have to check if the location provider (GPS) is switched ON. Otherwise you won't get any location updates. In your case, the location updates contain also wifi SSIDs, which you have scanned for.

Also note, that you have to check, if the user has NOT connected to that device previously via system settings, because it is not permitted by an app to connect to user defined access points from within an app. To allow the app to connect again in behalf of the user, the user must delete/forget that network in the system settings. If you don't check that, you will get an supplicant authentication error from the subsystem.

Another notes to IoT Wifi devices: they are mostly "headless" access points, i.e. have no internet. Many Android devices have the issue, that they require a valid captive portal. See this issue: https://android.stackexchange.com/questions/130265/stay-connected-to-specific-wifi-which-has-no-internet

To solve this issue, you must connect with a static IP address. However, Android does not offer an API to set this programmatically. So you must use reflection as stated here: Set static IP and gateway programmatically in Android 6.x (Marshmallow)



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!