I'm trying to implement a simple android application that broadcasts a WifiP2p bonjour service on one device and have a second device discover and connect to it. I've pretty much followed the tutorial here.
I have a valid Channel
object, a DnsSdServiceResponseListener
, and a DnsSdTxtRecordListener
, and set them via this call:
mManager.setDnsSdResponseListeners(channel, servListener, txtListener);
As of right now both listeners just spit out some debugging info to keep it real simple.
The problem is that my DnsSdServiceResponseListener
is never called, but the DnsSdTxtRecordListener
does get called and all the arguments passed in look legit. How can one be called but not the other?
I am testing using two actual devices, both running android 4.2.2.
Thanks!
Wi-Fi Direct is similar in concept to “ad-hoc” Wi-Fi mode. However, unlike an ad-hoc Wi-Fi connection, Wi-Fi Direct includes an easier way to automatically discover nearby devices and connect to them.
Network service discovery (NSD) gives your app access to services that other devices provide on a local network. Devices that support NSD include printers, webcams, HTTPS servers, and other mobile devices.
To check the status of Wi-Fi Direct on your device, go into Settings -> Network & internet -> Wi-Fi -> Wi-Fi preferences and then tap Wi-Fi Direct. Your smartphone will start scanning for devices that you can connect to. Unlike with Bluetooth, there is no button or anything that you need to tap to turn Wi-Fi Direct on.
Wi-Fi Direct (also known as peer-to-peer or P2P) allows your application to quickly find and interact with nearby devices, at a range beyond the capabilities of Bluetooth. The Wi-Fi Direct (P2P) APIs allow applications to connect to nearby devices without needing to connect to a network or hotspot.
Finally figured it out.. kind of. When getting an instance of a service request, I was specifying the service instance name and type to match the values used when creating the service...
Registering the service:
mServiceInfo = WifiP2pDnsSdServiceInfo.newInstance(Consts.SERVICE_INSTANCE, Consts.SERVICE_REG_TYPE, record);
Creating the service request:
mServiceRequest = WifiP2pDnsSdServiceRequest.newInstance(Consts.SERVICE_INSTANCE, Consts.SERVICE_REG_TYPE);
I removed the arguments to the newInstance method when getting the service request...
mServiceRequest = WifiP2pDnsSdServiceRequest.newInstance();
and both listeners are being called now. It seems like my first method would filter out other services being broadcasted nearby, which is why I originally opted for that overload.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With