我正在使用WifiP2pGroup
创建WifiP2pManager.connect()
。这样可以正常工作,但无论是否传输数据,该组在30分钟后总是解散。有谁知道为什么会发生这种情况,或者是否可以预防?
我尝试在设备之间连续发送数据,并通过唤醒锁保持屏幕整个时间,但没有任何区别。我已经确定wifi设置为不睡觉,并且还获得了一个wifilock,并关闭了应用程序和wifi直接的电池优化,但这些也没有帮助。三十分钟后(或具体而言,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
答案 0 :(得分:3)
以下内容类似于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),这是一个有趣的代码片段:< / p>
/* 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续订,但它本身并没有实现它。
答案 1 :(得分: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 Listener 1
因此你可以解决你的问题。 :)
答案 2 :(得分:0)
这似乎是我正在使用的LineageOS构建中的一个错误,因为它只发生在平板电脑是群组所有者时。当手机是群组所有者时,不会发生断开连接。
答案 3 :(得分:0)
您应指定要创建的组具有持久性,例如,在Linux上,您将执行以下操作:
sudo wpa_cli p2p_group_add persistent