同步操作将从 klzhong/Android-BLE-Library 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
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.
New features added in version 2.3:
:ble-ktx
module added with support for coroutines and Flow.
.suspend()
methods added in Request
s.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.onServicesChanged()
callback, added in API 31 (Android 12).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()
This is still experimental feature and any feedback is welcome.
New features added in version 2.2:
Version 2.2 breaks some API known from version 2.1.1. Check out migration guide.
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.3.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.3.1'
To import the BLE library with set of parsers for common Bluetooth SIG characteristics, use:
implementation 'no.nordicsemi.android:ble-common:2.3.1'
For more information, read this.
An extension for easier integration with LiveData
is available after adding:
implementation 'no.nordicsemi.android:ble-livedata:2.3.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 ble module as a dependency to your project:
include ':ble'
project(':ble').projectDir = file('../Android-BLE-Library/ble')
implementation project(':ble')
inside dependencies.You may do the same with other modules available in this project. Keep in mind, that ble-livedata module requires Kotlin, but no special changes are required in the app.
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"
}
A 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.
BleManager
may be used in different ways:
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).
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.
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.
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.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。