Android AltBeacon Ranging Background Service

I am working on a bluetooth application using the AltBeacon library. It seems that only in the case of BeaconManager allowed for each application. The problem I encountered is this: I want to constantly work in the background, which constantly performs bluetooth setup and sends notifications. If I open my application (bring it to the fore), I was a service to pause. Then the front end will do the ranking and display the content on the screen.

The problem is that the beacon manager (from BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this); ) in activity and maintenance is the same instance. Therefore, when the activity is closed, it calls beaconManager.unbind(this); , and the range alert in the service no longer works.

Can I get two separate copies of the lighthouse manager? If not, how can I work in continuous operation and activity?

Rangingctivity

 @Override protected void onCreate(Bundle savedInstanceState) { ... regionEstimote = new Region("estimote", null, null, null); 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); } @Override protected void onDestroy() { super.onDestroy(); notificationManager.cancel(NOTIFICATION_ID); //beaconManager.unbind(this); } @Override public void onBeaconServiceConnect() { beaconManager.setRangeNotifier(new RangeNotifier() { .... }); try { beaconManager.startRangingBeaconsInRegion(regionEstimote); } catch (RemoteException e) { Log.e(TAG, "RangingActivity", e); } } 

BeaconService.java

 @Override public int onStartCommand(Intent intent, int flags, int startId) { if(beaconHistory == null) beaconHistory = new HashMap<Integer, Date>(); mBeaconManager = BeaconManager.getInstanceForApplication(this); mBeaconManager.getBeaconParsers().add(new BeaconParser(). setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24")); return START_STICKY; } @Override public void onDestroy() { super.onDestroy(); beaconHistory = null; mBeaconManager.unbind(this); } @Override public void onBeaconServiceConnect() { mBeaconManager.setRangeNotifier(new RangeNotifier() { @Override public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) { if(ActivityBase.isActivityVisible()) { //don't do ranging logic if any activity from my app is in the foreground return; } ... } }); try { mBeaconManager.startRangingBeaconsInRegion(regionMint); } catch (RemoteException e) { Log.e(TAG, "BeaconService", e); } } 
+7
java android bluetooth ibeacon altbeacon
source share
2 answers

This is the case when the custom class android.app.Application very useful. BeaconManager is a singleton, so only one is allowed. Similarly, the Application class has one instance for the active Android application. If you want to perform beacon detection in Activity and Service at the same time, use the centralized Application class to bind to the BeaconManager , and then redirect callbacks to both your Activity and your Service .

You can see an example of binding to the BeaconManager in the Application class and then pass the callbacks to the Activity in the reference application here: https://github.com/AltBeacon/android-beacon-library-reference/blob/master/app/src/main/ java / org / altbeacon / beaconreference / BeaconReferenceApplication.java

+4
source share

You must extend the Applicaton class and run background monitoring of the region, as described here (see "Running the application in the background").

To perform the ranking, in the same class we implement RangeNotifier:

 public class AndroidApp extends Application implements BootstrapNotifier, RangeNotifier {... 

Run the ranking in the didEnterRegion file:

 @Override public void didEnterRegion(Region region) { try { mBeaconManager.startRangingBeaconsInRegion(region); } catch (RemoteException e) { if (BuildConfig.DEBUG) Log.d(Const.TAG, "Can't start ranging"); } } 
  1. Implement the didRangeBeaconsInRegion method:

     @Override public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) { if (beacons.size() > 0) { Beacon firstBeacon = beacons.iterator().next(); if (BuildConfig.DEBUG) Log.d(Const.TAG, "Beacon ranged: UUID: " + firstBeacon.getId1().toString() + " Major: " + firstBeacon.getId2().toString() + " Minor: " + firstBeacon.getId3().toString()); // Do something with the result // Stop ranging try { mBeaconManager.stopRangingBeaconsInRegion(region); } catch (RemoteException e) { e.printStackTrace(); } } } 
+1
source share

All Articles