防止 WiFi-direct 在不使用时关闭
Preventing WiFi-direct from switching off when not used
我的目标是 API 17 并使用下面的代码启用 WiFi-direct (P2P)。
一切正常(查找并连接到对等点),但是当不使用时,WiFi-direct 会不时关闭(看起来这取决于 android phone - 我的大约 3-5 分钟) 这也会关闭 WiFi,使我失去互联网连接。
我有一个接收器可以检测 P2P 的状态何时发生变化以将其重新打开,但是即使 P2P 未连接到任何对等点也能始终保持打开状态会很棒。
是否可以继续 ping android phone 本身来做到这一点?还有其他建议吗?
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
// UI update to indicate wifi p2p status.
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
// Wifi Direct mode is enabled
activity.setIsWifiP2pEnabled(true);
} else {
activity.setIsWifiP2pEnabled(false);
activity.resetData();
}
您应该获取 WifiLock。还是你?
来自 WakeLock 的 javadoc 条目:
Normally the Wi-Fi radio may turn off when the user has not used the
device in a while. Acquiring a WifiLock will keep the radio on until
the lock is released.
查看更多关于 WifiLock 的信息:
http://developer.android.com/reference/android/net/wifi/WifiManager.WifiLock.html
另请阅读此问题的答案:Is using WakeLock overkill while using WifiLock on Android?
我的目标是 API 17 并使用下面的代码启用 WiFi-direct (P2P)。 一切正常(查找并连接到对等点),但是当不使用时,WiFi-direct 会不时关闭(看起来这取决于 android phone - 我的大约 3-5 分钟) 这也会关闭 WiFi,使我失去互联网连接。
我有一个接收器可以检测 P2P 的状态何时发生变化以将其重新打开,但是即使 P2P 未连接到任何对等点也能始终保持打开状态会很棒。
是否可以继续 ping android phone 本身来做到这一点?还有其他建议吗?
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
// UI update to indicate wifi p2p status.
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
// Wifi Direct mode is enabled
activity.setIsWifiP2pEnabled(true);
} else {
activity.setIsWifiP2pEnabled(false);
activity.resetData();
}
您应该获取 WifiLock。还是你?
来自 WakeLock 的 javadoc 条目:
Normally the Wi-Fi radio may turn off when the user has not used the device in a while. Acquiring a WifiLock will keep the radio on until the lock is released.
查看更多关于 WifiLock 的信息: http://developer.android.com/reference/android/net/wifi/WifiManager.WifiLock.html
另请阅读此问题的答案:Is using WakeLock overkill while using WifiLock on Android?