Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
A library that makes working with Bluetooth LE on Android a pleasure. Seriously.
License
NordicSemiconductor/Android-BLE-Library
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
An Android library that solves a lot of Android’s Bluetooth Low Energy problems.
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.6.1'
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.6.1'
To import the BLE library with set of parsers for common Bluetooth SIG characteristics, use:
implementation 'no.nordicsemi.android:ble-common:2.6.1'
For more information, read this.
An extension for easier integration with LiveData is available after adding:
implementation 'no.nordicsemi.android:ble-livedata:2.6.1'
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:
if (file('../Android-BLE-Library').exists()) < includeBuild('../Android-BLE-Library') >
The library uses Java 1.8 features. If you’re using Android Studio below 4.2, make sure your build.gradle includes the following configuration:
compileOptions < sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 > // For Kotlin projects additionally: kotlinOptions < jvmTarget = "1.8" >
BleManager class provides the following features:
- Connection, with automatic retries
- Service discovery
- Bonding (optional) and removing bond information (using reflections)
- Automatic handling of Service Changed indications
- Device initialization
- Asynchronous and synchronous BLE operations using queue
- Splitting and merging long packets when writing and reading characteristics and descriptors
- Requesting MTU and connection priority (on Android Lollipop or newer)
- Reading and setting preferred PHY (on Android Oreo or newer)
- Reading RSSI
- Refreshing device cache (using reflections)
- Reliable Write support
- Operation timeouts (for connect, disconnect and wait for notification requests)
- Error handling
- Logging
- GATT server (since version 2.2)
- Kotlin support (coroutines, Flow, . ) (since version 2.3)
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:
- getGattCallback() method has been deprecated. Instead, simply move the inner methods to the BleManager class. See this PR.
- Support for server only implementation using attachClientConnection(BluetoothDevice) . Call it instead of connect(BluetoothDevice) to use the device as server-only.
- Data provider for read requests (server side) was added.
- Cancellation support for flows and suspended methods added to :ble-kts .
- More :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.
- Progress indications for split outgoing data and merged incoming data can be observed as Flow using splitWithProgressFlow(. ) and mergeWithProgressFlow(. ) .
- :ble-ktx module added with support for coroutines and Flow.
- .suspend() methods added in Request s.
- asFlow() method added to ValueChangedCallback .
- Connection and bonding state available as Flow.
- New helper methods to get a BluetoothGattCharacteristic with given required properties and instance id added to BluetoothGattService .
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()
- GATT Server support. This includes setting up the local GATT server on the Android device, new requests for server operations:
- wait for read,
- wait for write,
- send notification,
- send indication,
- set characteristic value,
- set descriptor value.
- New conditional requests:
- wait if,
- wait until.
- BLE operations are no longer called from the main thread.
- There’s a new option to set a handler for invoking callbacks. A handler can also be set per-callback.
Version 2.2 breaks some API known from version 2.1.1. Check out migration guide.
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.
About
A library that makes working with Bluetooth LE on Android a pleasure. Seriously.