Expand description
CoreBluetooth from pure Rust — no Swift source, no Objective-C source, no generated shim.
§Why the Objective-C runtime and not the Swift one
swiftui-native reaches SwiftUI by resolving Swift-mangled symbols and
synthesising Swift type metadata, because SwiftUI is a Swift-native
framework — it exports about twenty thousand $s-prefixed symbols.
CoreBluetooth exports none. Its .tbd lists Objective-C classes and its
SDK directory has no .swiftinterface; import CoreBluetooth in Swift is
the Clang importer synthesising Swift-shaped declarations from Objective-C
headers at compile time, which still lower to objc_msgSend.
So the runtime this crate builds types in is the Objective-C one. The
principle is unchanged: nothing is compiled ahead of time, no source file in
another language is shipped, and the delegate class is produced at run time
by objc::ClassBuilder. build.rs names three libraries and does nothing
else.
§Layout
objc— message sends, Foundation readers, runtime class synthesis.dispatch— the serial queue delegate callbacks are delivered on.cb— typed accessors forCBCentralManagerand friends.delegate— the synthesised delegate class and itsdelegate::Events.classicandrfcomm— Bluetooth Classic devices and byte streams over IOBluetooth, a separate framework from everything above. macOS only, behindapple-classicandrfcomm.peripheral— the mirror image:CBPeripheralManager, its own synthesised delegate, and the mutable attribute types a GATT server publishes.
Central ties them together. For the Web Bluetooth API built on top, see
the webbluetooth crate.
§Authorization
Bluetooth is TCC-gated on both platforms. A process with no
NSBluetoothAlwaysUsageDescription gets cb::ManagerState::Unauthorized
rather than an error, and no prompt. See the workspace README.
§License
MIT — Copyright © 2025 Eugene Hauptmann
Re-exports§
pub use cb::RestoredCentralState;pub use cb::Advertisement;pub use cb::Authorization;pub use cb::ManagerState;pub use cb::PeripheralState;pub use cb::Properties;pub use cb::WriteType;pub use delegate::Event;pub use delegate::EventSink;pub use l2cap::Channel as L2capChannel;pub use objc::AutoreleasePool;pub use objc::Retained;pub use objc::Id;pub use objc::NIL;pub use peripheral::AttError;pub use peripheral::InvalidAttribute;pub use peripheral::PeripheralEvent;pub use peripheral::PeripheralEventSink;pub use peripheral::PeripheralHost;pub use peripheral::Permissions;pub use peripheral::RestoredPeripheralState;
Modules§
- cb
- Typed accessors for the CoreBluetooth classes.
- delegate
- The delegate class, built at runtime.
- dispatch
- A libdispatch serial queue.
- l2cap
- L2CAP connection-oriented channels.
- macros
- Typed views of
objc_msgSend. - objc
- Objective-C runtime: message sends, collection readers, and class synthesis.
- peripheral
- The peripheral role. macOS and iOS only — see
build.rs. The peripheral role:CBPeripheralManagerand its runtime-built delegate. - peripheral_
backend - The Apple peripheral engine:
CBPeripheralManager.
Macros§
- cached_
class - Look up a class once per call site, then remember it.
- cached_
sel - Resolve a selector once per call site, then remember it.
- msg_
send - Send a message returning an object pointer.
- msg_
send_ t - Send a message returning a non-object value (
bool,isize, a pointer…). - msg_
send_ void - Send a message returning nothing.
Structs§
- Central
- A
CBCentralManagerwith a runtime-built delegate on a private queue.
Enums§
- L2cap
Closed - An L2CAP Protocol/Service Multiplexer — the port number of a channel.
Functions§
- authorization
- Whether CoreBluetooth is present and this process may use it.
Type Aliases§
- Psm
- An L2CAP Protocol/Service Multiplexer — the port number of a channel.