I am trying to detect beacon devices using my device, and so monitoring them and also listening to the range notifications.
I am using this library and the library-reference app. I managed to listen to a custom beacon using the set beacon layout method.
Step1. I set the layout in the application class Step 2. I mke the baseactivity implement beaconconsume where it performs ranging.
When starting the range monitoring service/method we use "myRangeUniqueId" but the didexit and didenter methods use "backgroundId" I think. Why is that so?
So the situation is this I move the beacon device some few meters away , I still get I see a beacon notification and I do not see a beacon.. These messages keep changing alternatively even when the beacon is far.
Do I have to do anything to prevent this? Please help.
the code snippets include below:
The application class implements BootstrapNotifier and the code is as under
BeaconManager beaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this); beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("LAYOUT_HERE")); Region region = new Region("backgroundRegion", null, null, null); regionBootstrap = new RegionBootstrap(this, region); // simply constructing this class and holding a reference to it in your custom Application // class will automatically cause the BeaconLibrary to save battery whenever the application // is not visible. This reduces bluetooth power usage by about 60% backgroundPowerSaver = new BackgroundPowerSaver(this); @Override public void didEnterRegion(Region arg0) { // In this example, this class sends a notification to the user whenever a Beacon // matching a Region (defined above) are first seen. Log.d(TAG, "did enter region."); if (!haveDetectedBeaconsSinceBoot) { Log.d(TAG, "auto launching MainActivity"); haveDetectedBeaconsSinceBoot = true; } else { // i am not sending out any notification here since there could be multiple beacons and i need to identify any one of them with a specific uuid } } @Override public void didExitRegion(Region region) { sendNotification("exited",2); }
i do nothing in didDetermineStateForRegion
method,its just overridden
I have a
BaseActivity
that implementsBeaconConsumer
private BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); beaconManager.bind(this); } @Override protected void onDestroy() { super.onDestroy(); beaconManager.unbind(this); } @Override protected void onPause() { super.onPause(); if(beaconManager.isBound(this))beaconManager.setBackgroundMode(true); } @Override protected void onResume() { super.onResume(); if (beaconManager.isBound(this)) beaconManager.setBackgroundMode(false); } @Override public void onBeaconServiceConnect() { beaconManager.setRangeNotifier(new RangeNotifier() { @Override public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) { if (beacons.size() > 0) { /*EditText editText = (EditText)RangingActivity.this .findViewById(R.id.rangingText); Beacon firstBeacon = beacons.iterator().next(); logToDisplay("The first beacon "+firstBeacon.toString()+" is about "+firstBeacon.getDistance()+" meters away."); */ CommonUtilities.sendNotification(BaseActivity.this,"entered",1); } } } } } }); try { beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId",null, null, null)); } catch (RemoteException e) { } }
P.S: When i move the device to a different location say some 5 meters away, i get random notification, that the beacon is in range and then immediately i get a notification that the beacon is out of range.
Thanks