一款库,让在 Android 上使用 Bluetooth LE 变得愉快。真的。
An Android library that solves a lot of Android's Bluetooth Low Energy problems.
[!Note] We're working on a new version of Bluetooth LE library, which eventually will replace this one: Kotlin BLE Library / version 2.0. Mind, that version 2 is not backward compatible version 1 or with this library.
Currently, the library is in the early development stage. It is not recommended for production use, but we encourage you to try it out and provide feedback.
The new library offers the following improvements over this one:
- 100% Kotlin code
- Scanning for Bluetooth LE devices (scan results as
Flow)- Support for Bluetooth LE advertising
- Support for mocking BLE devices (easy testing)
- All BLE operations use
suspendmodifier orFlowsRead more on project's Readme file.
The library may be found on Maven Central repository. Add it to your project by adding the following dependency:
implementation 'no.nordicsemi.android:ble:2.11.0'
The last version not migrated to AndroidX is 2.0.5.
BLE library with Kotlin extension is available in:
implementation 'no.nordicsemi.android:ble-ktx:2.11.0'
To import the BLE library with set of parsers for common Bluetooth SIG characteristics, use:
implementation 'no.nordicsemi.android:ble-common:2.11.0'
For more information, read this.
An extension for easier integration with LiveData is available after adding:
implementation 'no.nordicsemi.android:ble-livedata:2.11.0'
This extension adds ObservableBleManager with state and bondingState properties, which
notify about connection and bond state using androidx.lifecycle.LiveData.
Clone this project and add it to your project:
In settings.gradle file add the following lines:
if (file('../Android-BLE-Library').exists()) {
includeBuild('../Android-BLE-Library')
}
Sync project and build it.
The library uses Java 1.18 features. If you're using Android Studio below Giraffe, make sure your build.gradle includes the following configuration:
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_17
targetCompatibility JavaVersion.VERSION_1_17
}
// For Kotlin projects additionally:
kotlinOptions {
jvmTarget = "1.17"
}
BleManager class provides the following features:
Note: The library does not provide support for scanning for Bluetooth LE devices. Instead, we recommend using Android Scanner Compat Library which brings almost all recent features, introduced in Lollipop and later, to the older platforms.
See Releases for details. Below is short summary:
Version 2.7getGattCallback() method has been deprecated. Instead, simply move the inner methods to the
BleManager class. See this PR.attachClientConnection(BluetoothDevice). Call it instead of
connect(BluetoothDevice) to use the device as server-only.:ble-kts.:ble-ktx extensions..suspendForResponse() and .suspendForValidResponse() extension methods added to read and write requests..asResponseFlow() and .asValidResponseFlow() methods added to ValueChangedCallback..stateAsFlow() and .bondingStateAsFlow() in BleManager now return the same flow when called multiple times.Flow using
splitWithProgressFlow(...) and mergeWithProgressFlow(...).then(...) method added to ValueChangedCallback which will be called when the callback has been unregistered,
or the device has invalidated services (i.e. it has disconnected). Useful to release resources.Request using setHandler(null), which will make the callbacks
called immediately from the worker looper.Log.INFO or higher will now be logged. Use
getMinLogPriority() to return a different value if needed. Logs with lower priority will not be produced
at all, making the library less laggy (parsing incoming data to hex takes notable time).Data.FORMAT_xxx_BE types. Also, FORMAT_xxx have been
deprecated in favor of FORMAT_xxx_LE.before, with, then, fail, ...) are now wrapped in try-catch blocks.:ble-ktx module added with support for coroutines and Flow.
.suspend() methods added in Requests.asFlow() method added to ValueChangedCallback.BluetoothGattCharacteristic with given required properties
and instance id added to BluetoothGattService.JsonMerger class added, which should help with use cases when a device sends a JSON file in multiple
packets.
:ble-livedata migrated to Java with some API changes, as sealed classes are no longer available.
Support for new onServicesChanged() callback, added in API 31 (Android 12).
Option to cancel pending Bluetooth LE connection using ConnectRequest.cancelPendingConnection().
When using coroutines, use .suspend() method in Request, instead of enqueue() or await().
To register to notifications and indications (or incoming write requests for server) use
setNotificationCallback(characteristic)
.merge(JsonMerger()) // Example of how to use JsonMerger, optional
.asFlow()
Version 2.2 breaks some API known from version 2.1.1. Check out migration guide.
See Usage for more details.
The Trivia game is an example demonstrating client and server.
Please refer to the examples/ble-gatt-client folder for a project that illustrates the GATT
server provided as a foreground service. There's a simple UI with a text field to update
with the value of a characteristic that can be read and subscribed to. This characteristic also
demands encryption as an illustration of best-practice.
You can run this client on one device and a complimenting server on another (see the next section).
Note: This project is not maintained actively. It is provided as an example only and may not be migrated to latest version.
Starting from version 2.2 you may now define and use the GATT server in the BLE Library.
Please refer to the examples/ble-gatt-server folder for a project that illustrates the GATT
server provided as a foreground service. There's a simple UI with a text field to update
the value of a characteristic that can be read and subscribed to. This characteristic also
demands encryption as an illustration of best-practice.
Note: This project is not maintained actively. It is provided as an example only and may not be migrated to latest version.
Find the simple example here Android nRF Blinky.
For an example how to use it from a ViewModel or a Service, check
nRF Toolbox.
The BLE library v 1.x is no longer supported. Please migrate to 2.x for bug fixing releases. Find it on version/1x branch.
A migration guide is available here.
暂无开放 Issues,或尚未同步最近议题。