I'm developing an application, which uses wifi-direct to create groups of maximum 4 devices ( 1 host + 3 peers). I read manuals of wifi-direct from developer.android.com, found this wonderful answer: https://stackoverflow.com/a/31641302/3106249 - and still, I have couple of issues which I don't know how to handle.
First problem step by step:
Registering local service and creating group on host device.
Map<String, String> record = new HashMap<String, String>(); record.put(TXTRECORD_PROP_AVAILABLE, "visible"); record.put(Core.SESSION_KEY, Core.SESSION_KEY_VALUE); record.put(Core.SERVICE_INSTANCE_KEY, SERVICE_INSTANCE); localService = WifiP2pDnsSdServiceInfo.newInstance(SERVICE_INSTANCE, Core.SERVICE_REG_TYPE, record); manager.clearLocalServices(channel, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { Log.d(TAG, "clearLocalServices success"); manager.addLocalService(channel, localService, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { Log.d(TAG, "addLocalService success"); manager.createGroup(channel, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { Log.d(TAG, "createGroup success"); } @Override public void onFailure(int reason) { Log.d(TAG, "createGroup fail: " + reason); } }); } @Override public void onFailure(int reason) { Log.d(TAG, "addLocalService fail: " + reason); } }); } @Override public void onFailure(int reason) { Log.d(TAG, "clearLocalServices fail: " + reason); } });
Discovering required peed by host device with 10 seconds interval.
manager.removeServiceRequest(channel, serviceRequest, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { Log.d(TAG, "discovering, removeServiceRequest success"); manager.stopPeerDiscovery(channel, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { Log.d(TAG, "discovering, stopPeerDiscovery success"); manager.discoverPeers(channel, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { Log.d(TAG, "discovering, discoverPeers success"); manager.addServiceRequest(channel, serviceRequest, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { Log.d(TAG, "discovering, addServiceRequest success"); manager.discoverServices(channel, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { //Log.d(TAG, "discoverServices success"); } @Override public void onFailure(int reason) { Log.d(TAG, "discoverServices fail: " + reason); } }); } @Override public void onFailure(int reason) { Log.d(TAG, "addServiceRequest fail: " + reason); } }); } @Override public void onFailure(int reason) { Log.d(TAG, "discoverPeers fail: " + reason); } }); } @Override public void onFailure(int reason) { Log.d(TAG, "stopPeerDiscovery fail: " + reason); } }); } @Override public void onFailure(int reason) { Log.d(TAG, "clearServiceRequests fail: " + reason); } });
Sending connection invite (by calling
WiFiP2pManager#connect()
)WifiP2pConfig config = new WifiP2pConfig(); config.deviceAddress = device.deviceAddress; config.wps.setup = WpsInfo.PBC; manager.connect(channel, config, new WifiP2pManager.ActionListener() { @Override public void onSuccess() { Log.d(TAG, "manger.onSuccess with " + device.deviceName); } @Override public void onFailure(int errorCode) { Log.d(TAG, "Failed connecting to service " + errorCode); } });
- Connection prompt dialog appears on peer. Accepting connection.
- Connection prompt dialog NEVER appears on host device.
WiFiP2pManager#connect()
, it returns in ActionListenter's onFailure
method, with error code 2. Then, I call connect
again after 5-10 seconds, and it returns in onSuccess
method. Despite this, on connected device does not appear connection prompt dialog, which means that no connection invite is received I suppose.
These two issues are the main problems that makes application totally unusable. Can anybody explain to me what I'm doing wrong or how to handle these issues?
UPD
Attaching logs for small discovery session.
Host log (nexus 7): http://pastebin.com/ycfqRE4m
Peer log (nexus 10): http://pastebin.com/5kbp6e7A