0

A bit of a background of the question. I'm making a multiplayer game that uses Network service discovery to broadcast a service. Though I'm neither begginner in java nor in android, I AM a begginner in technologies that use Wi-Fi to connect devices. So, in my app I have an activity where a user offered to enter a room or to create one. When user clicks "Create a room", I first check whether there is an already registered service. If one exists, I unregister it and register a new one, also I write the result of these actions to the debug Log. Here is the code of a method, which is called inside "Create Room" button's onClick() method:

public void registerService(){
        if(mConnection.getLocalPort() > -1) {
            mNsdHelper.tearDown();
            mNsdHelper.registerService(mConnection.getLocalPort());
            mNsdHelper.discoverServices();
            Log.d(Keys.MAFIA_TAG, "Service Registered from registerService()");
        } else {
            Log.d(Keys.MAFIA_TAG, "ServerSocket isn't bound.");
        }    
    }

the tearDown() methods code:

public void tearDown() {
        if (mRegistrationListener != null) {
            try {
                mNsdManager.unregisterService(mRegistrationListener);
            } finally {
            }
            mRegistrationListener = null;
        }
    }

The problem is: as I can see from logs, the service gets successfully unregistered, and right after it system tells me that the same service is discovered so now I have two services: newly created and the old one. Here are the logs:

User presses "Create Room":

05-06 20:30:10.362    3685-3685/com.example.yarkov.mafiamultiplayer D/mafia main﹕ DeviceSony  //the name of the service
05-06 20:30:10.363    3685-3685/com.example.yarkov.mafiamultiplayer D/NsdHelper﹕ Service registration started
05-06 20:30:10.366    3685-3685/com.example.yarkov.mafiamultiplayer D/mafia main﹕ Service Registered from registerService()
05-06 20:30:10.378    3685-4701/com.example.yarkov.mafiamultiplayer D/NsdHelper﹕ Service discovery started
05-06 20:30:10.433    3685-3685/com.example.yarkov.mafiamultiplayer I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@2bdc45ef time:32538709
05-06 20:30:11.180    3685-4701/com.example.yarkov.mafiamultiplayer D/NsdHelper﹕ Service registered: DeviceSony
05-06 20:30:11.431    3685-4701/com.example.yarkov.mafiamultiplayer D/NsdHelper﹕ Same machine: DeviceSony //a service has been found, but it's a local one

User presses the Back button:

05-06 20:30:35.440    3685-4701/com.example.yarkov.mafiamultiplayer D/NsdHelper﹕ Service unregistered: DeviceSony
05-06 20:30:35.563    3685-3685/com.example.yarkov.mafiamultiplayer I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@35979eff time:32563839
05-06 20:30:36.649    3685-4701/com.example.yarkov.mafiamultiplayer E/NsdHelper﹕ service lostname: DeviceSony, type: _http._tcp., host: null, port: 0

User presses "Create Room" again:

05-06 20:31:16.867    3685-3685/com.example.yarkov.mafiamultiplayer I/Timeline﹕ Timeline: Activity_launch_request id:com.example.yarkov.mafiamultiplayer time:32605143
05-06 20:31:16.935    3685-3685/com.example.yarkov.mafiamultiplayer D/mafia main﹕ DeviceSony
05-06 20:31:16.935    3685-3685/com.example.yarkov.mafiamultiplayer D/NsdHelper﹕ Service registration started
05-06 20:31:16.935    3685-3685/com.example.yarkov.mafiamultiplayer D/mafia main﹕ Service Registered from registerService()
05-06 20:31:16.940    3685-5996/com.example.yarkov.mafiamultiplayer D/NsdHelper﹕ Service discovery started
05-06 20:31:17.016    3685-3722/com.example.yarkov.mafiamultiplayer D/OpenGLRenderer﹕ endAllStagingAnimators on 0xb83c6600 (RippleDrawable) with handle 0xb83ae018
05-06 20:31:17.028    3685-3685/com.example.yarkov.mafiamultiplayer I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@4d088df time:32605304
05-06 20:31:17.624    3685-5996/com.example.yarkov.mafiamultiplayer D/NsdHelper﹕ Service registered: DeviceSony
05-06 20:31:17.872    3685-5996/com.example.yarkov.mafiamultiplayer D/NsdHelper﹕ Same machine: DeviceSony
05-06 20:31:17.875    3685-4701/com.example.yarkov.mafiamultiplayer D/NsdHelper﹕ Same machine: DeviceSony

We can see that 2 services were found instead of one, which means that the first service was not unregistered properly.

Thank you all in advance!

Sergey Maslov
  • 758
  • 1
  • 8
  • 22

1 Answers1

-1

The answer is as simple as I'm fed up with this wi-fi connection: you just have to call NsdManager.stopServiceDiscovery() in (in my case) tearDown() method.

But here another question appears: I've noticed, that when system discovers a local service, it's port is 0, when localport is somewhat like 3456. Why?

Sergey Maslov
  • 758
  • 1
  • 8
  • 22