3

I'm currently writing an android app that utilises AltBeacon's (previously Radius Network's) Android-Beacon-Library.

I require the app to display a notification upon seeing a beacon, which I have achieved using a Bootstrap Notifier as detailed in the sample code here under the Starting an App in the Background header. (Modifying this code to display a notification as opposed to starting the application).

However I also require the app to setup a RangeNotifier in another activity so I can measure the distances of the beacons from the mobile device. I also did this through modifying the sample code in the link above under the Ranging Example Code header.

When I did this however, it seemed fine on binding to the activity and setting up the RangingNotifier, but upon destroying the activity it didn't seem to unbind the BeaconConsumer implemented by the activity and the the code within didRangeBeaconsInRegion(...) function kept executing even though onDestroy() was being called.

So I tried calling beaconManager.stopRangingBeaconsInRegion(region); before calling beaconManager.unbind(this); and that seemed to stop the code within didRangeBeaconsInRegion(...) from executing, but after the activity was destroyed I still kept seeing the Bluetooth LE scanning on repeatedly in the Logs without delay between each scan as such.

08-14 11:09:50.527: D/BluetoothAdapter(21572): stopLeScan()
08-14 11:09:50.537: D/BluetoothAdapter(21572): startLeScan(): null
08-14 11:09:50.547: D/BluetoothAdapter(21572): onClientRegistered() - status=0 clientIf=7

Often when I upload a new build and go back into the particular activity using the RangingNotifier I would receive the following error:

java.lang.RuntimeException: An error occured while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:300)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    at java.lang.Thread.run(Thread.java:841)
Caused by: java.util.ConcurrentModificationException
    at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
    at org.altbeacon.beacon.service.BeaconService$ScanProcessor.doInBackground(BeaconService.java:609)
    at org.altbeacon.beacon.service.BeaconService$ScanProcessor.doInBackground(BeaconService.java:602)
    at android.os.AsyncTask$2.call(AsyncTask.java:288)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    ... 4 more

Which leads me to believe that the BeaconManager possibly isn't undbinding from the Activity.

Is it possible to use both Background Monitoring via using an Application Class, and use a RangingNotifier in an activity within the same application?

Sorry for the long question, couldn't seem to find anything detailing this elsewhere!

1 Answers1

0

If I were you, I would have all beacon related stuff in your Application class, and then use the LocalBroadcastManager to let other parts of you app know about the ranging: Assuming RANGING_DONE is a public static final String RANGING_DONE = "RANGING_DONE";

Intent rangingDoneIntent = new Intent(YourAppClass.RANGING_DONE);
LocalBroadcastManager.getInstance(this).sendBroadcast(
            rangingDoneIntent);

You could even add beacons to the Intent, as they implement Parcelable.

Then in your activity, listen for RANGING_DONE intents:

LocalBroadcastManager.getInstance(this).registerReceiver(mRangingDoneReciever, new IntentFilter(YourAppClass.RANGING_DONE));

Where mRangingDoneReciever is a BroadcastReciever:

private BroadcastReceiver mRangingDoneReciever = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        //Do something with the recieved ranging....
    }
}

I hope I can use my answer...

/Steffen