我有一个运行 Android 的嵌入式模块,并且操作系统在大约 5 分钟不活动后断开 WiFi。如果我随后运行任何使用网络连接的应用程序,WiFi 将保持关闭状态,并且图标不会显示在状态栏中。我必须进入设置和 WiFi 设置才能恢复 WiFi。
我有一个应用程序必须通过 WiFi 连接定期发送数据。如果我在 WiFi 处于活动状态的情况下启动程序并每 2-3 分钟发送一次数据,WiFi 将保持活动状态并且永远不会断开连接。如果我让 WiFi 断开连接然后尝试发送,它永远不会恢复。在代码中,调用:
wifi.isWifiEnabled()
始终返回 true,但图标仍然隐藏,表示 WiFi 未连接。我试图从代码中调用 reconnect() 作为带有按钮的测试,但 WiFi 从未出现。
从终端调试端口,我可以键入 IFCONFIG WLAN0 UP 并且 WiFi 连接到 AP。
我可以拨打任何电话会导致 WiFi 重新连接吗?除了检测 TCP/IP 连接失败之外,我有什么方法可以实际检测到 WiFi 已断开连接?
为了响应下面的请求,我添加了以下代码片段和 LOGCAT 输出
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiLock = wifi.createWifiLock(WifiManager.WIFI_MODE_FULL, "Mini210-Lock");
if(!wifiLock.isHeld())
{
wifi.setWifiEnabled(true);
wifi.reconnect();
// wifi.reassociate(); // Also try this!!
wifiLock.acquire(); // Keep system alive
Log.i("Alarm System", "WiFi Lock Acquired");
}
运行上述代码时,Logcat 显示如下。
09-10 22:43:25.273: I/Alarm System(611): WiFi Lock Acquired
09-10 22:43:25.417: I/ActivityManager(91): Display axon.alarm.system/.AlarmSystemActivity: +15s198ms (total +4h23m59s645ms)
09-10 22:43:31.636: W/wpa_supplicant(133): Failed to initiate AP scan.
09-10 22:43:41.636: W/wpa_supplicant(133): Failed to initiate AP scan.
当我在调试监控端口输入ifconfig wlan0 up时,logcat中出现如下(部分重复输出已被删除)
09-10 22:56:12.726: I/wpa_supplicant(133): CTRL-EVENT-SCAN-RESULTS Ready
09-10 22:56:12.726: I/wpa_supplicant(133): Trying to associate with 00:22:75:32:a9:47 (SSID='kpk_mobile' freq=2412 MHz)
09-10 22:56:20.976: D/dhcpcd(623): checking 192.168.1.123 is available on attached networks
09-10 22:56:20.976: D/dhcpcd(623): sending ARP probe (1 of 3), next in 1.73 seconds
09-10 22:56:22.710: D/dhcpcd(623): sending ARP probe (2 of 3), next in 1.00 seconds
09-10 22:56:23.714: D/dhcpcd(623): sending ARP probe (3 of 3), next in 2.00 seconds
09-10 22:56:25.718: D/dhcpcd(623): leased 192.168.1.123 for 86400 seconds
09-10 22:56:25.718: D/dhcpcd(623): adding IP address 192.168.1.123/24
09-10 22:56:25.718: D/dhcpcd(623): adding route to 0.0.0.0/0 via 192.168.1.1
09-10 22:56:25.730: D/dhcpcd(623): executing `/system/etc/dhcpcd/dhcpcd-run-hooks', reason BOUND
09-10 22:56:25.773: D/dhcpcd(623): forking to background
09-10 22:56:25.804: V/WifiStateTracker(91): DhcpHandler: DHCP request succeeded
09-10 22:56:25.804: V/WifiStateTracker(91): IP configuration: ipaddr 192.168.1.123 gateway 192.168.1.1 netmask 255.255.255.0 dns1 192.168.1.1 dns2 0.0.0.0 DHCP server 192.168.1.1 lease 86400 seconds
09-10 22:56:25.816: I/TelephonyRegistry(91): notifyDataConnection: state=0 isDataConnectivityPossible=false reason=dataDisabled interfaceName=null networkType=0
09-10 22:56:25.828: D/WifiWatchdogService(91): (android.server.ServerThread) kpk_mobile (00:22:75:32:a9:47) does not require the watchdog
09-10 22:56:25.843: V/FTPDroid(356): EventsReceiver: NETWORK_STATE_CHANGED_ACTION (exists: true, connected: true)
09-10 22:56:25.863: D/Tethering(91): MasterInitialState.processMessage what=3
09-10 22:56:25.902: I/GTalkService(278): [ServiceAutoStarter] --- connectivity changed
09-10 22:56:25.902: I/GTalkService(278): [ServiceAutoStarter] --- start GTalk service ---
09-10 22:56:27.789: D/dhcpcd(647): sending ARP announce (1 of 2), next in 2.00 seconds
09-10 22:56:29.789: D/dhcpcd(647): sending ARP announce (2 of 2)
09-10 22:56:29.789: D/dhcpcd(647): renew in 43196 seconds