Android P2P service discovery callbacks not being called

后端 未结 1 1061
萌比男神i
萌比男神i 2021-01-15 06:47

I\'m fairly new to Android and I\'m working on an application that will exchange data over WIFI direct and I would like to use DNS Service Discovery instead of pure P2P disc

1条回答
  •  北恋
    北恋 (楼主)
    2021-01-15 07:19

    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]

    0 讨论(0)
提交回复
热议问题