Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

discovery of Bonjour services continues after browseService.stop() is called #26

Closed
beegee-tokyo opened this issue Sep 13, 2017 · 2 comments

Comments

@beegee-tokyo
Copy link

beegee-tokyo commented Sep 13, 2017

Not sure if this is a problem in my code.
I start a service from my activity to discover devices that broadcast their services (in my case SERVICE_TYPE = "_arduino._tcp.";).
In onStartCommand() of the service I do

@Override
public int onStartCommand( Intent intent, int flags, int startId ) {
	mHandler = new Handler(Looper.getMainLooper());
	dnssd = new DNSSDBindable(this);
	try {
		browseService = dnssd.browse("_arduino._tcp.", new BrowseListener() {
			@Override
			public void serviceFound(DNSSDService browser, int flags, int ifIndex,
									 final String serviceName, final String regType, final String domain) {
				try {
					dnssd.resolve(flags, ifIndex, serviceName, regType, domain, new ResolveListener() {
						@Override
						public void serviceResolved(DNSSDService resolver, int flags, int ifIndex,
													String fullName, final String hostName, final int port,
													final Map<String, String> txtRecord) {
							try {
								QueryListener listener = new QueryListener() {
									@Override
									public void queryAnswered(DNSSDService query, final int flags, final int ifIndex,
															  final String fullName, int resolveClass, int resolveType,
															  final InetAddress address, int ttl) {
										if (BuildConfig.DEBUG) Log.d(DEBUG_LOG_TAG, "Query address " + fullName);
										queryLooper = new Runnable() {
											@Override
											public void run() {
												BonjourService.Builder builder =
														new BonjourService.Builder(flags, ifIndex, serviceName,
																regType, domain)
																.dnsRecords(txtRecord)
																.port(port)
																.hostname(hostName);
												// Do something with the resolve results
											}
										};
										mHandler.post(queryLooper);
									}

									@Override
									public void operationFailed(DNSSDService service, int errorCode) {}
								};
								dnssd.queryRecord(0, ifIndex, hostName, 1, 1, listener);
								dnssd.queryRecord(0, ifIndex, hostName, 28, 1, listener);
							} catch (DNSSDException e) {e.printStackTrace();}
						}

						@Override
						public void operationFailed(DNSSDService service, int errorCode) {}
					});
				} catch (DNSSDException e) {e.printStackTrace();}
			}

			@Override
			public void serviceLost(DNSSDService browser, int flags, int ifIndex,
									String serviceName, String regType, String domain) {
			}

			@Override
			public void operationFailed(DNSSDService service, int errorCode) {
			}
		});
	} catch (DNSSDException e) {
		if (BuildConfig.DEBUG) Log.d(DEBUG_LOG_TAG, "error", e);
	}

	// Start a countdown to stop the service after 15 seconds
	if (timer != null) {
		timer.cancel();
		timer = null;
	}
	timer = new CountDownTimer(15000, 1000) {
		public void onTick(long millisUntilFinished) {
			//Nothing here!
		}

		public void onFinish() {
			mHandler.removeCallbacksAndMessages(null);
			mHandler.removeCallbacks(null);
			browseService.stop();
			sendMyBroadcast(); 
			timer.cancel();
			timer = null;
			if (BuildConfig.DEBUG) Log.d(DEBUG_LOG_TAG, "NSDDevices - Discovery finished!");
			stopSelf();
		}
	};
	timer.start();

	return super.onStartCommand( intent, flags, startId );
}

I stop the service discovery after 15 seconds (No need to have it running all the time).
But even after browseService.stop(); I can see debug message Query address xxx.local. in the log file.
How do I properly stop the discovery?

@andriydruk
Copy link
Owner

andriydruk commented Sep 13, 2017

Hi, you set logging inside QueryListener, not BrowseListener.
If you get messages inside QueryListener after stopping 'browse' operation, it's ok because 'queryRecord' operation might starts before you stop 'browse'.
If you prefer don't receive any callbacks after stopping you should stop every 'browse', every 'resolve' and every 'queryRecord' operations. Or you can try Rx version of this library, it does all stopping automatically.

Try to put logging inside BrowseListener and if you will have the same problem -> please reopen this issue.

@beegee-tokyo
Copy link
Author

beegee-tokyo commented Sep 13, 2017

Got it, thanks for your prompt reply and for this great lib which works much better than Android NSD. Will try the RX version.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants