34 releases

0.11.6 Oct 7, 2024
0.11.5 Jan 10, 2024
0.11.3 Nov 19, 2023
0.11.0 Jul 5, 2023
0.4.1 Mar 17, 2020

#10 in Hardware support

Download history 2126/week @ 2024-08-24 3219/week @ 2024-08-31 2954/week @ 2024-09-07 3031/week @ 2024-09-14 3003/week @ 2024-09-21 2944/week @ 2024-09-28 3683/week @ 2024-10-05 3165/week @ 2024-10-12 3553/week @ 2024-10-19 2814/week @ 2024-10-26 2637/week @ 2024-11-02 2429/week @ 2024-11-09 2714/week @ 2024-11-16 2723/week @ 2024-11-23 3540/week @ 2024-11-30 2425/week @ 2024-12-07

11,770 downloads per month
Used in 54 crates (49 directly)

MIT/Apache-2.0/BSD-3-Clause

345KB
7.5K SLoC

Contains (JAR file, 59KB) gradle-wrapper.jar

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. 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.

Platform Status

  • Linux / Windows / macOS / 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

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.10", 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.

Some information on performing the build is available in the original issue for Android support in btlplug.

A quick overview of the build process:

  • For java, you will need the java portion of jni-utils-rs available either in a Maven repository or locally (if locally, you'll need to check out btleplug and change the gradle file).
  • Either build the java portion of btleplug, in the 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.
  • For Rust, the build should go as normal, though we recommend using 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.** { *; }

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 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.

Alternative Libraries

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

  • Bluey - Cross Platform BLE library that takes a different API approach (less Bluez centric)
  • Bluer - Official Rust interface for Bluez on Linux, with more features since it only supports one platform (we use Bluez-async internally.)

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.

Dependencies

~5–46MB
~725K SLoC