Android / Java WiFi direct peer-to-peer list

Hey,

I'm trying to get an Android device (this is a Nexus 7, although I can't imagine that this is too important) to communicate with the Raspberry Pi through the wonders of WiFi Direct. Presumably this is possible, and it seemed six hours ago, as a better solution than a descent along a server-client route, but I ran into problems

The Android developer site is good in that it has two guides:

http://developer.android.com/training/connect-devices-wirelessly/wifi-direct.html http://developer.android.com/guide/topics/connectivity/wifip2p.html

This is great because I don't have any Java experience. But the problem with ... is actually quite a lot on the fact that the elements in the code that they consider plausible (i.e. where to put things) are often missing.

I carefully followed both guides, but I ran into a problem: the intention of WIFI_P2P_PEERS_CHANGED_ACTION is never broadcast, so when I look for peers, it starts searching, says that everything is in order, but does not give me a list of results ... which I assume , must

I struggled with this for most of the day with a pleasant case of hay fever to make life even sweeter.

CODE:

Activity:

package com.example.bingotest; import java.util.ArrayList; import java.util.List; import android.net.wifi.p2p.WifiP2pDevice; import android.net.wifi.p2p.WifiP2pDeviceList; import android.net.wifi.p2p.WifiP2pManager; import android.net.wifi.p2p.WifiP2pManager.Channel; import android.net.wifi.p2p.WifiP2pManager.PeerListListener; import android.os.Build; import android.os.Bundle; import android.annotation.TargetApi; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.ActivityInfo; import android.util.Log; import android.view.Menu; public class BingoActivity extends Activity { private BingoView _view; private IntentFilter _intentFilter = new IntentFilter(); private BroadcastReceiver _broadcastReceiver = null; private WifiP2pManager _manager; private Channel _channel; private List _peers = new ArrayList(); private PeerListListener _peerListListener; //------------------------------------------------------------------------------- @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); _view = new BingoView(this); setContentView(_view); _intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION); _intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION); _intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION); _intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION); _manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE); _channel = _manager.initialize(this, getMainLooper(), null); //_broadcastReceiver = new WiFiDirectBroadcastReceiver(_manager, _channel, this); //registerReceiver(_broadcastReceiver, _intentFilter); _peerListListener = new PeerListListener() { @Override public void onPeersAvailable(WifiP2pDeviceList peerList) { Log.d("wifi", "here"); // Out with the old, in with the new. _peers.clear(); _peers.addAll(peerList.getDeviceList()); if (_peers.size() == 0) { Log.d("wifi", "No devices found"); return; } } }; } //------------------------------------------------------------------------------- @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.bingo, menu); return true; } //------------------------------------------------------------------------------- @Override public void onResume() { super.onResume(); _broadcastReceiver = new WiFiDirectBroadcastReceiver(_manager, _channel, this, _peerListListener); registerReceiver(_broadcastReceiver, _intentFilter); } //------------------------------------------------------------------------------- @Override public void onPause() { super.onPause(); unregisterReceiver(_broadcastReceiver); } } 

WiFiDirectBroadcastReceiver Class:

 package com.example.bingotest; import java.util.ArrayList; import java.util.List; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.wifi.p2p.WifiP2pManager; import android.net.wifi.p2p.WifiP2pManager.Channel; import android.net.wifi.p2p.WifiP2pManager.PeerListListener; import android.util.Log; public class WiFiDirectBroadcastReceiver extends BroadcastReceiver { private WifiP2pManager _manager; private Channel _channel; private BingoActivity _activityRef; private List _peers = new ArrayList(); PeerListListener _peerListListener; //------------------------------------------------------------------------------- public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel, BingoActivity activity, PeerListListener peerListListener) { super(); _manager = manager; _channel = channel; _activityRef = activity; _peerListListener = peerListListener; _manager.discoverPeers(_channel, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { Log.d("wifi", "onsuccess"); } @Override public void onFailure(int reasonCode) { Log.d("wifi", "onfailure"); } }); } //------------------------------------------------------------------------------- @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); Log.d("wifi", "receive: " + intent.getAction()); if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) { // Check to see if Wi-Fi is enabled and notify appropriate activity int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1); if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) { Log.d("wifi", "WIFI DIRECT ON"); } else { Log.d("wifi", "WIFI DIRECT OFF"); } } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) { Log.d("wifi", "Peers changed"); if (_manager != null) { _manager.requestPeers(_channel, _peerListListener); } } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) { // Respond to new connection or disconnections } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) { // Respond to this device wifi state changing } } //------------------------------------------------------------------------------- } 

(apologies for the potentially messy and wasteful code - I experimented with trying to get the results)

Help would be very necessary. I assume that peerListListener is in the wrong place. My knowledge of Android is pretty minimal.

Thanks in advance.

+4
source share
4 answers

I see that you made the initial discovery when creating WiFiDirectBroadcastReceiver

and then in Activity.onResume () you first create WiFiDirectBroadcastReceiver and then registerReceiver () .

 @Override public void onResume() { super.onResume(); _broadcastReceiver = new WiFiDirectBroadcastReceiver(_manager, _channel, this, _peerListListener); registerReceiver(_broadcastReceiver, _intentFilter); } 

The problem you are not seeing the answer has returned, probably due to consistency. Move DiscoverPeers () to Activity.onResume () to fix the problem,

+1
source

You do not need a call to DiscoverPeers :

To start searching for nearby devices using Wi-Fi Direct, call discoverPeers ().

The specific logic of the application, when you want this initial detection to happen, will determine where in your code occurs. In the API examples for SDK 17, this happens when a menu item is selected from a menu. This can be easily transferred to the onResume method (this will be triggered every time the action resumes, or in the client for a button on the screen.

For further troubleshooting, I would recommend keeping the WiFiDirectBroadcastReceiver class inside the Activity class (if you don't need to reuse the code) for ease of use with callbacks - you can access the external methods of the class directly from the inner class and make it a little easier to troubleshoot.

0
source

Try changing this:

 _manager.requestPeers(_channel, _peerListListener); 

:

 _manager.requestPeers(_channel, _activityRef._peerListListener); 
0
source

Google provides an example of Wifi Direct from October 2011: https://android.googlesource.com/platform/development/+/master/samples/WiFiDirectDemo

0
source

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


All Articles