android-ble-rs
Android Bluetooth API wrapper, currently supporting BLE client role operations.
A few portions of the code (especially L2capChannel) is orginally written by
Dirbaio.
Version 0.1.x of this crate is supposed to be API-compatible with version 0.6.x of
the bluest library. In fact, some type definitions
are copied from bluest. Anything incompatible with bluest in the API may be reported as a bug.
This crate uses ndk_context, which is automatically initialized in android_activity.
Test
Make sure the Android SDK, NDK, Rust target aarch64-linux-android and
cargo-apk are installed.
Note that cargo-apk2 can also be used.
Create android-ble-test according to the template provided below, and build it with cargo apk build -r.
Note: -r means building for the release profile, which produces a much smaller package.
Install the target/release/apk/android-ble-test.apk on the Android device, and enable permissions
manually on the device.
Start the android-ble-test on the device, then check the log output with adb logcat android_ble_test:D '*:S'.
cargo-apk template
Cargo.toml:
[]
= "android-ble-test"
= "0.1.0"
= "2024"
= false
[]
= "0.4"
= "0.1"
= "0.15.1"
= "0.1.1"
= { = "0.6", = ["native-activity"] }
# jni-min-helper = { version = "0.3", features = ["futures"] }
= "2.6"
= "2.2.0"
[]
= ["cdylib"]
[]
= "com.example.android_ble_test"
= ["aarch64-linux-android"]
# For `cargo-apk2`:
# put <https://docs.rs/crate/jni-min-helper/0.3.2/source/java/PermActivity.java> in this folder
# java_sources = "java"
# Android 12 or above may require runtime permission request. Use `cargo-apk2` for performing this.
# <https://developer.android.com/develop/connectivity/bluetooth/bt-permissions>
# <https://docs.rs/jni-min-helper/0.3.2/jni_min_helper/struct.PermissionRequest.html>
[]
= 23
= 33
[[]]
= "android.hardware.bluetooth_le"
= true
[[]]
= "android.permission.BLUETOOTH_SCAN"
= 31
[[]]
= "android.permission.BLUETOOTH_CONNECT"
= 31
[[]]
= "android.permission.ACCESS_FINE_LOCATION"
# TODO: uncomment this line when `usesPermissionFlags` becomes supported in `cargo-apk2`.
# max_sdk_version = 30
[[]]
= "android.permission.BLUETOOTH"
= 30
[[]]
= "android.permission.BLUETOOTH_ADMIN"
= 30
# configurations below are for `cargo-apk2`
# [[package.metadata.android.application.activity]]
# name = "android.app.NativeActivity"
# [[package.metadata.android.application.activity.intent_filter]]
# actions = ["android.intent.action.VIEW", "android.intent.action.MAIN"]
# categories = ["android.intent.category.LAUNCHER"]
# [[package.metadata.android.application.activity.meta_data]]
# name = "android.app.lib_name"
# value = "android_ble_test"
# [[package.metadata.android.application.activity]]
# name = "rust.jniminhelper.PermActivity"
src/lib.rs:
use android_ble as bluest;
use ;
use ;
use info;
// Please put your new test case here.
async