An Android library that solves a lot of Android's Bluetooth Low Energy problems. The BleManager class exposes high level API for connecting and communicating with Bluetooth LE peripherals. The API is clean and easy to read.
BleManager class provides the following features:
The library does not provide support for scanning for Bluetooth LE devices. For scanning, we recommend using Android Scanner Compat Library which brings almost all recent features, introduced in Lollipop and later, to the older platforms.
The library may be found on jcenter and Maven Central repository. Add it to your project by adding the following dependency:
implementation 'no.nordicsemi.android:ble:2.1.1'
The last version not migrated to AndroidX is 2.0.5.
Clone this project and add ble module as a dependency to your project:
include ':ble'
project(':ble').projectDir = file('../Android-BLE-Library/ble')
implementation project(':ble')
inside dependencies.BleManager
may be used for a single connection
(see nRF Toolbox -> RSC profile)
or when multiple connections are required (see nRF Toolbox -> Proximity profile),
from a Service (see nRF Toolbox -> RSC profile), ViewModel's repo
(see Architecture Components
and nRF Blinky),
or as a singleton (not recommended, see nRF Toolbox -> HRM).
A single BleManager
instance is responsible for connecting and communicating with a single peripheral.
Multiple manager instances are allowed. Extend BleManager
with you manager where you define the
high level device's API.
writeCharacteristic(...)
, etc.) return the Request
class now,
instead of boolean.onLinklossOccur
callback has been renamed to onLinkLossOccurred
.onCharacteristicRead
, onCharacteristicNotified
, etc.) inside
BleManagerGattCallback
has been deprecated. Use Request
callbacks instead.onBondingFailed
has been added to BleManagerCallbacks
.shouldAutoConnect()
has ben deprecated, use useAutoConnect(boolean)
in ConnectRequest
instead.BluetoothGattCallback
is required
in order to perform the next operation.RequestQueue
and ReliableWriteRequest
are supported.initGatt(BluetoothGatt)
with initialize()
:Old code:
@Override
protected Deque<Request> initGatt(final BluetoothGatt gatt) {
final LinkedList<Request> requests = new LinkedList<>();
requests.add(Request.newEnableNotificationsRequest(characteristic));
return requests;
}
New code:
@Override
protected void initialize() {
setNotificationCallback(characteristic)
.with(new DataReceivedCallback() {
@Override
public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
...
}
});
enableNotifications(characteristic)
.enqueue();
}
See changes in Android nRF Toolbox and Android nRF Blinky for more examples.
Remember to call .enqueue()
method for initialization requests!
Connect's completion callback is called after the initialization is done (without or with errors).
BleManagerGattCallback
to request callbacks.DataReceivedCallback
interface in a class
where your parse your data, and return higher-level values. For a sample, check out nRF Toolbox
and Android BLE Common Library.
If you are depending on a SIG adopted profile, like Heart Rate Monitor, Proximity, etc.,
feel free to include the BLE Common Library in your project.
It has all the parsers implemented. If your profile isn't there, we are happy to accept PRs.connect()
and disconnect()
methods also require calling .enqueue()
in asynchronous use.shouldAutoConnect()
method in the manager with connect(device).useAutConnect(true).enqueue()/await()
.The new version is compatible with nRF Toolbox and BLE Common Library. The latter one is a set of useful parsers and callbacks for common Bluetooth SIG adopted profiles.
The libraries are available on jcenter, but if you need to make some changes, clone all 3 projects, ensure the path to :ble and :ble-common modules are correct in settings.gradle file, and sync the project.
Find the simple example here Android nRF Blinky.
For an example how to use it from an Activity or a Service, check the base Activity and Service classes in nRF Toolbox.
BleManagerCallbacks
:
exampleBleManager
class and implement required methods:
exampleThe BLE library v 1.x is no longer supported. Please migrate to 2.x for bug fixing releases. Find it on version/1x branch.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。