I'm facing a problem here, until version 6.0 works fine now from 6.0 onwards it does not work what I do not understand is that it was permission, but the error is as if the listener was already in use by NsdManager. >
java.lang.IllegalArgumentException: listener already in use
at android.net.nsd.NsdManager.registerService(NsdManager.java:485)
at br.com.shootinghouse.sc.ServerActivity.registerService(ServerActivity.java:76)
at br.com.shootinghouse.sc.ServerActivity.onResume(ServerActivity.java:50)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1269)
at android.app.Activity.performResume(Activity.java:6783)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3406)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3469)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2732)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Code
private Manager manager;
private LiteListener listener;
private String SERVICE_NAME = "Server device";
private String SERVICE_TYPE = "_http._tcp.";
private int SERVICE_Port = 5432;
private NsdManager mNsdManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNsdManager = (NsdManager) getSystemService(getApplicationContext().NSD_SERVICE);
registerService(SERVICE_Port);
try {
manager = new Manager(new AndroidContext(getApplicationContext()), Manager.DEFAULT_OPTIONS);
} catch (Throwable e) {
e.printStackTrace();
}
startListener();
URL url = null;
try {
url = new URL("http://" + getLocalIpAddress() + ":" + SERVICE_Port + "/banco144");
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("Listening on :: " + url.toString() + " "
+ SERVICE_NAME + " " +
SERVICE_TYPE);
}
@Override
protected void onResume() {
super.onResume();
if (mNsdManager != null) {
registerService(SERVICE_Port);
}
}
void startListener() {
listener = new LiteListener(manager, SERVICE_Port, null);
Thread thread = new Thread(listener);
thread.start();
}
@Override
protected void onDestroy() {
if (mNsdManager != null) {
mNsdManager.unregisterService(mRegistrationListener);
}
super.onDestroy();
}
public void registerService(int port) {
NsdServiceInfo serviceInfo = new NsdServiceInfo();
serviceInfo.setServiceName(SERVICE_NAME);
serviceInfo.setServiceType(SERVICE_TYPE);
serviceInfo.setPort(port);
try {
mNsdManager.registerService(serviceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);
} catch (Exception e) {
e.printStackTrace();
}
}
NsdManager.RegistrationListener mRegistrationListener = new NsdManager.RegistrationListener() {
@Override
public void onServiceRegistered(NsdServiceInfo NsdServiceInfo) {
SERVICE_NAME = NsdServiceInfo.getServiceName();
}
@Override
public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
}
@Override
public void onServiceUnregistered(NsdServiceInfo serviceInfo) {
}
@Override
public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
}
};
public String getLocalIpAddress() {
WifiManager wm = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
return Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
}