Background: Device used: Samsung Galaxy Note 10+, Samsung Galaxy S8
- I'm developing an Android application that uses WiFi-direct to transfer photos.
- In order to do so, I first establish BLE connection between two devices, so that they can exchange macAddress data through BLE characteristics.
- Upon Bluetooth service is discovered in "OnServiceDiscovered" callback method, I call "discoverPeers" method.
- When "discoverPeers()" succeeds, WifiP2PManager calls "requestPeers(channel, PeerListListener)"
- Strangely, despite many wifi devices being present, the wifi device list is empty, always.
Here is the code
public void discoverWifiPeers() {
manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Log.e("INFO", "Discover Peers succeeded");
manager.requestPeers(channel, myPeerListListener);
}
@Override
public void onFailure(int reasonCode) {
Log.e("Error", "Discover Peers failed");
}
});
}
private WifiP2pManager.PeerListListener myPeerListListener = new WifiP2pManager.PeerListListener() {
@Override
public void onPeersAvailable(WifiP2pDeviceList wifiP2pDeviceList) {
for (WifiP2pDevice device : wifiP2pDeviceList.getDeviceList())
{
if(!wifiDeviceList.contains(device))
wifiDeviceList.add(device);
}
Log.i(CommonConstants.INFO, "onPeersAvailable() - devicelist =>" + wifiDeviceList.size());
}
};
Measures I had taken:
- I searched lots of blogs and StackOverflow questions for this, and so far all of them pointed coarse location permission. I added the code below.
private void requestCoarseLocation(){
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_COARSE_LOCATION)) {
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
CommonConstants.REQUEST_COARSE_LOCATION
);
}
}
}
}
- It gave Wifi device list for the very first time I added the COARSE_PERMISSION, but after that one time it resumed showing empty list for the WiFi list.
How can I always guarantee to have the wifi device list of devices in proximity?