【问题标题】:Wifi-Direct always disconnects after thirty minutesWifi-Direct 总是在三十分钟后断开连接
【发布时间】:2017-10-22 12:30:29
【问题描述】:

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

我曾尝试在设备之间连续发送数据,并使用唤醒锁始终保持屏幕开启,但这并没有什么区别。我确保 wifi 设置为不休眠,还获得了 wifilock,并关闭了应用程序和 wifi direct 的电池优化,但这些也无济于事。三十分钟后(或者更确切地说,29m 18s - 非常一致)我收到了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

【问题讨论】:

  • 您的应用是否在 30 分钟内处于前台?
  • 是的,应用程序在前台,实际上它一启动就运行前台服务,一直运行到应用程序关闭。
  • 在此期间前台服务或活动都没有被回收或暂停?那就是......既不调用onDestroy也不调用onPause?它只是自己建立连接?
  • 我必须帮助诊断问题的两个建议... 1. 如果可能,我建议关闭移动数据并断开与您的 wifi 网络的连接。可能是使用您的互联网连接的其他东西正在重置适配器。 2. 在开发者设置中,我会尝试打开“详细 wifi 日志记录”。也许那里有线索。
  • 连接是点对点的,它不通过路由器。一台设备充当组所有者,并且(我认为)运行自己的 DHCP 服务器...

标签: android wifi-direct wifip2p


【解决方案1】:

您可以开发一项服务。然后以编程方式每 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】:

以下看起来 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 完成的,绕过了本地 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.

所以看来 dhcp 租约永远不会在 p2p 框架中更新(见下文)。坦率地说,我必须检查一下;如果为真,这是一个不平凡的错误。如果是真的,那么按照上述帖子每 25 分钟重新连接一次的解决方案可能是唯一的方法。注意: dhcp 更新发生在本机 dhcp 更新之前运行的租约更新时间的 48%(请参阅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 更新,但自己没有实现它。

【讨论】:

  • 嗨,谢谢,我尝试了这个建议,但没有奏效。
  • 我发现 DHCP 租用时间是 1 小时。客户应该在租用时间的中途请求续订,这就是为什么要在 30 分钟后尝试续订的原因。但我不知道为什么更新失败。但是我正在运行 LineageOS 每晚构建,这可能是问题的根源。
  • 那么可能是因为连接不良而导致续订超时。它看起来不像重试,所以也许上面的建议(例如通过设置增加连接超时)会起作用。您在日志中看到任何重试吗?您是否看到需要重试的初始连接,表明连接不良?我知道我的机器人需要很长时间才能使用 WiFiDirect 进行初始连接。
  • 哎哟。 stuff.mit.edu/afs/sipb/project/android/docs/training/… 表示在他们的示例中重试是手动的。也许在你的 connect() 中你可以自动重试几次?
  • 你能剪切和粘贴你的 connect() 方法的定义吗?
【解决方案3】:

这似乎是我正在使用的 LineageOS 构建中的一个错误,因为它仅在平板电脑是组所有者时发生。当手机为群主时,不会发生断线。

【讨论】:

    【解决方案4】:

    您应该指定要创建持久的组,例如在 Linux 上,您可以执行以下操作:

    sudo wpa_cli p2p_group_add persistent
    

    【讨论】:

      猜你喜欢
      • 2018-01-16
      • 2018-09-20
      • 1970-01-01
      • 1970-01-01
      • 2015-05-03
      • 1970-01-01
      • 2021-08-10
      • 2018-09-11
      • 1970-01-01
      相关资源
      最近更新 更多