MCPcopy Index your code
hub / github.com/deviceplug/btleplug

github.com/deviceplug/btleplug @0.12.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release 0.12.0 ↗ · + Follow
895 symbols 1,884 edges 127 files 117 documented · 13%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

btleplug

Crates.io Version docs.rs page Crates.io Downloads Crates.io License

Discord

Github donate button

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. If you're curious about how the library grew, you can read more on that in this blog post.

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 or ble-peripheral-rust.

This library DOES NOT SUPPORT BLUETOOTH 2/CLASSIC. There are no plans to add BT2/Classic support.

Platform Status

  • Linux / Windows / macOS (>= 10.15) / iOS / Android
  • Device enumeration and characteristic/services implemented and working.
  • Please file bugs and missing features if you find them.
  • WASM/WebBluetooth
  • WebBluetooth is possible, and a PR is in, but needs review.
  • Tracking issue here
  • Please hold off on filing more issues until base implementation is landed.

Platform Feature Table

  • X: Completed and released
  • O: In development
  • Blank: Not started
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
Retrieve MTU X X X X
Retrieve Connection Parameters X X
Read RSSI X X X X
Retrieve Adapter State X X X
Request Connection Parameters X X
Add Peripheral by Address X

Platform Caveats

Scan Filtering on Linux (BlueZ)

The ScanFilter passed to start_scan() behaves differently on Linux than other platforms. btleplug forwards service UUID filters to BlueZ, but BlueZ merges discovery filters across all D-Bus clients. Your application will receive advertisements matching the union of all applications' filters, not just your own. Additionally, BlueZ's UUID filter can drop some advertisement types (such as service data) that it shouldn't. Applications targeting Linux should perform their own post-filtering on scan results rather than relying solely on ScanFilter.

Library Features

Serialization/Deserialization

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.12", features = ["serde"] }

Build/Installation Notes for Specific Platforms

macOS

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 PreferencesSecurity & PrivacyPrivacyBluetooth, clicking the '+' button, and selecting 'Terminal' (or iTerm or whichever terminal application you use).

Android

Due to requiring a hybrid Rust/Java build, btleplug for Android requires a somewhat complicated setup.

There is now a build script at ./scripts/build-java.sh for building the java portion of the library on linux or macOS. This can also be used as a guide for manual building if need be.

If your app uses Proguard/R8 with minifyEnabled true, you must add keep rules for btleplug's Java classes. All of btleplug's Java code (including vendored jni-utils classes) is only accessed via JNI from native code, so R8 will treat it as dead code and strip it without these rules.

