I am trying to discover WiFi Direct peer to peer android devices but peers are discovered only when both phones are running WiFi Direct discovery.
What I have Understood so far is, they will see each other only when they are both scanning for WiFi direct connections at the same time. This is because the way WiFi Direct works is that when phones are scanning for WiFi Direct connections, they will negotiate with the other peers for the role of Access Point or Slave device. Hence both need to call discoverPeers() to become discoverable themselves and find nearby devices.
What I want in my application is that only one device starts the scanning process and all nearby devices supporting wifi direct should be listed. So how can this be achieved using wifi Direct? Are there any other alternatives to this.
Thanks in Advance
There is no way to achieve your goal using WiFiDirect. You need to turn on WiFiDirect (programmatically or manual) on all devices which are going to be connected.
But there is a way to reduce discovering process effort. You can use a service discovering instead of regular p2p connection.
It allows doing p2p discovering only on one device (client). A second device (server) is just waiting for a connection invite.
In one of our application, we had an issue: when a device does p2p discovering, a bandwidth of the network is extremely decreased what led to disconnect already connected devices.
Using a service discovering really helped us. You can use the code as an example.
The only alternative I was able to find is navigating to Wi-Fi Direct settings. On Android 6.0 device it can be achieved like this: Wi-Fi -> Settings (3 dots menu in the right top corner) -> Wi-Fi Direct. Most probably it starts the discovery process itself...
What I deducted is that the Wi-Fi Direct is not a standalone feature as e.g. Bluetooth. It is a part of Wi-Fi so it can't be turned on/off separately. The Wi-Fi P2P Network creation starts with Group Owner negotiation which can be performed only between Wi-Fi Direct capable devices. As the P2P Group is formed, it is available for Wi-Fi Legacy devices also (it works like a standard ad-hoc network, visible by all Wi-Fi capable devices).
So in order to create P2P Network (Group) you have to negotiate with other P2P devices which is only possible if you turn on Wi-Fi Direct.
As you have noticed that both the device should be on the discovery mode. I am currently working on a project on WIFI-DIRECT , and yes it is very unreliable . You will also find it hard to discover after a connection and/or disconnecting . So what I have done is ,when the user is turning ON the device WIFI i am using a thread (background in a infinite loop) to run the discovery after every 8 seconds and updating the UI. This worked for me and user doesn't have to search continuously.
public class DiscoveryUpdater implements Runnable {
@Override
public void run() {
while(true) {
mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Log.d("DiscoveryUpdater", "Successful in adding Discovery Request");
mManager.requestPeers(mChannel,peerListListener);
}
@Override
public void onFailure(int reasonCode) {
Log.d("DiscoveryUpdater", "Failed in adding Discovery Request "+reasonCode);
}
});
try {
Thread.sleep(8000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Hope it helps ( Remember to update your list , I have not shown that)
来源:https://stackoverflow.com/questions/48248292/alternative-to-discovering-peers-with-wifi-direct-as-it-requires-both-phones-run