btleplug is an async Rust BLE library, supporting Windows 10, macOS, Linux, iOS, and Android (including Flutter, see below for more info).
It grew out of several earlier abandoned libraries for various platforms (rumble, blurmac, etc...), with the goal of building a fully cross platform library. Adding support for other platforms such as Android is planned.
btleplug is meant to be host/central mode only. If you are interested in peripheral BTLE (i.e. acting like a Bluetooth LE device instead of connecting to one), check out bluster.
This library DOES NOT SUPPORT BLUETOOTH 2/CLASSIC. There are no plans to add BT2/Classic support.
Feature | Windows | MacOS / iOS | Linux | Android |
---|---|---|---|---|
Bring Up Adapter | X | X | X | X |
Handle Multiple Adapters | X | |||
Discover Devices | X | X | X | X |
└ Discover Services | X | X | X | X |
└ Discover Characteristics | X | X | X | X |
└ Discover Descriptors | X | X | X | X |
└ Discover Name | X | X | X | X |
└ Discover Manufacturer Data | X | X | X | X |
└ Discover Service Data | X | X | X | X |
└ Discover MAC address | X | X | X | |
GATT Server Connect | X | X | X | X |
GATT Server Connect Event | X | X | X | X |
GATT Server Disconnect | X | X | X | X |
GATT Server Disconnect Event | X | X | X | X |
Write to Characteristic | X | X | X | X |
Read from Characteristic | X | X | X | X |
Subscribe to Characteristic | X | X | X | X |
Unsubscribe from Characteristic | X | X | X | X |
Get Characteristic Notification Event | X | X | X | X |
Read Descriptor | X | X | X | X |
Write Descriptor | X | X | X | X |
To enable implementation of serde's Serialize
and Deserialize
across some common types in the api
module, use the serde
feature.
[dependencies]
btleplug = { version = "0.10", features = ["serde"] }
To use Bluetooth on macOS Big Sur (11) or later, you need to either package your
binary into an application bundle with an Info.plist
including
NSBluetoothAlwaysUsageDescription
, or (for a command-line application such as
the examples included with btleplug
) enable the Bluetooth permission for your
terminal. You can do the latter by going to System Preferences → Security &
Privacy → Privacy → Bluetooth, clicking the '+' button, and selecting
'Terminal' (or iTerm or whichever terminal application you use).
Due to requiring a hybrid Rust/Java build, btleplug for Android requires a somewhat complicated setup.
Some information on performing the build is available in the original issue for Android support in btlplug.
A quick overview of the build process:
src/droidplug/java
directory, using the
included gradle files, and them to a Maven repo, or have the Java portion of your android app point to that as a local implementation.cargo-ndk
to build. Output
the jniLibs and make sure they end up in the right place in your app.Proguard optimization can be an issue when using btleplug, as the .aar file generated by the java code in btleplug is only accessed by native code, and can be optimized out as part of dead code removal and resource shrinking. To fix this, changes will need to be made to your build.gradle file, and proguard rules will need to be defined.
For build.gradle:
buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
proguard-rules.pro:
#Flutter Wrapper - Only needed if using flutter
-keep class io.flutter.app.** { *; }
-keep class io.flutter.plugin.** { *; }
-keep class io.flutter.util.** { *; }
-keep class io.flutter.view.** { *; }
-keep class io.flutter.** { *; }
-keep class io.flutter.plugins.** { *; }
#btleplug resources
-keep class com.nonpolynomial.** { *; }
-keep class io.github.gedgygedgy.** { *; }
As the Corebluetooth implementation is shared between macOS and iOS, btleplug on iOS should "just work", and seems to be stable. How this is built can vary based on your app setup and what language you're binding to, but sample instructions are as follows (taken from here):
There are also some examples in the Flutter shim listed below.
While we don't specifically support Flutter in this repo yet, there's a template repo available at https://github.com/trobanga/flutter_btleplug. This template has builds for both Android and iOS using btleplug.
Everyone has different bluetooth needs, so if btleplug doesn't fit yours, try these other libraries by the rust community!
BTLEPlug is covered under a BSD 3-Clause License, with some parts from Rumble/Blurmac covered under MIT/Apache dual license, and BSD 3-Clause licenses, respectively. See LICENSE.md for more info and copyright information.