Ohea Lock
A Rust library for controlling Ohea Lock BLE smart locks.
Features
- Transport-agnostic core design
- Optional
btleplug support for desktop platforms
- Async-first API
- Full lock control (lock, unlock, state reading)
- Device information retrieval (battery, firmware, name)
Installation
[dependencies]
ohea-lock = { version = "0.1", features = ["btleplug-support"] }
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
Example
Complete example: discover → connect → pair → control lock
use ohea_lock::{BtleplugTransport, OheaLock, Result};
use btleplug::api::{Central, Manager as _, Peripheral as _, ScanFilter};
use btleplug::platform::Manager;
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<()> {
let manager = Manager::new().await?;
let adapter = manager
.adapters()
.await?
.into_iter()
.next()
.expect("No Bluetooth adapter found");
println!("Scanning for Ohea Lock...");
adapter.start_scan(ScanFilter::default()).await?;
tokio::time::sleep(Duration::from_secs(5)).await;
adapter.stop_scan().await?;
let peripheral = adapter
.peripherals()
.await?
.into_iter()
.filter(|p| {
futures::executor::block_on(async {
p.properties()
.await
.ok()
.flatten()
.and_then(|props| props.local_name)
.is_some_and(|name| name == "Ohea Lock")
})
})
.next()
.expect("Ohea Lock not found");
println!("Connecting...");
let transport = BtleplugTransport::connect_and_discover(peripheral).await?;
let mut lock = OheaLock::new(transport);
lock.initialize().await?;
let info = lock.get_device_info().await?;
println!("Device: {}", info.name);
println!("Firmware: {}", info.firmware_version);
println!("Battery: {}%", info.battery_level);
let state = lock.get_lock_state().await?;
println!("Lock state: {:?}", state);
println!("Unlocking...");
lock.unlock().await?;
tokio::time::sleep(Duration::from_secs(2)).await;
println!("Locking...");
lock.lock().await?;
println!("Done!");
Ok(())
}
API Overview
let lock = OheaLock::new(transport);
lock.initialize().await?;
lock.lock().await?;
lock.unlock().await?;
let state = lock.get_lock_state().await?;
let info = lock.get_device_info().await?; let battery = lock.get_battery_level().await?; let firmware = lock.get_firmware_version().await?;
lock.subscribe_lock_state().await?;
lock.subscribe_battery_level().await?;
lock.unsubscribe_all().await?;
Testing
cargo test --features btleplug-support
Interactive Debug Tool
For hardware debugging and interactive testing:
cargo run --example debug --features btleplug-support
License
GPL-3.0