In your build.gradle, ensure Proguard is configured for release builds:

    buildTypes {
        release {
            shrinkResources true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

proguard-rules.pro:

# btleplug Java classes (accessed only via JNI)
-keep class com.nonpolynomial.** { *; }
-keep class io.github.gedgygedgy.** { *; }

iOS

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):

  • Write a rust library (static) that uses btleplug and exposes an FFI API to C
  • Use cbindgen to generate a C header file for that API
  • Use cargo-lipo to build a universal static lib
  • Drag the header file and the library into your Xcode project
  • Add NSBluetoothAlwaysUsageDescription to your Info.plist file

There are also some examples in the Flutter shim listed below.

Flutter

While we don't specifically support Flutter, there's a template repo available at https://github.com/trobanga/flutter_btleplug. This template has builds for both Android and iOS using btleplug.

As flutter compilation tends to be complex across platforms, we cannot help with flutter build issues.

Tauri

While we don't specifically support Tauri, there's a plugin available at https://github.com/MnlPhlp/tauri-plugin-blec. Please note that all Tauri questions should go to the plugin repo before coming here, we cannot help with Tauri issues as none of this project's developers use Tauri.

Alternative Libraries

Everyone has different bluetooth needs, so if btleplug doesn't fit yours, try these other libraries by the rust community!

  • Bluest - Cross Platform BLE library (Windows/macOS/iOS/Linux)
  • Bluey - Cross Platform BLE library (Windows/Android)
  • Bluer - Official Rust interface for Bluez on Linux, with more features since it only supports one platform (we use Bluez-async internally.)

Integration Tests

btleplug includes integration tests that exercise real BLE operations against a test peripheral.

Quick start with hardware:

# Flash the nRF52840 DK (one-time setup)
cd test-peripheral/zephyr
west build -b nrf52840dk/nrf52840 && west flash

# Run integration tests
cargo test --test '*' -- --ignored

Quick start with a virtual peripheral (requires USB BLE dongle):

# Start the Bumble virtual peripheral
cd test-peripheral/bumble
pip install -r requirements.txt
./run.sh usb:0  # transport argument may vary; see Bumble docs

# In another terminal, run integration tests
cargo test --test '*' -- --ignored

See test-peripheral/README.md for full setup instructions.

License

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.

Extension points exported contracts — how you extend this code

Future (Interface)
Interface for allowing Rust code to interact with Java code in an asynchronous manner. The intention of this interface i [6 …
src/droidplug/java/src/main/java/io/github/gedgygedgy/rust/future/Future.java
Central (Interface)
(no doc) [4 implementers]
src/api/mod.rs
PollResult (Interface)
Represents the result of polling an async future. If a future result is available, the future should return an insta [3 …
src/droidplug/java/src/main/java/io/github/gedgygedgy/rust/task/PollResult.java
BleUuid (Interface)
An extension trait for `Uuid` which provides BLE-specific methods. [1 implementers]
src/api/bleuuid.rs
Stream (Interface)
Interface for allowing Rust code to interact with Java code in an asynchronous manner. The intention of this interface i [3 …
src/droidplug/java/src/main/java/io/github/gedgygedgy/rust/stream/Stream.java
Peripheral (Interface)
(no doc)
src/api/mod.rs
StreamPoll (Interface)
Represents the result of polling an async stream. See Stream#pollNext for a description of how to use this i [3 implementers]
src/droidplug/java/src/main/java/io/github/gedgygedgy/rust/stream/StreamPoll.java
Manager (Interface)
(no doc)
src/api/mod.rs

Core symbols most depended-on inside this repo

iter
called by 38
src/droidplug/jni/objects.rs
disconnect
called by 30
src/droidplug/jni/objects.rs
find_add_class
called by 28
src/droidplug/jni_utils/classcache.rs
finish
called by 27
src/droidplug/java/src/main/java/io/github/gedgygedgy/rust/stream/QueueStream.java
get
called by 26
src/droidplug/java/src/main/java/io/github/gedgygedgy/rust/task/PollResult.java
find_and_connect
called by 26
tests/common/peripheral_finder.rs
with_obj
called by 25
src/droidplug/peripheral.rs
asyncWithFuture
called by 23
src/droidplug/java/src/main/java/com/nonpolynomial/btleplug/android/impl/Peripheral.java

Shape

Method 523
Function 233
Class 116
Enum 12
Interface 11

Languages

Rust70%
Java15%
Kotlin8%
C5%
Python3%
C++1%

Modules by API surface

src/corebluetooth/internal.rs59 symbols
src/droidplug/jni/objects.rs53 symbols
src/droidplug/java/src/main/java/com/nonpolynomial/btleplug/android/impl/Peripheral.java53 symbols
tests/android/src/androidTest/kotlin/com/nonpolynomial/btleplug/test/BleIntegrationTest.kt32 symbols
src/droidplug/jni_utils/exceptions.rs32 symbols
src/bluez/peripheral.rs32 symbols
tests/android/src/main/kotlin/com/nonpolynomial/btleplug/test/NativeTests.kt31 symbols
src/api/bdaddr.rs31 symbols
tests/common/test_cases.rs29 symbols
src/droidplug/peripheral.rs29 symbols
src/winrtble/peripheral.rs28 symbols
test-peripheral/bumble/test_peripheral.py25 symbols

For agents

$ claude mcp add btleplug \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact