Android library scanning BLE (Bluetooth Low Energy) beacons nearby with RxJava
Library was tested with Estimote and Kontakt beacons.
This library has limited functionality, but its API is very simple and has just four methods:
ReactiveBeacons(context)
boolean isBleSupported()
void requestBluetoothAccessIfDisabled(activity)
Observable<Beacon> observe()
JavaDoc is available at: http://pwittchen.github.io/ReactiveBeacons/
min SDK = 9, but if you are using API level lower than 18, don't forget to check BLE support on the device.
Initialize ReactiveBeacons
object:
private ReactiveBeacons reactiveBeacons;
@Override protected void onCreate(Bundle savedInstanceState) {
reactiveBeacons = new ReactiveBeacons(this);
}
Create subscribtion:
private Subscription subscription;
@Override protected void onResume() {
super.onResume();
if (!reactiveBeacons.isBleSupported()) { // optional, but recommended step
// show message for the user that BLE is not supported on the device
} else {
// optionally, we can request Bluetooth Access
reactiveBeacons.requestBluetoothAccessIfDisabled(this);
}
subscription = reactiveBeacons.observe()
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Action1<Beacon>() {
@Override public void call(Beacon beacon) {
// do something with beacon
}
});
}
Unsubscribe subscription in onPause()
method to stop BLE scan.
@Override protected void onPause() {
super.onPause();
subscription.unsubscribe();
}
Please note: Library may emit information about the same beacon multiple times. New emission is created everytime when RSSI changes. We can distinguish several beacons by their MAC addresses with beacon.device.getAddress()
method.
Add <uses-feature .../>
tag inside <manifest ...>
tag in AndroidManifest.xml
file in your application if you support Android devices with API level 18 or higher. You can skip this, if you are supporting lower API levels.
<uses-feature
android:name="android.hardware.bluetooth_le"
android:required="true" />
Check BLE support if you are supporting devices with API level lower than 18.
if (!reactiveBeacons.isBleSupported()) {
// show message for the user that BLE is not supported on the device
}
If BLE is not supported, Observable emitting Beacons will be always empty.
Use requestBluetoothAccessIfDisabled(context)
method to ensure that Bluetooth is enabled.
If you are supporting devices with API level lower than 18, you don't have to request Bluetooth access every time.
if (!reactiveBeacons.isBleSupported()) {
// show message for the user that BLE is not supported on the device
} else {
reactiveBeacons.requestBluetoothAccessIfDisabled(context);
}
Exemplary application is located in app
directory of this repository.
Beacon
class represents BLE beacon and has the following attributes:
BluetoothDevice device;
int rssi;
byte[] scanRecord;
int txPower;
All of the elements are assigned dynamically, but txPower
has default value equal to -59
.
It works quite fine for different types of beacons.
Beacon class has also getDistance()
method, which returns distance from mobile device to beacon in meters and getProximity()
method, which returns Proximity
value.
Proximity
can be as follows:
IMMEDIATE
- from 0m to 1mNEAR
- from 1m to 3mFAR
- more than 3m
Beacon class has also static create(...)
method responsible for creating Beacon objects.
Filter
class provides static filtering methods, which can be used with RxJava filter(...)
method inside specific subscription.
Currently the following filters are available:
proximityIsEqualTo(Proximity)
proximityIsNotEqualTo(Proximity)
distanceIsEqualTo(double)
distanceIsGreaterThan(double)
distanceIsLowerThan(double)
hasName(String)
hasMacAddress(String)
Of course, we can create our own custom filters, which are not listed above if we need to.
Exemplary usage
In the example below, we are filtering all Beacons with Proximity
equal to NEAR
value.
reactiveBeacons.observe()
.filter(Filter.proximityIsEqualTo(Proximity.NEAR))
.subscribe(new Action1<Beacon>() {
@Override public void call(Beacon beacon) {
beacons.put(beacon.device.getAddress(), beacon);
refreshBeaconList();
}
});
You can depend on the library through Maven:
<dependency>
<groupId>com.github.pwittchen</groupId>
<artifactId>reactivebeacons</artifactId>
<version>0.2.0</version>
</dependency>
or through Gradle:
dependencies {
compile 'com.github.pwittchen:reactivebeacons:0.2.0'
}
Code style used in the project is called SquareAndroid
from Java Code Styles repository by Square available at: https://github.com/square/java-code-styles. Currently, library doesn't have checkstyle verification attached. It can be done in the future.
- Bluetooth Low Energy on Wikipedia
- android-bluetooth-demo repository
- Converting callbacks to RxJava Observables
- Transmission power range and RSSI
- What are Broadcasting Power, RSSI and other characteristics of beacon's signal?
- Estimating beacon proximity/distance based on RSSI - Bluetooth LE
- RSSI (Received Signal Strength Indication) on Wikipedia
- Specification for Eddystone, an open beacon format from Google
Copyright 2015 Piotr Wittchen
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.