WiFi P2P 导致设备 WiFi 自动连接功能不起作用?
WiFi P2P causes device WiFi auto-connect feature not to work?
我遇到一个问题,我的设备在范围内时停止自动连接到 WiFi 网络。我的应用程序有一个在后台运行的服务(即使应用程序被终止)每分钟注册和取消注册服务以使用 WiFi P2P 重复检测附近的设备。
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
Log.d(TAG, "WiFi P2P Enabled");
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Log.d(TAG, "Discover Peers Successful");
}
@Override
public void onFailure(int i) {
Log.d(TAG, "Discover Peers Unsuccessful");
}
});
} else {
Log.d(TAG, "WiFi P2P Disabled");
}
// Check to see if Wi-Fi is enabled and notify appropriate activity
} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
Log.d(TAG, "Peers detected");
if (manager != null) {
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
Log.d(TAG, "Peers Requested");
manager.requestPeers(channel, peerListListener);
}
// Call WifiP2pManager.requestPeers() to get a list of current peers
} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
Log.d(TAG, "Connection Changed");
// Respond to new connection or disconnections
} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
Log.d(TAG, "WiFi P2P State Changed");
// Respond to this device's wifi state changing
}
}
};
WifiP2pManager.PeerListListener peerListListener = new WifiP2pManager.PeerListListener() {
@Override
public void onPeersAvailable(WifiP2pDeviceList wifiP2pDeviceList) {
Log.d(TAG, "Peers available");
String deviceEncrypt = "";
if (!wifiP2pDeviceList.getDeviceList().equals(peers)) {
peers.clear();
peers.addAll(wifiP2pDeviceList.getDeviceList());
deviceNameArray = new String[wifiP2pDeviceList.getDeviceList().size()];
deviceArray = new WifiP2pDevice[wifiP2pDeviceList.getDeviceList().size()];
int index = 0;
for (WifiP2pDevice device : wifiP2pDeviceList.getDeviceList()) {
deviceNameArray[index] = device.deviceName;
deviceArray[index] = device;
index++;
try {
deviceEncrypt = AESUtils.encrypt(device.deviceAddress);
Log.d(TAG, "Encrypted MAC Address discovered: " + deviceEncrypt);
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
Log.d(TAG, "No peers found");
}
}
};
我意识到这会阻止设备在范围内时自动连接到已知的 WiFi 网络。知道如何解决这个问题吗?下面的代码每分钟注册和取消注册 broadcastReceiver 以实现一致的对等检测(这是 onStartCommand 函数中的 运行)。
private void refreshTime() {
handler.postDelayed(runnable = new Runnable() {
@Override
public void run() {
Log.d(TAG, "Refreshed");
registerReceiver(broadcastReceiver, intentFilter);
unregisterReceiver(broadcastReceiver);
registerReceiver(broadcastReceiver, intentFilter);
handler.postDelayed(runnable, delay);
}
}, delay);
}
回答我自己的问题。所以 manager.discoverPeers 是阻止 Android 从 auto-connecting 连接到已知 Wifi 网络的原因。我通过将 refreshTime() 拆分为两个不同的函数来解决此问题。
private void refreshTime() {
refreshHandler.postDelayed(refreshRunnable = new Runnable() {
@Override
public void run() {
Log.d(TAG, "Refreshed Registered");
registerReceiver(broadcastReceiver, intentFilter);
refreshHandler.postDelayed(refreshRunnable, refreshDelay);
}
}, refreshDelay);
}
private void stopDiscovery() {
peerHandler.postDelayed(peerRunnable = new Runnable() {
@Override
public void run() {
manager.stopPeerDiscovery(channel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Log.d(TAG, "Peer Discovery Stopped");
try {
unregisterReceiver(broadcastReceiver);
Log.d(TAG, "Refreshed Unregistered");
} catch (IllegalArgumentException e) {
Log.e(TAG, e.toString());
}
}
@Override
public void onFailure(int i) {
Log.d(TAG, "Peer Discovery Failed to Stop");
}
});
peerHandler.postDelayed(peerRunnable, peerDelay);
}
}, peerDelay);
}
stopDiscovery() 调用 manager.stopPeerDiscovery() 并且 onSuccess 它将取消注册 broadcastReceiver。
两者都是 运行 在两个不同的时间间隔。 refreshTime() 每 1 分钟运行一次,而 stopDiscovery() 每 2 分钟运行一次。这会使 discoverPeers() 停止大约一分钟,并允许 OS 到 auto-connect 到已知的 Wifi 网络。
再次。如果有人对此有更优雅的方法,请与我分享。谢谢
我遇到一个问题,我的设备在范围内时停止自动连接到 WiFi 网络。我的应用程序有一个在后台运行的服务(即使应用程序被终止)每分钟注册和取消注册服务以使用 WiFi P2P 重复检测附近的设备。
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
Log.d(TAG, "WiFi P2P Enabled");
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Log.d(TAG, "Discover Peers Successful");
}
@Override
public void onFailure(int i) {
Log.d(TAG, "Discover Peers Unsuccessful");
}
});
} else {
Log.d(TAG, "WiFi P2P Disabled");
}
// Check to see if Wi-Fi is enabled and notify appropriate activity
} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
Log.d(TAG, "Peers detected");
if (manager != null) {
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
Log.d(TAG, "Peers Requested");
manager.requestPeers(channel, peerListListener);
}
// Call WifiP2pManager.requestPeers() to get a list of current peers
} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
Log.d(TAG, "Connection Changed");
// Respond to new connection or disconnections
} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
Log.d(TAG, "WiFi P2P State Changed");
// Respond to this device's wifi state changing
}
}
};
WifiP2pManager.PeerListListener peerListListener = new WifiP2pManager.PeerListListener() {
@Override
public void onPeersAvailable(WifiP2pDeviceList wifiP2pDeviceList) {
Log.d(TAG, "Peers available");
String deviceEncrypt = "";
if (!wifiP2pDeviceList.getDeviceList().equals(peers)) {
peers.clear();
peers.addAll(wifiP2pDeviceList.getDeviceList());
deviceNameArray = new String[wifiP2pDeviceList.getDeviceList().size()];
deviceArray = new WifiP2pDevice[wifiP2pDeviceList.getDeviceList().size()];
int index = 0;
for (WifiP2pDevice device : wifiP2pDeviceList.getDeviceList()) {
deviceNameArray[index] = device.deviceName;
deviceArray[index] = device;
index++;
try {
deviceEncrypt = AESUtils.encrypt(device.deviceAddress);
Log.d(TAG, "Encrypted MAC Address discovered: " + deviceEncrypt);
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
Log.d(TAG, "No peers found");
}
}
};
我意识到这会阻止设备在范围内时自动连接到已知的 WiFi 网络。知道如何解决这个问题吗?下面的代码每分钟注册和取消注册 broadcastReceiver 以实现一致的对等检测(这是 onStartCommand 函数中的 运行)。
private void refreshTime() {
handler.postDelayed(runnable = new Runnable() {
@Override
public void run() {
Log.d(TAG, "Refreshed");
registerReceiver(broadcastReceiver, intentFilter);
unregisterReceiver(broadcastReceiver);
registerReceiver(broadcastReceiver, intentFilter);
handler.postDelayed(runnable, delay);
}
}, delay);
}
回答我自己的问题。所以 manager.discoverPeers 是阻止 Android 从 auto-connecting 连接到已知 Wifi 网络的原因。我通过将 refreshTime() 拆分为两个不同的函数来解决此问题。
private void refreshTime() {
refreshHandler.postDelayed(refreshRunnable = new Runnable() {
@Override
public void run() {
Log.d(TAG, "Refreshed Registered");
registerReceiver(broadcastReceiver, intentFilter);
refreshHandler.postDelayed(refreshRunnable, refreshDelay);
}
}, refreshDelay);
}
private void stopDiscovery() {
peerHandler.postDelayed(peerRunnable = new Runnable() {
@Override
public void run() {
manager.stopPeerDiscovery(channel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Log.d(TAG, "Peer Discovery Stopped");
try {
unregisterReceiver(broadcastReceiver);
Log.d(TAG, "Refreshed Unregistered");
} catch (IllegalArgumentException e) {
Log.e(TAG, e.toString());
}
}
@Override
public void onFailure(int i) {
Log.d(TAG, "Peer Discovery Failed to Stop");
}
});
peerHandler.postDelayed(peerRunnable, peerDelay);
}
}, peerDelay);
}
stopDiscovery() 调用 manager.stopPeerDiscovery() 并且 onSuccess 它将取消注册 broadcastReceiver。
两者都是 运行 在两个不同的时间间隔。 refreshTime() 每 1 分钟运行一次,而 stopDiscovery() 每 2 分钟运行一次。这会使 discoverPeers() 停止大约一分钟,并允许 OS 到 auto-connect 到已知的 Wifi 网络。
再次。如果有人对此有更优雅的方法,请与我分享。谢谢