WiFi Direct on Android is not working properly

I am trying to develop an application using wifi direct in android Jelly Bean 4.1.1. If p2p is enabled, I immediately call

mManager.discoverPeers(mChannel, actionListener); 

After that, I return to

 onPeersAvailable(WifiP2pDeviceList) 

I am testing 2 Samsung (Google) Nexus devices, and Wi-Fi Direct is enabled on both. But this callback returns an empty peer list. But, for example, if I click the Search Peers button on the direct wifi interface by default, then the second device starts immediately

WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION

and this inturn call

 onPeersAvailable(WifiP2pDeviceList peers) 

as I request for peers for this event using

 mManager.requestPeers(mChannel, WifiDirectService.this); 

This time I can see my peers. I see this several times.

What could be the reason? Thanks

+5
source share
1 answer

You should use the Discovering peermethod:

 manager.discoverPeers(channel, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { ... } @Override public void onFailure(int reasonCode) { ... } }); 

If the discovery process succeeds and detects peers, the system sends WIFI_P2P_PEERS_CHANGED_ACTION an intention that you can listen on the broadcast receiver to obtain a list of peers. When your application receives the intent WIFI_P2P_PEERS_CHANGED_ACTION, you can request a list of peers detected using requestPeers ().

For a complete example, please check: http://developer.android.com/guide/topics/connectivity/wifip2p.html#discovering

+1
source

Source: https://habr.com/ru/post/1437828/


All Articles