Network Service Discovery Android tutorial error: Service lost, Phone turns OFF… here is a solution to the problem.
Network Service Discovery Android tutorial error: Service lost, Phone turns OFF
I’m having trouble trying to follow developer.android.com’s web tutorial.
( http://developer.android.com/training/connect-devices-wirelessly/nsd.html )
I get an error and sometimes my phone turns off.
Here’s my code:
package com.example.networking;
import java.io.IOException;
import java.net.ServerSocket;
import javax.sound.sampled.Port;
import android.net.nsd.NsdManager;
import android.net.nsd.NsdManager.DiscoveryListener;
import android.net.nsd.NsdManager.RegistrationListener;
import android.net.nsd.NsdManager.ResolveListener;
import android.net.nsd.NsdServiceInfo;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
@SuppressLint("NewApi")
public class MainActivity extends Activity {
RegistrationListener mRegistrationListener;
DiscoveryListener mDiscoveryListener;
String mServiceName;
NsdServiceInfo mServiceInfo;
ServerSocket mServerSocket;
int mLocalPort;
NsdManager mNsdManager;
final String TAG = "---Networking";
final String SERVICE_TYPE = "_http._tcp.";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Initialize a server socket on the next available port.
try {
mServerSocket = new ServerSocket(0);
} catch (IOException e) {
TODO Auto-generated catch block
e.printStackTrace();
}
Store the chosen port.
mLocalPort = mServerSocket.getLocalPort();
registerService(mLocalPort);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void registerService(int port) {
Create the NsdServiceInfo object, and populate it.
mServiceInfo = new NsdServiceInfo();
The name is subject to change based on conflicts
with other services advertised on the same network.
mServiceInfo.setServiceName("NsdChat");
mServiceInfo.setServiceType("_http._tcp.");
mServiceInfo.setPort(port);
mNsdManager = (NsdManager) getApplicationContext().getSystemService(Context.NSD_SERVICE);
initializeRegistrationListener();
initializeDiscoveryListener();
mNsdManager.registerService(
mServiceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);
mNsdManager.discoverServices(
SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
}
public void initializeRegistrationListener() {
mRegistrationListener = new NsdManager.RegistrationListener() {
@Override
public void onServiceRegistered(NsdServiceInfo NsdServiceInfo) {
Save the service name. Android may have changed it in order to
resolve a conflict, so update the name you initially requested
with the name Android actually used.
mServiceName = NsdServiceInfo.getServiceName();
Log.d(TAG , "Service name: " + mServiceName);
Log.d(TAG , "Port number: " + mLocalPort);
}
@Override
public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
Registration failed! Put debugging code here to determine why.
Log.d(TAG , "Registration Failed! Error code: " + errorCode);
}
@Override
public void onServiceUnregistered(NsdServiceInfo arg0) {
Service has been unregistered. This only happens when you call
NsdManager.unregisterService() and pass in this listener.
Log.d(TAG , "Service unregistered.");
}
@Override
public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
Unregistration failed. Put debugging code here to determine why.
Log.d(TAG , "Unregistration failed!");
}
};
}
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 if (service.getServiceName().equals(mServiceName)) {
The name of the service tells the user what they'd be
connecting to. It could be "Bob's Chat App".
Log.d(TAG, "Same machine: " + mServiceName);
} else if (service.getServiceName().contains("NsdChat")){
mNsdManager.resolveService(service, new ResolveListener() {
@Override
public void onServiceResolved(NsdServiceInfo serviceInfo) {
TODO Auto-generated method stub
Log.d(TAG, "Resolving service...");
}
@Override
public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
TODO Auto-generated method stub
Log.d(TAG, "Service resolve failed!");
}
});
}
}
@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);
}
};
}
}
The problem is visible on the attached picture (I’m not sure how I should call it). I get the error repeatedly.
Can someone explain what I’m doing wrong or why this error appears?
Should I have different registration and discovery ports? (Different NsdManager? )
Solution
I think your logs are good. There are successful discoveries and successful solutions, so go ahead and resolve the results.