1

I´m using the Android IBeacon Library for a Project. I need to create a Service that starts ranging for beacons in the background and notify the user when it finds one (the nearest one). I have searched a lot and coded based on many examples I have found, but still it doesn´t work. Using logs I found that the IBeaconManager doesn't bind, so onIBeaconServiceConnect never gets called. I have already tried some solutions I found here but none of them has been useful. I would really appreciate if someone could help me solving this problem. I post some of my code here

public class RangingService extends Service implements IBeaconConsumer
{
private IBeaconManager beaconManager; @Override public void onCreate() { super.onCreate(); beaconManager = IBeaconManager.getInstanceForApplication(this); Log.d("RangingService","Created beaconManager instance"); beaconManager.setBackgroundBetweenScanPeriod(120000); beaconManager.setBackgroundScanPeriod(30000); beaconManager.bind(this); if(beaconManager.isBound(this)) { Log.d("RangingService","Beacon manager bound"); } else { Log.d("RangingService","Beacon manager not bound"); } //Show the service has started notify("RangingService created", "RangingService has started"); } @Override public void onDestroy() { super.onDestroy(); beaconManager.unBind(this); } @Override public void onIBeaconServiceConnect() { Log.d("RangingService", "Entering onIBeaconServiceConnect"); beaconManager.setRangeNotifier(new RangeNotifier() { @Override public void didRangeBeaconsInRegion(Collection<IBeacon> beacons, Region region) { if(beacons.size() > 0) { IBeacon nearestBeacon = beacons.iterator().next(); for(IBeacon b : beacons) { if(nearestBeacon.getProximity() == IBeacon.PROXIMITY_UNKNOWN) { nearestBeacon = b; } else { if(b.getProximity() != IBeacon.PROXIMITY_UNKNOWN) { if(b.getAccuracy() < nearestBeacon.getAccuracy()) { nearestBeacon = b; } } } } Log.d("RangingService","Nearest Beacon Found "+nearestBeacon.getMajor()+";"+nearestBeacon.getMinor()); notify("Beacon read","Major: "+nearestBeacon.getMajor()+"; Minor: "+nearestBeacon.getMinor()); } else { Log.d("RangingService","No beacons"); } } }); try { Log.d("RangingService", "Entering startRangingBeacons"); beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null)); } catch(RemoteException e) { notificar("Error", e.getMessage()); Log.e("RangingService", "Error while starting scanning: "+e.getMessage()); } } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub Log.d("RangingService", "Entering onBind"); return null; }

The Service is already in my manifest also. Thanks for your help.

Andres M.
  • 60
  • 1
  • 7
  • Why you don't use `onStartCommand()` and `onBind()` methods? How are you starting the service and binding to it? – Alberto Oct 13 '14 at 22:49
  • @Alberto I start the service from the `onCreate()` of my main activity with `startService()`. I don't use `onBind()` or `onStartCommand()` because the examples I've found don't use them, and my code is based on them. – Andres M. Oct 13 '14 at 22:58

2 Answers2

2

A couple of points:

  1. The code indicates you are using a 0.x version of the Android iBeacon Library. If you are starting a new project, I would strongly recommend you use the Android Beacon Library 2.0, as the earlier library is no longer actively maintained, and it is now hard to find documentation for it.

  2. Regardless of the library version you are using, you should be able to get a callback to the onBeaconServiceConnect() method after you bind to the BeaconManager. The fact that you don't get this callback probably indicates that the BeaconService is not starting up properly.

The most likely reason that the BeaconService is not starting up properly is because it is not properly declared in the manifest. If you are using Eclipse, you must edit your project.properties file and add the line: manifestmerger.enabled=true. If you are using AndroidStudio, this is not necessary. If you are using IntelliJ, you may have to declare the service manually.

You can verify if the manifest has the proper entries by looking at the generated manifest file in bin/AndroidManifest.xml, and verifying it has an entry for the BeaconService.

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
0

I had faced similar issues. Got resolved by doing following things

  1. In eclipse project.properties added manifestmerger.enabled=true

  2. restarted the eclipse

  3. uninstalled other beacon apps in my android phone and restarted the phone