public class BeaconManager extends Object
Activity or Service.
This class is used in conjunction with BeaconConsumer interface, which provides a callback
when the BeaconService is ready to use. Until this callback is made, ranging and monitoring
of beacons is not possible.
In the example below, an Activity implements the BeaconConsumer interface, binds
to the service, then when it gets the callback saying the service is ready, it starts ranging.
public class RangingActivity extends Activity implements BeaconConsumer {
protected static final String TAG = "RangingActivity";
private BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ranging);
beaconManager.bind(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
beaconManager.unbind(this);
}
@Override
public void onBeaconServiceConnect() {
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection beacons, Region region) {
if (beacons.size() > 0) {
Log.i(TAG, "The first beacon I see is about "+beacons.iterator().next().getDistance()+" meters away.");
}
}
});
try {
beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
| Modifier and Type | Class and Description |
|---|---|
class |
BeaconManager.ServiceNotDeclaredException |
| Modifier and Type | Field and Description |
|---|---|
protected static BeaconSimulator |
beaconSimulator |
protected RangeNotifier |
dataRequestNotifier |
static long |
DEFAULT_BACKGROUND_BETWEEN_SCAN_PERIOD
The default duration in milliseconds spent not scanning between each Bluetooth scan cycle when no ranging/monitoring clients are in the foreground
|
static long |
DEFAULT_BACKGROUND_SCAN_PERIOD
The default duration in milliseconds of the Bluetooth scan cycle when no ranging/monitoring clients are in the foreground
|
static long |
DEFAULT_EXIT_PERIOD
The default duration in milliseconds of region exit time
|
static long |
DEFAULT_FOREGROUND_BETWEEN_SCAN_PERIOD
The default duration in milliseconds spent not scanning between each Bluetooth scan cycle
|
static long |
DEFAULT_FOREGROUND_SCAN_PERIOD
The default duration in milliseconds of the Bluetooth scan cycle
|
protected static String |
distanceModelUpdateUrl |
protected Set<MonitorNotifier> |
monitorNotifiers |
protected Set<RangeNotifier> |
rangeNotifiers |
protected static Class |
rssiFilterImplClass
Default class for rssi filter/calculation implementation
|
protected static BeaconManager |
sInstance |
| Modifier | Constructor and Description |
|---|---|
protected |
BeaconManager(Context context) |
| Modifier and Type | Method and Description |
|---|---|
void |
addMonitorNotifier(MonitorNotifier notifier)
Specifies a class that should be called each time the
BeaconService sees or
stops seeing a Region of beacons. |
void |
addRangeNotifier(RangeNotifier notifier)
Specifies a class that should be called each time the
BeaconService gets ranging
data, which is nominally once per second when beacons are detected. |
void |
applySettings()
Call this method if you are running the scanner service in a different process in order to
synchronize any configuration settings, including BeaconParsers to the scanner
|
void |
bind(BeaconConsumer consumer)
Binds an Android
Activity or Service to the BeaconService. |
boolean |
checkAvailability()
Check if Bluetooth LE is supported by this Android device, and if so, make sure it is enabled.
|
protected void |
checkIfMainProcess() |
void |
disableForegroundServiceScanning()
Disables a foreground scanning service, if previously configured.
|
void |
enableForegroundServiceScanning(Notification notification,
int notificationId)
Configures the library to use a foreground service for bacon scanning.
|
long |
getBackgroundBetweenScanPeriod() |
boolean |
getBackgroundMode() |
long |
getBackgroundScanPeriod() |
List<BeaconParser> |
getBeaconParsers()
Gets a list of the active beaconParsers.
|
static BeaconSimulator |
getBeaconSimulator() |
protected RangeNotifier |
getDataRequestNotifier() |
static String |
getDistanceModelUpdateUrl() |
long |
getForegroundBetweenScanPeriod() |
long |
getForegroundScanPeriod() |
Notification |
getForegroundServiceNotification() |
int |
getForegroundServiceNotificationId() |
static BeaconManager |
getInstanceForApplication(Context context)
An accessor for the singleton instance of this class.
|
static boolean |
getManifestCheckingDisabled()
Returns whether manifest checking is disabled
|
Collection<Region> |
getMonitoredRegions() |
MonitorNotifier |
getMonitoringNotifier()
Deprecated.
replaced by (@link #getMonitorNotifiers)
|
Set<MonitorNotifier> |
getMonitoringNotifiers()
Read-only access to the registered
MonitorNotifier instances |
NonBeaconLeScanCallback |
getNonBeaconLeScanCallback() |
Collection<Region> |
getRangedRegions() |
RangeNotifier |
getRangingNotifier()
Deprecated.
replaced by (@link #getRangeNotifiers)
|
Set<RangeNotifier> |
getRangingNotifiers()
Read-only access to the registered
RangeNotifier instances |
static long |
getRegionExitPeriod()
Get region exit milliseconds
|
static Class |
getRssiFilterImplClass() |
boolean |
getScheduledScanJobsEnabled() |
static boolean |
isAndroidLScanningDisabled()
Determines if Android L Scanning is disabled by user selection
|
boolean |
isAnyConsumerBound()
Tells you if the any beacon consumer is bound to the service
|
boolean |
isBackgroundModeUninitialized() |
boolean |
isBound(BeaconConsumer consumer)
Tells you if the passed beacon consumer is bound to the service
|
boolean |
isMainProcess()
Determines if this BeaconManager instance is associated with the main application process that
hosts the user interface.
|
boolean |
isRegionStatePersistenceEnabled()
Indicates whether region state preservation is enabled
|
boolean |
isScannerInDifferentProcess()
Determines if this BeaconManager instance is not part of the process hosting the beacon scanning
service.
|
static void |
logDebug(String tag,
String message)
Deprecated.
This will be removed in a later release. Use
LogManager.d(String, String, Object...) instead. |
static void |
logDebug(String tag,
String message,
Throwable t)
Deprecated.
This will be removed in a later release. Use
LogManager.d(Throwable, String, String, Object...)
instead. |
void |
notifyException() |
void |
removeAllMonitorNotifiers()
Remove all the Monitor Notifiers.
|
void |
removeAllRangeNotifiers()
Remove all the Range Notifiers.
|
boolean |
removeMonitoreNotifier(MonitorNotifier notifier)
Deprecated.
Misspelled. Replaced by
removeMonitorNotifier(org.altbeacon.beacon.MonitorNotifier) |
boolean |
removeMonitorNotifier(MonitorNotifier notifier)
Specifies a class to remove from the array of
MonitorNotifier. |
boolean |
removeRangeNotifier(RangeNotifier notifier)
Specifies a class to remove from the array of
RangeNotifier |
void |
requestStateForRegion(Region region)
Requests the current in/out state on the specified region.
|
static void |
setAndroidLScanningDisabled(boolean disabled)
Allows disabling use of Android L BLE Scanning APIs on devices with API 21+
If set to false (default), devices with API 21+ will use the Android L APIs to
scan for beacons
|
void |
setBackgroundBetweenScanPeriod(long p)
Sets the duration in milliseconds spent not scanning between each Bluetooth LE scan cycle when no ranging/monitoring clients are in the foreground
|
void |
setBackgroundMode(boolean backgroundMode)
This method notifies the beacon service that the application is either moving to background
mode or foreground mode.
|
void |
setBackgroundScanPeriod(long p)
Sets the duration in milliseconds of each Bluetooth LE scan cycle to look for beacons.
|
static void |
setBeaconSimulator(BeaconSimulator beaconSimulator) |
protected void |
setDataRequestNotifier(RangeNotifier notifier) |
static void |
setDebug(boolean debug)
Set to true if you want to show library debugging.
|
static void |
setDistanceModelUpdateUrl(String url) |
void |
setEnableScheduledScanJobs(boolean enabled)
Configures using a `ScanJob` run with the `JobScheduler` to perform scans rather than using a
long-running `BeaconService` to do so.
|
void |
setForegroundBetweenScanPeriod(long p)
Sets the duration in milliseconds between each Bluetooth LE scan cycle to look for beacons.
|
void |
setForegroundScanPeriod(long p)
Sets the duration in milliseconds of each Bluetooth LE scan cycle to look for beacons.
|
static void |
setManifestCheckingDisabled(boolean disabled)
Allows disabling check of manifest for proper configuration of service.
|
void |
setMaxTrackingAge(int maxTrackingAge)
Set the period of time, in which a beacon did not receive new
measurements
|
void |
setMonitorNotifier(MonitorNotifier notifier)
Deprecated.
|
void |
setNonBeaconLeScanCallback(NonBeaconLeScanCallback callback) |
void |
setRangeNotifier(RangeNotifier notifier)
Deprecated.
replaced by (@link #addRangeNotifier)
|
static void |
setRegionExitPeriod(long regionExitPeriod)
Set region exit period in milliseconds
|
void |
setRegionStatePeristenceEnabled(boolean enabled)
Deprecated.
Misspelled. Replaced by
setRegionStatePersistenceEnabled(boolean) |
void |
setRegionStatePersistenceEnabled(boolean enabled)
Turns off saving the state of monitored regions to persistent storage so it is retained over
app restarts.
|
static void |
setRssiFilterImplClass(Class c) |
void |
setScannerInSameProcess(boolean isScanner)
Reserved for internal use by the library.
|
void |
setSdkNotifier(SDKNotifier sdkNotifier) |
static void |
setsManifestCheckingDisabled(boolean disabled)
Deprecated.
|
static void |
setUseTrackingCache(boolean useTrackingCache)
Allow the library to use a tracking cache
|
void |
startMonitoringBeaconsInRegion(List<Region> regions) |
void |
startMonitoringBeaconsInRegion(Region region)
Tells the
BeaconService to start looking for beacons that match the passed
Region object. |
void |
startRangingBeaconsInRegion(List<Region> regions) |
void |
startRangingBeaconsInRegion(Region region)
Tells the
BeaconService to start looking for beacons that match the passed
Region object, and providing updates on the estimated mDistance every seconds while
beacons in the Region are visible. |
void |
stopAllMonitoringBeaconsInRegion() |
void |
stopAllRangingBeaconsInRegion() |
void |
stopMonitoringBeaconsInRegion(List<Region> regions) |
void |
stopMonitoringBeaconsInRegion(Region region)
Tells the
BeaconService to stop looking for beacons that match the passed
Region object. |
void |
stopRangingBeaconsInRegion(List<Region> regions) |
void |
stopRangingBeaconsInRegion(Region region)
Tells the
BeaconService to stop looking for beacons that match the passed
Region object and providing mDistance information for them. |
protected void |
syncSettingsToService() |
void |
unbind(BeaconConsumer consumer)
Unbinds an Android
Activity or Service to the BeaconService. |
void |
updateScanPeriods()
Updates an already running scan with scanPeriod/betweenScanPeriod according to Background/Foreground state.
|
@Nullable protected static volatile BeaconManager sInstance
@NonNull protected final Set<RangeNotifier> rangeNotifiers
@Nullable protected RangeNotifier dataRequestNotifier
@NonNull protected final Set<MonitorNotifier> monitorNotifiers
public static final long DEFAULT_FOREGROUND_SCAN_PERIOD
public static final long DEFAULT_FOREGROUND_BETWEEN_SCAN_PERIOD
public static final long DEFAULT_BACKGROUND_SCAN_PERIOD
public static final long DEFAULT_BACKGROUND_BETWEEN_SCAN_PERIOD
public static final long DEFAULT_EXIT_PERIOD
@Nullable protected static BeaconSimulator beaconSimulator
protected static String distanceModelUpdateUrl
protected static Class rssiFilterImplClass
protected BeaconManager(@NonNull
Context context)
public static void setDebug(boolean debug)
debug - True turn on all logs for this library to be printed out to logcat. False turns
off detailed logging..
This is a convenience method that calls setLogger to a verbose logger and enables verbose
logging. For more fine grained control, use:
LogManager.setLogger(org.altbeacon.beacon.logging.Logger)
instead.public void setSdkNotifier(SDKNotifier sdkNotifier)
public void notifyException()
public void setForegroundScanPeriod(long p)
bind(org.altbeacon.beacon.BeaconConsumer) or when switching
between background/foreground. To have it effect on an already running scan (when the next
cycle starts), call updateScanPeriods()p - public void setForegroundBetweenScanPeriod(long p)
bind(org.altbeacon.beacon.BeaconConsumer) or when switching
between background/foreground. To have it effect on an already running scan (when the next
cycle starts), call updateScanPeriods()p - public void setBackgroundScanPeriod(long p)
bind(org.altbeacon.beacon.BeaconConsumer) or when switching
between background/foreground. To have it effect on an already running scan (when the next
cycle starts), call updateScanPeriods()p - public void setBackgroundBetweenScanPeriod(long p)
p - public static void setRegionExitPeriod(long regionExitPeriod)
regionExitPeriod - public static long getRegionExitPeriod()
@NonNull public static BeaconManager getInstanceForApplication(@NonNull Context context)
public boolean isMainProcess()
public boolean isScannerInDifferentProcess()
public void setScannerInSameProcess(boolean isScanner)
protected void checkIfMainProcess()
@NonNull public List<BeaconParser> getBeaconParsers()
public boolean checkAvailability()
throws BleNotAvailableException
BleNotAvailableException - if Bluetooth LE is not supported. (Note: The Android emulator will do this)public void bind(@NonNull
BeaconConsumer consumer)
Activity or Service to the BeaconService. The
Activity or Service must implement the beaconConsumer interface so
that it can get a callback when the service is ready to use.consumer - the Activity or Service that will receive the callback when the service is ready.public void unbind(@NonNull
BeaconConsumer consumer)
Activity or Service to the BeaconService. This should
typically be called in the onDestroy() method.consumer - the Activity or Service that no longer needs to use the service.public boolean isBound(@NonNull
BeaconConsumer consumer)
consumer - public boolean isAnyConsumerBound()
public void setBackgroundMode(boolean backgroundMode)
backgroundMode - true indicates the app is in the backgroundDEFAULT_FOREGROUND_SCAN_PERIOD,
#DEFAULT_FOREGROUND_BETWEEN_SCAN_PERIOD;,
#DEFAULT_BACKGROUND_SCAN_PERIOD;,
#DEFAULT_BACKGROUND_BETWEEN_SCAN_PERIOD;,
setForegroundScanPeriod(long p),
setForegroundBetweenScanPeriod(long p),
setBackgroundScanPeriod(long p),
setBackgroundBetweenScanPeriod(long p)public void setEnableScheduledScanJobs(boolean enabled)
enabled - public boolean getScheduledScanJobsEnabled()
public boolean getBackgroundMode()
public long getBackgroundScanPeriod()
public long getBackgroundBetweenScanPeriod()
public long getForegroundScanPeriod()
public long getForegroundBetweenScanPeriod()
public boolean isBackgroundModeUninitialized()
@Deprecated public void setRangeNotifier(@Nullable RangeNotifier notifier)
BeaconService gets ranging
data, which is nominally once per second when beacons are detected.
IMPORTANT: Only one RangeNotifier may be active for a given application. If two different
activities or services set different RangeNotifier instances, the last one set will receive
all the notifications.notifier - The RangeNotifier to register.RangeNotifierpublic void addRangeNotifier(@NonNull
RangeNotifier notifier)
BeaconService gets ranging
data, which is nominally once per second when beacons are detected.
Permits to register several RangeNotifier objects.
The notifier must be unregistered using (@link #removeRangeNotifier)notifier - The RangeNotifier to register.RangeNotifierpublic boolean removeRangeNotifier(@NonNull
RangeNotifier notifier)
RangeNotifiernotifier - The RangeNotifier to unregister.RangeNotifierpublic void removeAllRangeNotifiers()
@Deprecated public void setMonitorNotifier(@Nullable MonitorNotifier notifier)
addMonitorNotifier(org.altbeacon.beacon.MonitorNotifier)BeaconService sees
or stops seeing a Region of beacons.
IMPORTANT: Only one MonitorNotifier may be active for a given application. If two different
activities or services set different MonitorNotifier instances, the last one set will receive
all the notifications.notifier - The MonitorNotifier to register.MonitorNotifier,
startMonitoringBeaconsInRegion(Region),
Regionpublic void addMonitorNotifier(@NonNull
MonitorNotifier notifier)
BeaconService sees or
stops seeing a Region of beacons.
Permits to register several MonitorNotifier objects.
Unregister the notifier using removeMonitoreNotifier(org.altbeacon.beacon.MonitorNotifier)notifier - The MonitorNotifier to register.MonitorNotifier,
startMonitoringBeaconsInRegion(Region),
Region@Deprecated public boolean removeMonitoreNotifier(@NonNull MonitorNotifier notifier)
removeMonitorNotifier(org.altbeacon.beacon.MonitorNotifier)public boolean removeMonitorNotifier(@NonNull
MonitorNotifier notifier)
MonitorNotifier.notifier - The MonitorNotifier to unregister.MonitorNotifier,
startMonitoringBeaconsInRegion(Region),
Regionpublic void removeAllMonitorNotifiers()
@Deprecated public void setRegionStatePeristenceEnabled(boolean enabled)
setRegionStatePersistenceEnabled(boolean)public void setRegionStatePersistenceEnabled(boolean enabled)
enabled - true to enable the region state persistence, false to disable it.public boolean isRegionStatePersistenceEnabled()
public void requestStateForRegion(@NonNull
Region region)
region - public void startRangingBeaconsInRegion(@NonNull
List<Region> regions)
throws RemoteException
RemoteExceptionpublic void stopRangingBeaconsInRegion(@NonNull
List<Region> regions)
throws RemoteException
RemoteExceptionpublic void stopAllRangingBeaconsInRegion()
throws RemoteException
RemoteExceptionpublic void startRangingBeaconsInRegion(@NonNull
Region region)
throws RemoteException
BeaconService to start looking for beacons that match the passed
Region object, and providing updates on the estimated mDistance every seconds while
beacons in the Region are visible. Note that the Region's unique identifier must be retained to
later call the stopRangingBeaconsInRegion method.region - RemoteExceptionsetRangeNotifier(RangeNotifier),
stopRangingBeaconsInRegion(Region region),
RangeNotifier,
Regionpublic void stopRangingBeaconsInRegion(@NonNull
Region region)
throws RemoteException
BeaconService to stop looking for beacons that match the passed
Region object and providing mDistance information for them.region - RemoteExceptionsetMonitorNotifier(MonitorNotifier notifier),
startMonitoringBeaconsInRegion(Region region),
MonitorNotifier,
Regionpublic void applySettings()
isScannerInDifferentProcess()protected void syncSettingsToService()
public void startMonitoringBeaconsInRegion(@NonNull
List<Region> regions)
throws RemoteException
RemoteExceptionpublic void stopMonitoringBeaconsInRegion(@NonNull
List<Region> regions)
throws RemoteException
RemoteExceptionpublic void stopAllMonitoringBeaconsInRegion()
throws RemoteException
RemoteExceptionpublic void startMonitoringBeaconsInRegion(@NonNull
Region region)
throws RemoteException
BeaconService to start looking for beacons that match the passed
Region object. Note that the Region's unique identifier must be retained to
later call the stopMonitoringBeaconsInRegion method.region - RemoteExceptionsetMonitorNotifier(MonitorNotifier),
stopMonitoringBeaconsInRegion(Region region),
MonitorNotifier,
Regionpublic void stopMonitoringBeaconsInRegion(@NonNull
Region region)
throws RemoteException
BeaconService to stop looking for beacons that match the passed
Region object. Note that the Region's unique identifier is used to match it to
an existing monitored Region.region - RemoteExceptionsetMonitorNotifier(MonitorNotifier),
startMonitoringBeaconsInRegion(Region region),
MonitorNotifier,
Regionpublic void updateScanPeriods()
throws RemoteException
RemoteException - - If the BeaconManager is not bound to the service.@Deprecated @Nullable public MonitorNotifier getMonitoringNotifier()
@NonNull public Set<MonitorNotifier> getMonitoringNotifiers()
MonitorNotifier instances
This provides a thread-safe "read-only" view of the Set of registered monitor
notifiers. Attempts to modify the returned set, or its iterator, will throw an
UnsupportedOperationException. Modifications to the underlying set should be made
through addMonitorNotifier(MonitorNotifier) and
removeMonitorNotifier(MonitorNotifier).
MonitorNotifier instancesaddMonitorNotifier(MonitorNotifier),
removeMonitorNotifier(MonitorNotifier),
Collections.unmodifiableSet(Set)@Deprecated @Nullable public RangeNotifier getRangingNotifier()
@NonNull public Set<RangeNotifier> getRangingNotifiers()
RangeNotifier instances
This provides a thread-safe "read-only" view of the Set of registered range
notifiers. Attempts to modify the returned set, or its iterator, will throw an
UnsupportedOperationException. Modifications to the underlying set should be made
through addRangeNotifier(RangeNotifier) and
removeRangeNotifier(RangeNotifier).
RangeNotifier instancesaddRangeNotifier(RangeNotifier),
removeRangeNotifier(RangeNotifier),
Collections.unmodifiableSet(Set)@NonNull public Collection<Region> getMonitoredRegions()
@NonNull public Collection<Region> getRangedRegions()
@Deprecated public static void logDebug(String tag, String message)
LogManager.d(String, String, Object...) instead.tag - message - @Deprecated public static void logDebug(String tag, String message, Throwable t)
LogManager.d(Throwable, String, String, Object...)
instead.tag - message - t - public static String getDistanceModelUpdateUrl()
public static void setDistanceModelUpdateUrl(@NonNull
String url)
public static void setRssiFilterImplClass(@NonNull
Class c)
public static Class getRssiFilterImplClass()
public static void setUseTrackingCache(boolean useTrackingCache)
useTrackingCache - public void setMaxTrackingAge(int maxTrackingAge)
maxTrackingAge - in millisecondspublic static void setBeaconSimulator(BeaconSimulator beaconSimulator)
@Nullable public static BeaconSimulator getBeaconSimulator()
protected void setDataRequestNotifier(@Nullable
RangeNotifier notifier)
@Nullable protected RangeNotifier getDataRequestNotifier()
@Nullable public NonBeaconLeScanCallback getNonBeaconLeScanCallback()
public void setNonBeaconLeScanCallback(@Nullable
NonBeaconLeScanCallback callback)
public static boolean isAndroidLScanningDisabled()
public static void setAndroidLScanningDisabled(boolean disabled)
disabled - @Deprecated public static void setsManifestCheckingDisabled(boolean disabled)
disabled - setManifestCheckingDisabled(boolean)public static void setManifestCheckingDisabled(boolean disabled)
disabled - public static boolean getManifestCheckingDisabled()
public void enableForegroundServiceScanning(Notification notification, int notificationId) throws IllegalStateException
notification - - the notification that will be displayed when beacon scanning is active,
along with the icon that shows up in the status bar.IllegalStateException - if called after consumers are already bound to the scanning
servicesetForegroundScanPeriod(long),
This method requires a notification to display a message to the user about why the app is
scanning in the background. The notification must include an icon that will be displayed
in the top bar whenever the scanning service is running.
If the BeaconService is configured to run in a different process, this call will have no
effect.public void disableForegroundServiceScanning()
throws IllegalStateException
IllegalStateException - if called after consumers are already bound to the scanning
serviceIn order to call this method to disable a foreground service, you must unbind from the
BeaconManager. You can then rebind after this call is made.public Notification getForegroundServiceNotification()
enableForegroundServiceScanning(android.app.Notification, int)public int getForegroundServiceNotificationId()
enableForegroundServiceScanning(android.app.Notification, int)