Ranging for beacons from an Android service

I’m trying to start the ranging operation from within an android service. I get the following exception when I call the connect method of BeaconManager
android.app.ServiceConnectionLeaked: Service com.honeywell.beaconzone.RelayZoneDataService has leaked ServiceConnection com.estimote.sdk.BeaconManager$InternalServiceConnection@426034c8 that was originally bound here
at android.app.LoadedApk$ServiceDispatcher.(LoadedApk.java:970)
at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:864)
at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1569)
at android.app.ContextImpl.bindService(ContextImpl.java:1552)
at android.content.ContextWrapper.bindService(ContextWrapper.java:517)
at com.estimote.sdk.BeaconManager.connect(BeaconManager.java:223)
at com.honeywell.beaconzone.RelayZoneDataService.startRanging(RelayZoneDataService.java:187)
at com.honeywell.beaconzone.RelayZoneDataService.onHandleIntent(RelayZoneDataService.java:152)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.os.HandlerThread.run(HandlerThread.java:61)

Should I be defining something in my manifest file? Is Ranging allowed from within an Android service?
What does this exception mean?

That usually means that you haven’t called disconnect() on BeaconManager when your service was being shutdown.

@Aaron_Dsouza Can you share your service class.This is the service class i am using now

public class BeaconsMonitoringService extends Service  {
	private BeaconManager beaconManager;
	private static final String PROXIMITY_UUID_1 = "lmno";
//	private static final String PROXIMITY_UUID_2 = "xyz";
	private static final Region BEACONS = new Region("cx",
			PROXIMITY_UUID_1, null, null);
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		if (beaconManager.isBluetoothEnabled()) {
			connectToService();
		}
		return Service.START_STICKY;
	}
	private void connectToService() {
		beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
			@Override
			public void onServiceReady() {
				try {
					L.enableDebugLogging(true);
//					for (Region region : BEACONS) {
//						beaconManager.startRanging(region);// for collection of Beacons!!
//					}
					beaconManager.startRanging(BEACONS);
				} catch (RemoteException e) {
					Log.e("BeaconsMonitoringService",
							"Cannot start ranging, something terrible happened");
					Log.e("BeaconsMonitoringService", "Cannot start ranging", e);
				}
			}
		});
	}
	@Override
	public void onCreate() {
		super.onCreate();
		beaconManager = new BeaconManager(this);
		beaconManager.setBackgroundScanPeriod(TimeUnit.SECONDS.toMillis(5),TimeUnit.SECONDS.toMillis(5));
		beaconManager.setForegroundScanPeriod(TimeUnit.SECONDS.toMillis(5), TimeUnit.SECONDS.toMillis(5));
		beaconManager.setRangingListener(new BeaconManager.RangingListener() {
			@Override
			public void onBeaconsDiscovered(Region region,
			                                List<Beacon> beacons) {
				if(beacons.size()<=0){
					Toast.makeText(getApplicationContext(), "No beacon found",
							Toast.LENGTH_SHORT).show();
				}else{
					for (int i = 0; i < beacons.size(); i++) {
						String beac=beacons.get(i).getProximityUUID();
						Toast.makeText(getApplicationContext(), "I found a beacon with UUID; "+beac,
								Toast.LENGTH_SHORT).show();
					}
				}
			}
		});
	}
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	@Override
	public void onDestroy() {
		super.onDestroy();
	}
}

Is the above code correct or should i use startMonitoringListener.I am getting values from RangingListener also.
@Witek.Is there anything else i need to do?Confused thats all.Thanks

I want to listen for beacon in background and here I’m listening using monitoring. [MonitoringListener]
And yes its working. But when I do post the data to the server on entering the beacon region(onRegionEntered), the monitoring listener stops monitoring.

Suggest steps, if anyone had fixed this.

Thanks.

Can you share code snippet?

How do you notice that it stopped monitoring?