Background BootstrapNotifier along with Activity with RangeNotifier using AltBeacon

I am currently writing an Android app that uses Android-Beacon-Library from AltBeacon (formerly Radius Network).

I need the app to display a notification when it sees the beacon that I received with the bootstrap notifier, as described in the sample code here under Running the app in the background . (Changing this code to display a notification, not launch the app).

However, I also require the app to set up a RangeNotifier in another activity so that I can measure the distances of the beacons from the mobile device. I also did this by modifying the example code in the link above under the heading Ranging Example Code .

However, when I did it, it seemed to me that it had something to do with binding to the activity and setting up the RangingNotifier, but after destroying the activity, it didn't seem to cancel the BeaconConsumer implemented as a result of the action and the code inside the function didRangeBeaconsInRegion(...)

continued to execute even though onDestroy () was called ...

So, I tried making a call beaconManager.stopRangingBeaconsInRegion(region);

before the call beaconManager.unbind(this);

and that seemed to stop executing the code inside didRangeBeaconsInRegion(...)

, but after the activity was destroyed, I still kept checking the Bluetooth LE scans in the logs constantly with no 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 times, when I load a new assembly and go back to a specific activity using the RangingNotifier, I get 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

      

This leads me to believe that the BeaconManager may not be distracted from the Activity.

Is it possible to use background monitoring using an application class and use the RangingNotifier in an activity within the same application?

Sorry for the long question, didn't seem to find anything detailed elsewhere!

+3


source to share


1 answer


If I were you, I would have all the beacon-related stuff in your Application class and then using LocalBroadcastManager to let other parts of your application know the ranking: 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 can even add beacons to the intent as they implement Parcelable.

Then in your activity listen to RANGING_DONE:

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

      



Where mRangingDoneReciever is BroadcastReciever:

private BroadcastReceiver mRangingDoneReciever = new BroadcastReceiver() {

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

      

Hopefully I can use my answer ...

/ Steffen

0


source







All Articles