Whenever I use registerService function it gives an exception with code error 0. 1) I am creating a local chat service app , where I need to have local service broadcast and while doing so I've been stuck as no matter what I do , Registration always fails with error code 0. I have no multiple registration instances in onResume() or onPause() blocks. 2) Logcat shows nothing other than
Attempted to finish an input event but the input event receiver has already been disposed.
whenever I click on register service button on top right hand corner.
This is my code :
public String mServiceName = "nearByDevices";
NsdServiceInfo mService;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
public void registerService(int port) {
// Create the NsdServiceInfo object, and populate it.
// Cancel any previous registration request
initializeRegistrationListener();
NsdServiceInfo serviceInfo = new NsdServiceInfo();
serviceInfo.setServiceName(mServiceName);
serviceInfo.setServiceType(SERVICE_TYPE);
serviceInfo.setPort(port);
NsdHelper(this);
mNsdManager = (NsdManager) mContext.getSystemService(Context.NSD_SERVICE);
mNsdManager.registerService(serviceInfo,NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);
}
public void NsdHelper(Context context) {
mContext = context;
mNsdManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE);
}
public void initializeRegistrationListener() {
mRegistrationListener = new NsdManager.RegistrationListener() {
@Override
public void onServiceRegistered(NsdServiceInfo NsdServiceInfo) {
mServiceName = NsdServiceInfo.getServiceName();
Toast.makeText(MainActivity.this, "Service registered:" + mServiceName, Toast.LENGTH_LONG).show();
}
@Override
public void onRegistrationFailed(NsdServiceInfo arg0, int arg1) {
Toast.makeText(MainActivity.this, "Service registration failed:" + arg1, Toast.LENGTH_LONG).show();
}
@Override
public void onServiceUnregistered(NsdServiceInfo arg0) {
Toast.makeText(MainActivity.this, "Service unregistered:" + arg0.getServiceName(), Toast.LENGTH_LONG).show();
}
@Override
public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
Toast.makeText(MainActivity.this, "Service unregistration failed:" + errorCode, Toast.LENGTH_LONG).show();
}
};
}
public void initializeDiscoveryListener() {
// Instantiate a new DiscoveryListener
mDiscoveryListener = new NsdManager.DiscoveryListener() {
// Called as soon as service discovery begins.
@Override
public void onDiscoveryStarted(String regType) {
Toast.makeText(MainActivity.this, "Service Discovery Started", Toast.LENGTH_LONG).show();
}
@Override
public void onServiceFound(NsdServiceInfo service) {
// A service was found! Do something with it.
Toast.makeText(MainActivity.this, "Service Discovery Success:" + service.getServiceName(), Toast.LENGTH_LONG).show();
if (!service.getServiceType().equals(SERVICE_TYPE)) {
// Service type is the string containing the protocol and
// transport layer for this service.
Toast.makeText(MainActivity.this, "Unknown Service Type" + service.getServiceType(), Toast.LENGTH_LONG).show();
} else if (service.getServiceName().equals(mServiceName)) {
Toast.makeText(MainActivity.this, "Same Machine" + mServiceName, Toast.LENGTH_LONG).show();
} else {
devices.add((service.getServiceName()).toString()); }
}
@Override
public void onServiceLost(NsdServiceInfo service) {
Toast.makeText(MainActivity.this, "Service Lost:" + service.getServiceName(), Toast.LENGTH_LONG).show();
if (mService == service) {
mService = null;
}
}
@Override
public void onDiscoveryStopped(String serviceType) {
Toast.makeText(MainActivity.this, "Discovery Stopped" + serviceType, Toast.LENGTH_LONG).show();
}
@Override
public void onStartDiscoveryFailed(String serviceType, int errorCode) {
Toast.makeText(MainActivity.this, "Discovery failed. Error Code" + errorCode, Toast.LENGTH_LONG).show();
}
@Override
public void onStopDiscoveryFailed(String serviceType, int errorCode) {
Toast.makeText(MainActivity.this, "Discovery failed. Error Code" + errorCode, Toast.LENGTH_LONG).show();
}
};
}
public void initializeResolveListener() {
mResolveListener = new NsdManager.ResolveListener() {
@Override
public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
// Called when the resolve fails. Use the error code to debug.
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;
int port = mService.getPort();
InetAddress host = mService.getHost();
}
};
}
public void discoverServices() {
stopDiscovery(); // Cancel any existing discovery request
initializeDiscoveryListener();
mNsdManager.discoverServices(
SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
}
public void stopDiscovery() {
if (mDiscoveryListener != null) {
try {
mNsdManager.stopServiceDiscovery(mDiscoveryListener);
} finally {
}
mDiscoveryListener = null;
}
} public NsdServiceInfo getChosenServiceInfo() {
return mService;
}
public void tearDown() {
if (mRegistrationListener != null) {
try {
mNsdManager.unregisterService(mRegistrationListener);
} finally {
}
mRegistrationListener = null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//initializeServerSocket();
devices = new ArrayList<String>();
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.main_listview, devices);
ListView listView = (ListView) findViewById(R.id.device_list);
if(devices.isEmpty()) {
devices.add("No Devices Nearby");
listView.setAdapter(adapter);
}
else
listView.setAdapter(adapter);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
switch (id) {
case R.id.register:
// initializeServerSocket();
registerService(65010);
return true;
case R.id.discover:
discoverServices();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app deep link URI is correct.
Uri.parse("android-app://com.example.devesh1.mynewapp/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app deep link URI is correct.
Uri.parse("android-app://com.example.devesh1.mynewapp/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
}
I had this issue while trying to register a service using the NsdManager as well. In the end I found out that the service type MUST adhere to the format described as an example in the NsdManager documentation. To clarify, the format is: "_name._communicationprotocol" eg.: "_ipp._tcp" and "_http._tcp. If it does not, it will result in a callback to onRegistrationFailed with errorCode=0
BTW. debug log from NsdManager is not outputted to Logcat as coming from your app. You must view the log with "No Filters" to see debug information from the NSD service
Update: Just stumbled into this error again. Was searching for _name._tcp.local
however I should only search for _name._tcp
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