1 unstable release

0.1.0 May 12, 2020

#132 in macOS and iOS APIs

Download history 119/week @ 2024-03-11 118/week @ 2024-03-18 80/week @ 2024-03-25 362/week @ 2024-04-01 177/week @ 2024-04-08 206/week @ 2024-04-15 149/week @ 2024-04-22 95/week @ 2024-04-29 55/week @ 2024-05-06 48/week @ 2024-05-13 25/week @ 2024-05-20 40/week @ 2024-05-27 86/week @ 2024-06-03 47/week @ 2024-06-10 68/week @ 2024-06-17 76/week @ 2024-06-24

282 downloads per month
Used in toio

MIT/Apache

120KB
2.5K SLoC

Core Bluetooth

Safe wrapper around Core Bluetooth framework used to communicate with Bluetooth-equipped low energy (LE) and Basic Rate / Enhanced Data Rate (BR/EDR) wireless technology.

Currently only the central role is supported.

Usage

See example in the crate docs and also the examples directory.

Crate Features

By default MPSC rendezvous channel from std is used to perform native framework calls. With async_std_unstable feature chis channel can be replaced with async_std::sync::channel making it possible to pump events in async context. Note the async_std will need unstable feature enabled.


lib.rs:

Safe wrapper around Core Bluetooth framework used to communicate with Bluetooth-equipped low energy (LE) and Basic Rate / Enhanced Data Rate (BR/EDR) wireless technology.

The API closely resembles to the native API with some changes for consistency sake. The main difference is that this API lacks most of the functions for accessing retained state, for thread-safety reasons. If needed users can maintain the retained state via information from events.

Central role

Central role is when application acts as "central" and initiates discovery of and connections to peripherals. The central package contains all the needed objects for central role.

Example

The following example shows how to discover peripherals, services and characteristics, connect to peripherals and subscribe to characteristics.

use core_bluetooth::*;
use core_bluetooth::central::*;

let (central, receiver) = CentralManager::new();

let handle_event = |event| {
    match event {
        CentralEvent::ManagerStateChanged { new_state } => {
            match new_state {
                // Must be in PoweredOn state.
                ManagerState::PoweredOn => central.scan(),
                _ => panic!("no bluetooth available"),
            }
        }
        CentralEvent::PeripheralDiscovered { peripheral, advertisement_data, .. } => {
            if advertisement_data.is_connectable() != Some(false) {
                central.connect(&peripheral);
            }
        }
        CentralEvent::PeripheralConnected { peripheral } => {
            peripheral.discover_services_with_uuids(&[
                "ebe0ccb0-7a0a-4b0c-8a1a-6ff2997da3a6".parse().unwrap()]);
        }
        CentralEvent::ServicesDiscovered { peripheral, services } => {
            if let Ok(services) = services {
                for service in services {
                    peripheral.discover_characteristics_with_uuids(&service, &[
                        "ebe0ccc1-7a0a-4b0c-8a1a-6ff2997da3a6".parse().unwrap()]);
                }
            }
        }
        CentralEvent::CharacteristicsDiscovered { peripheral, characteristics, .. } => {
            if let Ok(chars) = characteristics {
                peripheral.subscribe(&chars[0]);
            }
        }
        CentralEvent::CharacteristicValue { peripheral, value, .. } => {
            if let Ok(value) = value {
                // Decode the value.
                // In this example the value comes from a Xiaomi temperature sensor.
                let t = i16::from_le_bytes([value[0], value[1]]) as f64 / 100.0;
                let rh = value[2];
                println!("t = {} C, rh = {}%", t, rh);
            }
        }
        _ => {}
    }
};

You can find more examples in the examples directory.

Dependencies

~1–14MB
~142K SLoC