3

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

3 Answers3

3

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.

Oleg Sokolov
  • 1,134
  • 1
  • 12
  • 19
  • Running a background service performing discoverpeers can make it happen with wifip2p. Will the service discovery the same concept or better than running background service performing discoverpeers?Thanks for your answer. – Sarang S. Chaturvedi Jan 26 '18 at 17:18
  • The service discovery is a part of the 2p2 concept. You still need a background service to perform discovering. – Oleg Sokolov Jan 26 '18 at 20:22
  • The difference is - you don't discover all peers nearby. You discover a specific service. And the service doesn't discover any peers it just wait for connections from peers. – Oleg Sokolov Jan 26 '18 at 20:27
1

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.

Jakub Licznerski
  • 1,008
  • 1
  • 17
  • 33
  • Opening the Wi Fi Direct settings actually initiates the discovery process. But I want onl one device to identify the other without other doing nothing. Is it possible with something else? – Sarang S. Chaturvedi Jan 17 '18 at 04:40
  • 1
    As I've said, without initiating the discovery process your Wi-Fi Direct feature is off. It must be on for devices to be able to find each other before group forming. Think of it like if scanning was a simple WiFi network scan for WiFi P2P networks and the scan itself will create such a network per device. Also what may help you understand is local WiFi IP scan. You are not able to find devices outside the network even if their WiFi is on. – Jakub Licznerski Jan 17 '18 at 09:45
  • Thanks for the reply and answer, but my problem is still not solved. Does the same thing happen during service discovery,https://developer.android.com/training/connect-devices-wirelessly/nsd-wifi-direct.html , as dicoverpeers is called here also, so is it the same thing in service discovery also? – Sarang S. Chaturvedi Jan 18 '18 at 02:27
  • "The first lesson in this class, Using Network Service Discovery, showed you how to discover services that are connected to a local network." So NSD is for local network only, but it should discover also already created P2P Groups. – Jakub Licznerski Jan 18 '18 at 02:44
  • Then I have to run a background service which runs dicoverpeers function indefinitely. Will it affect the battery too much, is it impractical? – Sarang S. Chaturvedi Jan 18 '18 at 03:10
  • The discovery process is rather costly to your battery time as you perform two operations at once (search and listen). However it could be less energy consuming than "big data" transfer over long distance as it implies higher transmission power. Take into consideration, that the `discoverPeers` triggers discovery for a hardcoded 30s time span. You will have to listen to appropriate actions (`WIFI_STATE_CHANGED`) in order to perform discovery continually. Repating this process will probably trigger some Android warnings or errors or won't allow discovery at all (not sure about it). – Jakub Licznerski Jan 18 '18 at 03:22
  • Why won't you allow clients to decide whether they want to be discoverable or not ? – Jakub Licznerski Jan 18 '18 at 03:23
  • because clients need to know only when device want to connect and not just discover, bcz the other client only has to act as the passive agent for this app which only transfer data. – Sarang S. Chaturvedi Jan 18 '18 at 03:46
  • I don't quite understand the concept, but seems like technology more suitable for your use case is Bluetooth Low Energy. Unless you are not transferring data over very long (in BT terms) distance – Jakub Licznerski Jan 18 '18 at 03:50
0

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)

Onic Team
  • 1,620
  • 5
  • 26
  • 37
Jiraiya
  • 76
  • 1
  • 4