android - Wifi-Direct 总是在三十分钟后断开连接

标签 android wifi-direct wifip2p

我正在使用 WifiP2pManager.connect() 创建一个 WifiP2pGroup。这工作正常,但该组总是在三十分钟后解散,无论是否正在传输数据。有谁知道为什么会发生这种情况,或者是否可以避免这种情况?

我曾尝试在设备之间连续发送数据,并使用唤醒锁让屏幕始终保持打开状态,但这没有任何区别。我已确保 wifi 设置为不休眠,还获得了 wifilock,并关闭了应用程序和 wifi direct 的电池优化,但这些都无济于事。三十分钟后(或者具体来说,29 米 18 秒 - 它非常一致)我收到 CONNECTION_STATE_CHANGE 广播并且设备不再配对。

这些设备是运行 Android 7 (LineageOS)Nexus 7 和运行 Android 6Samsung Galaxy A5 >.

谢谢!

编辑:我试过在关闭移动数据和未连接 wifi 的情况下运行。该应用程序未被暂停或销毁,并且没有其他应用程序有权更改 wifi 状态。在其中一台设备的 wifi 日志中,我在断开连接时发现了这个事件:

E/DhcpStateMachine: DHCP renew failed on p2p-wlan0-0: Timed out waiting for DHCP Renew to finish
D/WifiP2pService: GroupCreatedState{ what=196613 }
E/WifiP2pService: DHCP failed

在另一台设备上,我在断开连接过程开始时发现了这个:

552-813/system_process V/WifiHAL: event received NL80211_CMD_DEL_STATION
552-846/system_process D/WifiMonitor: Event [IFNAME=p2p-p2p0-11 AP-STA-DISCONNECTED b6:22:8b:b1:df:0f p2p_dev_addr=f2:62:6f:d1:5f:0c]
552-846/system_process D/WifiMonitor: p2p0 cnt=6442 dispatchEvent: AP-STA-DISCONNECTED b6:22:8b:b1:df:0f p2p_dev_addr=f2:62:6f:d1:5f:0c
552-629/system_process D/WifiP2pService: Client list empty, remove non-persistent p2p group

最佳答案

下面看起来 2 个对等体的 DHCP 租约是 30 分钟,更新失败。

E/DhcpStateMachine: DHCP renew failed on p2p-wlan0-0: Timed out waiting for DHCP Renew to finish
D/WifiP2pService: GroupCreatedState{ what=196613 }
E/WifiP2pService: DHCP failed

服务广告商和服务浏览器有 IP 地址,浏览器的 DHCP 分配给广告商。如果 DHCP 更新失败,则连接丢失,设备必须重新连接。似乎在 29 分 18 秒尝试续租,并在 30 分钟的租约时间超时。此时浏览器必须重新连接。如果 DHCP 服务器不记得浏览器的 IP 地址,或者如果框架总是发布一个新的 IP,那么就会发生您描述的情况。希望这会有所帮助。

编辑:这篇文章似乎证实了群组所有者充当 DHCP 服务器:Get peer device's IP address in wifi-direct p2p connection .不过,我不知道如何增加 WiFi-Direct 的租用时间。

编辑:此链接讨论了 Android 上 DHCP 的一些问题:https://www.net.princeton.edu/android/android-stops-renewing-lease-keeps-using-IP-address-11236.html

编辑:https://www.net.princeton.edu/android/android-11236-partial-workaround.html 中包含可能的解决方案, 附在下面。这个想法是使用组所有者设备上的设置。

程序

Depending upon the version of Android on your device, there should be a "Wi-Fi sleep policy" setting, a "Wi-Fi disconnect policy" setting, or a "Keep Wi-Fi on during Sleep" setting. (Your device will have only one of these; the name varies.) It should be available at (only) one of the following locations:

      Settings 
         -> Wireless & networks 
            -> Wi-Fi settings 
               -> Menu (button) 
                  -> Advanced 
                     -> Wi-Fi sleep policy

      Settings
         -> Wireless & networks
            -> Wi-Fi settings
               -> Wi-Fi disconnect policy

      Settings
         -> Wireless & networks
            -> Wi-Fi settings
               -> Menu (button)
                  -> Advanced
                     -> Keep Wi-Fi on during Sleep

