public class BeaconManager
extends java.lang.Object
BeaconRegion
(or SecureBeaconRegion
) or any other abstract region, like MirrorRegion
)ConfigurableDevice
, Eddystone
, Nearable
, EstimoteLocation
etc.Example use for beacon ranging & Nearables discovery:
private static final UUID ESTIMOTE_PROXIMITY_UUID = UUID.fromString("B9407F30-F5F8-466E-AFF9-25556B57FE6D");
private static final BeaconRegion ALL_ESTIMOTE_BEACONS_REGION = new BeaconRegion("rid", ESTIMOTE_PROXIMITY_UUID, null, null);
BeaconManager beaconManager = new BeaconManager(context);
beaconManager.setRangingListener(new BeaconManager.BeaconRangingListener() {
{@literal @Override} public void onBeaconsDiscovered(BeaconRegion region, final List<Beacon> beacons) {
Log.d(TAG, "Ranged beacons: " + beacons);
}
});
beaconManager.setNearableListener(new BeaconManager.NearableListener() {
{@literal @Override} public void onNearablesDiscovered(List<Nearable> nearables) {
Log.d(TAG, "Discovered nearables: " + nearables);
}
});
beaconManager.setEddystoneListener(new BeaconManager.EddystoneListener() {
{@literal @Override} public void onEddystonesFound(List<Eddystone> eddystones) {
Log.d(TAG, "Nearby eddystones: " + eddystones);
}
});
// Before starting scan, manager needs to establish connection to the background service, which handles scanning.
// This may take some time and is asynchronous, so we need to wait for callback.
// After connecting, manager is ready to start doing its job.
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
{@literal @Override} public void onServiceReady() {
// Beacons ranging.
beaconManager.startRanging(ALL_ESTIMOTE_BEACONS_REGION);
// Nearable discovery.
beaconManager.startNearableDiscovery();
// Eddystone scanning.
beaconManager.startEddystoneScanning();
}
});
// When no longer needed. Note that beacon ranging, nearables discovery, Eddystone scanning will stop.
beaconManager.disconnect();
It is a good practice to have only one instance of BeaconManager
per application
stored in your Application
subclass.
If you want to use beacons with enabled Secure UUID. Use SecureBeaconRegion
instead of BeaconRegion
class
when starting ranging or monitoring.
Some methods throw RemoteException
. This can happen if service call
happened and hosting process no longer exists (only if service is remote process).
Modifier and Type | Class and Description |
---|---|
static interface |
BeaconManager.BeaconMonitoringListener
Callback to be invoked when beacons are discovered in a region.
|
static interface |
BeaconManager.BeaconRangingListener
Callback to be invoked when beacons are ranged.
|
static interface |
BeaconManager.ConfigurableDevicesListener
Callback to be invoked when Estimote Configurable packets are discovered nearby.
|
static interface |
BeaconManager.EddystoneListener
Callback to be invoked when Eddystone beacons are in range.
|
static interface |
BeaconManager.ErrorListener
Callback to be invoked when any error happened while performing low energy scanning.
|
static interface |
BeaconManager.LocationListener
Callback to be invoked when Estmimote Location packets are discovered nearby.
|
static interface |
BeaconManager.MirrorListener
Callback to be invoked when Estmimote Location packets are discovered nearby.
|
static interface |
BeaconManager.MirrorMonitoringListener
Callback to be invoked when mirrors are discovered .
|
static interface |
BeaconManager.MirrorRangingListener
Callback to be invoked when mirrors are ranged.
|
static interface |
BeaconManager.NearableListener
Callback to be invoked when nearables are discovered nearby.
|
static interface |
BeaconManager.ScanStatusListener
Callback to inform client when scanning is started and stopped.
|
static interface |
BeaconManager.ServiceReadyCallback
Callback to inform client when service is ready to use.
|
static interface |
BeaconManager.TelemetryListener
Callback to be invoked when Estmimote telemetry packets are discovered nearby.
|
Constructor and Description |
---|
BeaconManager(android.content.Context context)
Constructs
BeaconManager instance. |
Modifier and Type | Method and Description |
---|---|
void |
connect(BeaconManager.ServiceReadyCallback callback)
Connects to
BeaconService . |
void |
disconnect()
Disconnects from
BeaconService . |
void |
setBackgroundScanPeriod(long scanPeriodMillis,
long waitTimeMillis)
Changes defaults scanning periods when only monitoring is performed.
|
void |
setConfigurableDevicesListener(BeaconManager.ConfigurableDevicesListener listener)
Sets new Estimote Configurable packet listener.
|
void |
setEddystoneListener(BeaconManager.EddystoneListener listener)
Sets new Eddystone listener.
|
void |
setErrorListener(BeaconManager.ErrorListener listener)
Sets new error listener.
|
void |
setForegroundScanPeriod(long scanPeriodMillis,
long waitTimeMillis)
Changes defaults scanning periods when ranging is performed.
|
void |
setLocationListener(BeaconManager.LocationListener listener)
Sets new Estimote Location packet listener.
|
void |
setMirrorListener(BeaconManager.MirrorListener listener)
Sets new Estimote Mirror listener.
|
void |
setMonitoringListener(BeaconManager.BeaconMonitoringListener listener)
Sets new beacon monitoring listener.
|
void |
setMonitoringListener(BeaconManager.MirrorMonitoringListener listener)
Sets new Estimote Mirror listener.
|
void |
setNearableListener(BeaconManager.NearableListener listener)
Sets new Nearable listener.
|
void |
setRangingListener(BeaconManager.BeaconRangingListener listener)
Sets new ranging listener.
|
void |
setRangingListener(BeaconManager.MirrorRangingListener listener)
Sets new ranging listener.
|
void |
setRegionExitExpiration(long period)
Changes expiration time for beacons.
|
void |
setScanRequestDelay(long delayMillis)
Since Nougat, every application is allowed to start/stop BLE scan a maximum of 5 times per 30s.
|
void |
setScanStatusListener(BeaconManager.ScanStatusListener listener)
Sets new scan status listener.
|
void |
setTelemetryListener(BeaconManager.TelemetryListener listener)
Sets new Estimote telemetry listener.
|
void |
startConfigurableDevicesDiscovery()
Starts scanning for Estimote Mirrors in your vicinity.
|
void |
startEddystoneDiscovery()
Starts scanning for all Eddystone beacons in your vicinity.
|
void |
startEddystoneScanning()
Deprecated.
Please use
startEddystoneDiscovery() instead. |
void |
startLocationDiscovery()
Starts scanning for Estimote Location packets in your vicinity.
|
void |
startMirrorDiscovery()
Starts scanning for Estimote Mirrors in your vicinity.
|
void |
startMonitoring(BeaconRegion beaconRegion)
Starts beacon monitoring.
|
void |
startMonitoring(MirrorRegion mirrorRegion)
Starts Mirror monitoring.
|
void |
startNearableDiscovery()
Starts scanning for all Nearables in your vicinity.
|
void |
startRanging(BeaconRegion beaconRegion)
Starts ranging given range.
|
void |
startRanging(MirrorRegion mirrorRegion)
Starts ranging given range.
|
void |
startTelemetryDiscovery()
Starts scanning for Estimote telemetry packets in your vicinity.
|
void |
stopConfigurableDevicesDiscovery()
Stops discovery of Estimote Mirrors.
|
void |
stopEddystoneDiscovery()
Stops scanning of Eddystone beacons.
|
void |
stopEddystoneScanning()
Deprecated.
Please use
stopEddystoneDiscovery() instead. |
void |
stopLocationDiscovery()
Stops discovery of Estimote Location packets.
|
void |
stopMirrorDiscovery()
Stops discovery of Estimote Mirrors.
|
void |
stopMonitoring(java.lang.String regionId)
Stops monitoring for given region.
|
void |
stopNearableDiscovery()
Stops discovery of nearables.
|
void |
stopRanging(BeaconRegion beaconRegion)
Stops ranging for beacons that was starting with
startRanging(BeaconRegion) method. |
void |
stopRanging(MirrorRegion mirrorRegion)
Stops ranging for mirrors that was starting with
startRanging(MirrorRegion) method. |
void |
stopTelemetryDiscovery()
Stops discovery of Estimote telemetry.
|
public BeaconManager(android.content.Context context)
BeaconManager
instance. It's good practice to have only one
per application stored in your Application
subclass.context
- Context to wire up BeaconManager
.public void connect(BeaconManager.ServiceReadyCallback callback)
BeaconService
.callback
- Callback to be invoked when connection is made to service.public void disconnect()
BeaconService
. If there were any ranging or monitoring
in progress, they will be stopped. All listeners will be set to null.
This should be typically called in onDestroy method.public void setScanStatusListener(BeaconManager.ScanStatusListener listener)
Scan status listener can be used to detect that scanning was started/stopped due to external circumstances like user enabling/disabling Bluetooth adapter.
Note: This listener is not invoked during scan cycles. It only is called when user starts scanning, stops it or disables Bluetooth effectively stopping scanning.
Note: You will receive notification if BeaconManager
is still connected to BeaconService.
If you call disconnect()
you will no longer receive this notification.
listener
- New listener.public void setRangingListener(BeaconManager.BeaconRangingListener listener)
listener
- New listener.public void setRangingListener(BeaconManager.MirrorRangingListener listener)
listener
- New listener.public void setMonitoringListener(BeaconManager.BeaconMonitoringListener listener)
listener
- New listener.public void setMonitoringListener(BeaconManager.MirrorMonitoringListener listener)
listener
- New listener.public void setEddystoneListener(BeaconManager.EddystoneListener listener)
listener
- New listener.public void setNearableListener(BeaconManager.NearableListener listener)
listener
- New listener.public void setTelemetryListener(BeaconManager.TelemetryListener listener)
listener
- New listener.public void setLocationListener(BeaconManager.LocationListener listener)
listener
- New listener.public void setConfigurableDevicesListener(BeaconManager.ConfigurableDevicesListener listener)
listener
- New listener.public void setMirrorListener(BeaconManager.MirrorListener listener)
listener
- New listener.public void setErrorListener(BeaconManager.ErrorListener listener)
listener
- New error listener.public void setForegroundScanPeriod(long scanPeriodMillis, long waitTimeMillis)
Note: These are only suggested values. Scan period times may be adjusted by internal algorithms or operating system.
scanPeriodMillis
- How long to perform Bluetooth Low Energy scanning?waitTimeMillis
- How long to wait until performing next scanning?public void setBackgroundScanPeriod(long scanPeriodMillis, long waitTimeMillis)
Note: These are only suggested values. Scan period times may be adjusted by internal algorithms or operating system.
Note: Setting scan time to less than second is not recommended, because by default beacons' broadcast interval is 950ms.
scanPeriodMillis
- How long to perform Bluetooth Low Energy scanning?waitTimeMillis
- How long to wait until performing next scanning?public void setRegionExitExpiration(long period)
period
- - expiration period from 1000-60000ms range.public void setScanRequestDelay(long delayMillis)
delayMillis
- delay in milliseconds. Must be greater or equal to zero. The default delay value is set to 1.5s (1500millis).public void startRanging(BeaconRegion beaconRegion)
setRangingListener(BeaconRangingListener)
.
If given region is already ranged, this is no-op.
You can range multiple beaconRegions at the same time. Listener will be called for
each region separately.
You can use SecureBeaconRegion
to get beacons that are using Secure UUID
with real location UUID, major and minor numbers (only if you own those beacons).
beaconRegion
- BeaconRegion to range.public void startRanging(MirrorRegion mirrorRegion)
setRangingListener(BeaconRangingListener)
.
If given region is already ranged, this is no-op.
You can range multiple beaconRegions at the same time. Listener will be called for
each region separately.
You can use SecureBeaconRegion
to get beacons that are using Secure UUID
with real location UUID, major and minor numbers (only if you own those beacons).
mirrorRegion
- BeaconRegion to range.public void stopRanging(BeaconRegion beaconRegion)
startRanging(BeaconRegion)
method.beaconRegion
- BeaconRegion that was previously ranged.public void stopRanging(MirrorRegion mirrorRegion)
startRanging(MirrorRegion)
method.mirrorRegion
- mirror region that was previously ranged.public void startEddystoneDiscovery()
setEddystoneListener(EddystoneListener)
.public void stopEddystoneDiscovery()
@Deprecated public void startEddystoneScanning()
startEddystoneDiscovery()
instead.setEddystoneListener(EddystoneListener)
.@Deprecated public void stopEddystoneScanning()
stopEddystoneDiscovery()
instead.public void startNearableDiscovery()
setNearableListener(NearableListener)
.public void stopNearableDiscovery()
public void startTelemetryDiscovery()
setNearableListener(NearableListener)
.public void stopTelemetryDiscovery()
public void startLocationDiscovery()
setNearableListener(NearableListener)
.public void stopLocationDiscovery()
public void startMirrorDiscovery()
setNearableListener(NearableListener)
.public void stopMirrorDiscovery()
public void startConfigurableDevicesDiscovery()
setNearableListener(NearableListener)
.public void stopConfigurableDevicesDiscovery()
public void startMonitoring(BeaconRegion beaconRegion)
You can use SecureBeaconRegion
to get beacons that are using Secure UUID
with real location UUID, major and minor numbers (only if you own those beacons).
beaconRegion
- BeaconRegion to monitor.public void startMonitoring(MirrorRegion mirrorRegion)
mirrorRegion
- MirrorRegion
to monitor.public void stopMonitoring(java.lang.String regionId)
regionId
- string with unique region id.
Can be obtained from for example startMonitoring(BeaconRegion)