ble-server test
This commit is contained in:
parent
317ff93585
commit
82f1227593
1239
Cargo.lock
generated
1239
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -1,3 +1,3 @@
|
|||
[workspace]
|
||||
resolver = "2"
|
||||
members = ["crates/backend"]
|
||||
members = ["crates/backend", "crates/ble-server"]
|
||||
|
|
10
crates/ble-server/Cargo.toml
Normal file
10
crates/ble-server/Cargo.toml
Normal file
|
@ -0,0 +1,10 @@
|
|||
[package]
|
||||
name = "ble-server"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
bluer = { version = "*", features = ["full"] }
|
||||
btleplug = "*"
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
uuid = "*"
|
73
crates/ble-server/src/main.rs
Normal file
73
crates/ble-server/src/main.rs
Normal file
|
@ -0,0 +1,73 @@
|
|||
use btleplug::api::{
|
||||
bleuuid::uuid_from_u16, Central, Manager as _, Peripheral as _, ScanFilter, WriteType,
|
||||
};
|
||||
use btleplug::platform::{Adapter, Manager, Peripheral};
|
||||
use std::error::Error;
|
||||
use std::str::FromStr;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use tokio::time;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
let characteristic = Uuid::from_str("e54b0002-67f5-479e-8711-b3b99198ce6c").unwrap();
|
||||
let manager = Manager::new().await.unwrap();
|
||||
|
||||
// get the first bluetooth adapter
|
||||
let adapters = manager.adapters().await?;
|
||||
let central = adapters.into_iter().nth(0).unwrap();
|
||||
|
||||
// start scanning for devices
|
||||
central.start_scan(ScanFilter::default()).await?;
|
||||
// instead of waiting, you can use central.events() to get a stream which will
|
||||
// notify you of new devices, for an example of that see examples/event_driven_discovery.rs
|
||||
time::sleep(Duration::from_secs(5)).await;
|
||||
|
||||
// find the device we're interested in
|
||||
let handle = find_light(¢ral).await.unwrap();
|
||||
|
||||
println!("{:#?}", handle);
|
||||
|
||||
handle.connect().await?;
|
||||
|
||||
// connect to the device
|
||||
//light.connect().await?;
|
||||
|
||||
// discover services and characteristics
|
||||
handle.discover_services().await?;
|
||||
|
||||
// find the characteristic we want
|
||||
let chars = handle.characteristics();
|
||||
|
||||
println!("Characteristics: {:#?}", chars);
|
||||
|
||||
let light = chars.iter().find(|c| c.uuid == characteristic).unwrap();
|
||||
|
||||
handle
|
||||
.write(light, &[0x00], WriteType::WithoutResponse)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
println!("LED");
|
||||
|
||||
handle.disconnect().await.unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn find_light(central: &Adapter) -> Option<Peripheral> {
|
||||
for p in central.peripherals().await.unwrap() {
|
||||
if p.properties()
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.local_name
|
||||
.iter()
|
||||
.any(|name| name.contains("RAK4630"))
|
||||
{
|
||||
return Some(p);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
Loading…
Reference in a new issue