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

Wifi-Direct always disconnects after thirty minutes

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

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

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

谢谢!

编辑:我已尝试 运行 关闭移动数据且未连接 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

你可以开发一个服务。然后以编程方式每 25 分钟重新连接一次。

public class WifiReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {     
        ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
        NetworkInfo netInfo = conMan.getActiveNetworkInfo();
        if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI) 
            Log.d("WifiReceiver", "Have Wifi Connection");
        else
            Log.d("WifiReceiver", "Don't have Wifi Connection");    
    }   
};

为了访问活动网络信息,您需要将以下使用权限添加到您的 AndroidManifest.xml:

以及以下意图接收器(或者您可以通过编程方式添加它...)

<receiver android:name=".WifiReceiver">
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>

[参考:] [Wifi Connect-Disconnect Listener1

这样就可以解决你的问题了。 :)

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

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

编辑:这个 link 讨论了 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 完成的,绕过了本机 dhcp 服务器(参见 http://book2s.com/java/src/package/com/android/server/ethernet/ethernetnetworkfactory.html#199b9c81a59238cafe64b4e28b0c71ce),这是一个有趣的代码片段:

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

因此,在 p2p 框架 中,dhcp 租约似乎永远不会续订(见下文)。坦率地说,我必须检查一下;如果属实,这是一个非常重要的错误。如果是这样,那么按照上面post每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 更新,但它本身没有实现。

这似乎是我正在使用的 LineageOS 版本中的一个错误,因为只有当平板电脑是组所有者时才会发生这种情况。当phone是群主时,不会发生断线。

您应该指定要创建持久的组,例如在 Linux 上,您可以这样做:

sudo wpa_cli p2p_group_add persistent