以编程方式连接到WiFi后,几秒钟后断开连接 [英] After connecting to WiFi programmatically, disconnects a few seconds later

查看:274
本文介绍了以编程方式连接到WiFi后,几秒钟后断开连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代表用户切换wifi网络时遇到问题.我们需要连接到IoT设备才能进行设置.使用 WifiManager ConnectivityManager ,我可以建立连接,甚至可以对其进行REST调用,但是大约10秒钟后它就会变回原来的状态.我不明白为什么.我确信设备上有几条奇怪的日志行,我确信它们相互关联,但我不知道如何解决,主要是:

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" />

由于我正在使用SDK 27的设备上进行测试,因此我要求提供RuntimePermission的位置.此外,确实可以让我连接,但几秒钟后便改变了主意./p>

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.

推荐答案

由于Android更改了权限模型,因此尝试使用 WifiManager.startScan()扫描网络时,会导致此问题.您需要检查运行时权限才能访问粗略/精细位置,还必须检查位置提供程序(GPS)是否已打开.否则,您将不会获得任何位置更新.就您而言,位置更新还包含您扫描过的wifi SSID.

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.

IoT Wifi设备的另一个注意事项:它们大多是无头"访问点,即没有互联网.许多Android设备都有一个问题,即它们需要有效的强制门户.请参见以下问题: https://android.stackexchange.com/questions/130265/保持连接到特定的wifi-wifi,没有互联网

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

要解决此问题,必须使用静态IP地址进行连接.但是,Android不提供以编程方式设置此功能的API.因此,您必须按照此处所述使用反射:在Android 6.x(棉花糖)中以编程方式设置静态IP和网关

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)

这篇关于以编程方式连接到WiFi后,几秒钟后断开连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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