Android P2P service discovery callbacks not being called

荒凉一梦 提交于 2019-12-01 11:14:43
Harini Gunabalan

Finding 1:

Only after you attach the both the listeners(Service listener and the txtrecord listener) using setupDiscoverServices(), you need to create a Service Request for these attached listners and initiate the discovery. You approach to manually create a Service Request at any time and trigger discover services using a button , is error prone. There is no guarantee that the listeners are actually attached before calling startServiceDiscovery() using a button.

Solution1: Try to call the method, startServiceDiscovery(), after setting up the DNS SD Response listeners i.e. after mManager.setDnsSdResponseListeners(), instead of calling on the button click Listener.

Solution2: Else, I would suggest, move your

'mManager.addServiceRequest(channel, serviceRequest, new ActionListener() {....}'

part into the 'setupDiscoverServices()' method and try to activate the button(startServiceDiscovery()) in the 'onSuccess()' of the 'mManager.addServiceRequest'. By this way you could achieve a synchronization point between attaching listeners, registering service and starting discovery(manually)

The below link demonstrates a WiFiDirect project wherein the peers connect with each other through the network service discovery for message exchange. This order of the methods calls for network service discovery worked for me. https://github.com/Hariharan-Gandhi/WiFriends/blob/master/app/src/main/java/tud/cnlab/wifriends/WiFriends.java

Finding 2: I also noticed that that service type used in the following line is ".tcp".

serviceInfo = WifiP2pDnsSdServiceInfo.newInstance(SERVICE_NAME, "._tcp", record);

However as per Android developer guide, the second parameter(service type) should be of the format: "_protocol._transportlayer". http://developer.android.com/training/connect-devices-wirelessly/nsd.html. In the demo code here, developer.android.com/training/connect-devices-wirelessly/nsd-wifi-direct.html, they have used "_presence._tcp" where "presence" is a messaging protocol of XMPP. Refer the detailed explanation here, Available service types in WifiP2pDnsSdServiceInfo.newInstance.

I am not sure if it is acceptable to omit the Service name in the SVR record [Anybody with knowledge abt this part kindly update me if I am wrong]

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!