AltBeacon Library issue

1.6k views Asked by At

I am developing an android project with AltBeacon referring this code in GitHub - https://github.com/justinodwyer/Beacon-Scanner-and-Logger But facing the following issue in eclipse-

The BeaconManager is not bound to the service. Call beaconManager.bind(BeaconConsumer consumer) and wait for a callback to onBeaconServiceConnect()

My code is as follows.

BeaconScannerApp app = (BeaconScannerApp)this.getApplication();
beaconManager = app.getBeaconManager();
beaconManager.getBeaconParsers().add(new BeaconParser()
.setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
beaconManager.bind(this);
region = new Region("myRangingUniqueId", null, null, null);
beaconManager.setRangeNotifier(new RangeNotifier() {
        @Override 
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
        if (beacons.size() > 0) {
        Iterator <Beacon> beaconIterator = beacons.iterator();
        while (beaconIterator.hasNext()) {
        Beacon beacon = beaconIterator.next();
        logBeaconData(beacon);
        }
        }
        }
       });

       try {
           beaconManager.startRangingBeaconsInRegion(region);
       } catch (RemoteException e) {   
        Log.v("TEST", e.getMessage());
       }
1

There are 1 answers

0
ManuVR On BEST ANSWER

All the initialization from beacons should be made in OnBeaconServiceConnect() I had a trouble ones with DidExitRegion() and DidEnterRegion() and it was exactly because I was not doing to initialization of my BeaconManager Handlers in OnBeaconServiceConnect(). You can also, sometimes, do it OnCreate but for me was not working. It is C# but it is really similar.

Here it is an example that works well:

public void OnBeaconServiceConnect()
    {

        beaconManager.SetForegroundScanPeriod(1000L);
        beaconManager.SetForegroundBetweenScanPeriod(10000L);

        rangeNotifier.DidRangeBeaconsInRegionComplete += RangeNotifier_DidRangeBeaconsInRegionComplete;
        monitorNotifier.EnterRegionComplete += MonitorNotifier_EnterRegionComplete;
        monitorNotifier.ExitRegionComplete += MonitorNotifier_ExitRegionComplete;

        beaconManager.SetRangeNotifier(rangeNotifier);
        beaconManager.SetMonitorNotifier(monitorNotifier);

        try
        {
            beaconManager.StartMonitoringBeaconsInRegion(region);
            beaconManager.StartRangingBeaconsInRegion(region);
        }catch(RemoteException e)
        {
            Log.Debug(TAG,e.ToString());
        }

    }