I am trying to get discovery to work on Android Wear. I'm using the 
standard NdsManager sample code but just the bit to discover another 
service - I am not publishing my own service from the device.


The code works fine on a Samsung Galaxy S3 running 4.3 - but on a Sony 
SmartWatch 3 running 5.1.1 it fails with "Internal Error".


So here is the discovery code - the same code is running on both devices:


public class ServerDiscovery {
Context mContext;NsdManager mNsdManager;NsdManager.DiscoveryListener 
mDiscoveryListener;NsdManager.ResolveListener mResolveListener;
public static final String SERVICE_TYPE = "_protocol._tcp.";public static final 
String TAG = "ServerDiscovery";public String mServiceName = "Client";
NsdServiceInfo mService;
public ServerDiscovery(Context context) {
    mContext = context;}
public void start() {
    initializeResolveListener();
    initializeDiscoveryListener();
    mNsdManager = (NsdManager) mContext.getSystemService(Context.NSD_SERVICE);
    mNsdManager.discoverServices(SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, 
mDiscoveryListener);}
public void initializeDiscoveryListener() {

    // Instantiate a new DiscoveryListener
    mDiscoveryListener = new NsdManager.DiscoveryListener() {

        //  Called as soon as service discovery begins.
        @Override
        public void onDiscoveryStarted(String regType) {
            Log.d(TAG, "Service discovery started");
        }

        @Override
        public void onServiceFound(NsdServiceInfo service) {
            // A service was found!  Do something with it.
            Log.d(TAG, "Service discovery success" + service);
            if (!service.getServiceType().equals(SERVICE_TYPE)) {
                // Service type is the string containing the protocol and
                // transport layer for this service.
                Log.d(TAG, "Unknown Service Type: " + service.getServiceType());
            } else {
                mNsdManager.resolveService(service, mResolveListener);
            }
        }

        @Override
        public void onServiceLost(NsdServiceInfo service) {
            // When the network service is no longer available.
            // Internal bookkeeping code goes here.
            Log.e(TAG, "service lost" + service);
        }

        @Override
        public void onDiscoveryStopped(String serviceType) {
            Log.i(TAG, "Discovery stopped: " + serviceType);
        }

        @Override
        public void onStartDiscoveryFailed(String serviceType, int errorCode) {
            Log.e(TAG, "Discovery failed: Error code:" + errorCode);
           // mNsdManager.stopServiceDiscovery(this);
        }

        @Override
        public void onStopDiscoveryFailed(String serviceType, int errorCode) {
            Log.e(TAG, "Discovery failed: Error code:" + errorCode);
            mNsdManager.stopServiceDiscovery(this);
        }
    };}
public void initializeResolveListener() {
    mResolveListener = new NsdManager.ResolveListener() {
        @Override
        public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
            Log.e(TAG, "Resolve failed" + errorCode);
        }
        @Override
        public void onServiceResolved(NsdServiceInfo serviceInfo) {
            Log.e(TAG, "Resolve Succeeded. " + serviceInfo);
            if (serviceInfo.getServiceName().equals(mServiceName)) {
                Log.d(TAG, "Same IP.");
                return;
            }
            mService = serviceInfo;
        }
    };}

and it is called from the main activity using:


public class MyActivity extends AppCompatActivity {
ServerDiscovery mServerDiscovery;
@Overrideprotected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ...some ui setup code...

    mServerDiscovery = new ServerDiscovery(getApplicationContext());
    mServerDiscovery.start();}

There is a server running on the same network publishing a service using 
_protocol._tcp. This gets dicovered and resolved to as you'd expect:


Service discovery startedService discovery successname: Simon’s MacBooktype: 
_protocol._tcp.host: nullport: 0txtRecord: nullResolve Succeeded. name: 
Simon’s\032MacBooktype: ._protocol._tcphost: /192.168.192.114port: 
3000txtRecord: null

But when I call the same code from the Android Wear activity:


public class MyActivity extends WearableActivity {

    private static final SimpleDateFormat AMBIENT_DATE_FORMAT =
        new SimpleDateFormat("HH:mm", Locale.US);

    ServerDiscovery mServerDiscovery;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_s);
        setAmbientEnabled();

        ... UI Code ...

        mServerDiscovery = new ServerDiscovery(getApplicationContext());
        mServerDiscovery.start();
    }

All I get back is


E/ServerDiscovery: Discovery failed: Error code:0

Error code 0 apparently means Internal Error which does not help much.


The phone the watch is paired to is off, and the watch is connected to the 
WiFi network and in fact in other tests I can open a socket to the server 
fine - so I think the networking side of things is all working - it is just 
NdsManager that is not working for some reason.


Anyone got any idea why this is not working?


Thanks, Simon.

-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
To post to this group, send email to android-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/c834a59e-c7f3-46a8-a29d-2bd6aafddeb1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to