I recently made a project, and from what I read in your question, it has some similarities with what I did.
I know that I could constantly check this, but the battery usage is too much, and I would like it to be called even when my application is not in use.
Regarding the battery problem, when Bluetooth consumes a lot of power all the time, but at the same time you cannot detect BLE without turning on Bluetooth.
What I did is two experiments, and both of them are useful, but different, and I can’t say which one is better, but you need to test it so that it meets your requirement.
After starting Thread, which turns on Bluetooth and listens to iBeacon on and off (with sleep time), while programmatically. This can be done in many ways.
Using a package called Altbeacon, there are many useful functions, one of these functions is to automatically save the battery with an example code :
public class MyApplication extends Application implements BootstrapNotifier { private BackgroundPowerSaver backgroundPowerSaver; public void onCreate() { super.onCreate();
We need a broadcast event that wakes up our application when a BLE device with a specific Service-UUID is available. Perhaps there is now a better BLE API available than 2 years ago. The most energy-efficient and most accurate method is rewarded.
The other part, this is called triggering actions at a certain distance. I still use Altbeacon to check the range of beacons and the response. A sample code is something like
@Override public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) { for (Beacon beacon : beacons) { if (beacon.getDistance() < 5.0) { Log.d(TAG, "I see a beacon that is less than 5 meters away.");
So, when you said, you can also get the distance from a specific UUID. I'm building an Altbeacon based method that looks like this (see in the for and if loop):
private void startRangeNotifier() { Log.i(TAG, "Starting range notifier..."); beaconManager.setRangeNotifier(new BeaconRangeListener() { @Override public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) { if (beacons.size() > 0) { for (Beacon beacon : beacons) { Log.d(TAG, "uuid's: " + beacon); Log.d(TAG, "uuid id1: " + beacon.getId1()); if (beacon.getId1().toString() .equals("b9407f30-f5f8-466e-aff9-25556b57fe6d")) { Log.d(TAG, "uuid id1 distance: " + beacon.getDistance()); } } } } }); try { beaconManager.startRangingBeaconsInRegion( new Region(BEACON_MONITORING_ID, null, null, null)); } catch (RemoteException e) { e.printStackTrace(); } }
My log output:
D/Main activity:: uuid's: id1: b9407f30-f5f8-466e-aff9-25556b57fe6d id2: 31807 id3: 59251 D/Main activity:: uuid id1: b9407f30-f5f8-466e-aff9-25556b57fe6d D/Main activity:: uuid id1 distance: 0.2108658568686884
In my answer, I wanted to introduce the concept that I used, the Beacons project requires patience in general. As mentioned in another answer, here you can also combine the solution with Geofences and ActivityRecognition.
Note. Since the nature of the Bluetooth beacon, the distance is proximity, not absolute, and for a while even the Bluetooth beacon is 1 meter, it may seem 2 meters or 0.5 meters, so keep that in mind
Link Link: