initial commit

[x] connect to the device
 [x] find service and characteristic
This commit is contained in:
Tobias Ollive
2022-01-22 15:42:56 +01:00
parent 3412388c42
commit 4b2456ee53
4 changed files with 714 additions and 3 deletions

101
src/main.rs Normal file
View File

@@ -0,0 +1,101 @@
use bluer::{AdapterEvent, Device};
use bluer::gatt::remote::Characteristic;
use futures::{pin_mut, StreamExt};
use std::error::Error;
const SERVICE_UUID: bluer::Uuid = bluer::Uuid::from_u128(0xadaf0900c33242a893bd25e905756cb8);
const PIXEL_DATA_UUID: bluer::Uuid = bluer::Uuid::from_u128(0xadaf0903c33242a893bd25e905756cb8);
#[tokio::main(flavor = "current_thread")]
async fn main() -> bluer::Result<()> {
let session = bluer::Session::new().await?;
let adapter_names = session.adapter_names().await?;
let adapter_name = adapter_names.first().expect("no Bluetooth adapter found");
let adapter = session.adapter(adapter_name)?;
adapter.set_powered(true).await?;
let discover = adapter.discover_devices().await?;
pin_mut!(discover);
while let Some(evt) = discover.next().await {
match evt {
AdapterEvent::DeviceAdded(addr) => {
let device = adapter.device(addr)?;
match find_neopixel_service(&device).await {
Ok(Some(char)) => {
send seg
println!("found characteristic");
}
Ok(None) => {
println!("characteristic not found")
}
Err(err) => {
println!("device error : {}", &err);
let _ = adapter.remove_device(device.address()).await;
}
}
}
AdapterEvent::DeviceRemoved(addr) => {
println!("Device removed {}", addr);
}
_ => (),
}
}
Ok(())
}
async fn connect(device: &Device, retries: u8) -> bluer::Result<()> {
if device.is_connected().await? {
return Ok(())
}
for i in 0..retries {
match device.connect().await {
Ok(()) => return Ok(()),
Err(err) => {
println!("connection error ({}), retry…", i);
}
}
}
Err(bluer::Error {
kind: bluer::ErrorKind::ConnectionAttemptFailed,
message: "all attempts fail".parse().unwrap(),
})
}
async fn find_neopixel_service(device: &Device) -> bluer::Result<Option<Characteristic>> {
let addr = device.address();
let uuids = device.uuids().await?.unwrap_or_default();
if uuids.contains(&SERVICE_UUID) {
println!("service neopixel found for device {}", &addr);
match connect(device, 3).await {
Ok(()) => {
println!("successefully connected");
}
Err(err) => {
println!("unable to connect, trying new device, {}", err);
return Err(err)
}
}
for service in device.services().await? {
if SERVICE_UUID == service.uuid().await? {
for char in service.characteristics().await? {
if PIXEL_DATA_UUID == char.uuid().await? {
return Ok(Some(char));
}
}
}
}
}
Ok(None)
}