Demo app using btleplug for BLE communication for android
android - Demo app with 2 buttons. On clicking scan it will call function from rust library to start scanning bluethooth devices.
rust-android - Using jni to build dependencies.
rust-core - My rust library that uses btleplug for implementing BLE communication related functions.
scripts/install-android.bat will build the libraries and copy the SO files for android.
- Installation process
- Install Node.js
- Install Rust
- Android Studio
- Install Android NDK - download android-ndk-r22b-linux-x86_64.zip and unzip it e.g.
NOTE: at time of writing you needed to use android-ndk-r22b or
earlier, because later versions fail with an error like
ld: error: unable to find library -lgcc
. See
rust-lang #85806 for more.
- Configure Rust for cross compilation:
rustup target add aarch64-linux-android
rustup target add x86_64-linux-android
(Note: aarch64
is for most physical Android devices, but x86_64
is useful
for running an Android emulator on a PC. You can also add i686
if you use
32-bit emulators.)
Set linker to build so. files. Replace the path in rust-android/.cargo/config.toml
.
[target.aarch64-linux-android]
linker = "NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android30-clang.cmd"
[target.x86_64-linux-android]
linker = "NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-linux-android30-clang"
Replacing NDK_HOME with something like /path/to/android-ndk-r22b/
.
- Software dependencies
https://crates.io/crates/btleplug - Async Rust BLE library Due to requiring a hybrid Rust/Java build, btleplug for Android requires android\libs\droidplug-debug.aar generated by locally builing btleplug. (https://github.com/deviceplug/btleplug#android)
- Build shared object:
npm run build
This will create automatically...
./rust-android/target/aarch64-linux-android/release/libmylib.so
./rust-android/target/armv7-linux-androideabi/release/libmylib.so
./rust-android/target/i686-linux-android/release/libmylib.so
./rust-android/target/x86_64-linux-android/release/libmylib.so
(Plus some other files we don't need.)
...and copy the shared libraries into your Android project
./android/app/src/main/jniLibs/*