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;
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.