If the Wi-Fi settings item is greyed-out, you may need to turn on Wi-Fi before you can select this item.
If your device has a "Wi-Fi sleep policy" or " Wi-Fi disconnect policy", it should offer several choices. These choices available vary among different versions of Android. At least one of the choices should be "After 15 mins" or "When screen turns off". Select that choice.

Otherwise, if your device has a "Keep Wi-Fi on during Sleep" setting, it should offer several choices. These choices available may vary among different versions of Android. At least one of the choices should be "Never". Select that choice.

If you have any software installed on your Android device which modifies the way Android turns Wi-Fi on or off, then reconfigure that software so it no longer controls the device's Wi-Fi interface. Alternatively, disable or remove that software.

An example would be an application designed to keep your device's Wi-Fi interface always turned on. Another would be an application configured to turn on your device's Wi-Fi interface based on certain conditions (for example, location, time of day, or battery charge).

These applications are not the cause of the bugs. However, they can prevent this partial workaround from being effective, because these may be configured to override Android's "Wi-Fi Sleep Policy" (or "Wi-Fi disconnect policy"), causing the Wi-Fi interface to remain connected to the wireless network for an extended period while the Android device is asleep.

Every time you upgrade the Android software/firmware in the future, verify that the setting you selected above (the "Wi-Fi sleep policy", "Wi-Fi disconnect policy", or "Keep Wi-Fi on during Sleep" setting) remains configured as described above. Such upgrades sometimes modify existing settings; you must make a point of checking (and if necessary, changing) this setting immediately after upgrading.

Take care that any software you install in the future does not modify the way Android turns on or off Wi-Fi. 

通过挖掘 p2 的源代码,底线是 p2p 的 dhcp 东西是由 ethernetworkfactory 完成的,绕过 native dhcp 服务器(参见 http://book2s.com/java/src/package/com/android/server/ethernet/ethernetnetworkfactory.html#199b9c81a59238cafe64b4e28b0c71ce ),这是一个有趣的代码 fragment :

/* Called by the NetworkFactory on the handler thread. */
public void onRequestNetwork() {
    // TODO: Handle DHCP renew.

因此看起来 dhcp 租约永远不会在 p2p 框架中续订(见下文)。坦率地说,我必须检查一下;如果属实,这是一个非常重要的错误。如果是这样,那么按照上述帖子每 25 分钟重新连接一次的解决方案可能是唯一的方法。注意:dhcp 更新发生在租约更新时间的 48% 之前运行,先于本地 dhcp 更新(参见 https://android.googlesource.com/platform/frameworks/base/+/c3a2858/core/java/android/net/DhcpStateMachine.java);相关行是

//Do it a bit earlier than half the lease duration time
//to beat the native DHCP client and avoid extra packets
//48% for one hour lease time = 29 minutes

所以在 p2pdirect 中,看起来有一个层避免本地 dhcp 更新,但它本身并不实现。

关于android - Wifi-Direct 总是在三十分钟后断开连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44119808/

相关文章:

java - 如果按下连接按钮,如何创 build 备客户端或发送者

android - 使用 tasks.getByName 找不到名称的任务

java - 如何在android studio中用jsoup获取这个元素

android - 需要从 GCM 迁移到 FCM?

java - Deitel 如何编程Android Cannon Game 匿名内部类警告

android - 在 android 上使用 wifi direct 发现服务后要做什么?

android - 为什么我在使用 WifiP2pManager 时总是 BUSY?

Android WiFi-Direct : discovering, 连接问题。详细讨论

android - 通过 Wifi Direct (p2p) 服务将 Raspberry 与 Android 连接

flutter - 在 Flutter 中使用 Wi-Fi Direct 创建 P2P 连接