Skip to content

Commit

Permalink
fix: scan all filtered services for a BLEConnectionUuidSet
Browse files Browse the repository at this point in the history
  • Loading branch information
gmallios committed May 3, 2024
1 parent 5ff17db commit 46d8f6a
Showing 1 changed file with 29 additions and 32 deletions.
61 changes: 29 additions & 32 deletions soundcore-lib/src/ble/btleplug/connection.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use async_trait::async_trait;
use btleplug::api::{CharPropFlags, Characteristic, Peripheral as _, Service};
use btleplug::api::{Characteristic, CharPropFlags, Peripheral as _, Service};
use btleplug::platform::Peripheral;
use futures::StreamExt;
use log::{error, trace, warn};
Expand Down Expand Up @@ -78,39 +78,36 @@ impl BtlePlugConnection {
.filter(|svc| !EXCLUDED_SERVICE_UUIDS.contains(&svc.uuid))
.collect::<Vec<_>>();

let service = services.first();

match service {
Some(service) => {
trace!("Inspecting Service: {:#?}", service);
let characteristics = service.characteristics.to_owned();
let read_characteristic = characteristics.to_owned().into_iter().find(|c| {
c.properties.contains(CharPropFlags::NOTIFY)
&& c.properties.contains(CharPropFlags::READ)
});

let write_characteristic = characteristics.into_iter().find(|c| {
c.properties.contains(CharPropFlags::WRITE)
&& c.properties.contains(CharPropFlags::WRITE_WITHOUT_RESPONSE)
});

match (read_characteristic, write_characteristic) {
(Some(read_characteristic), Some(write_characteristic)) => {
Ok(Some(BLEConnectionUuidSet {
service_uuid: service.uuid,
read_uuid: read_characteristic.uuid,
write_uuid: write_characteristic.uuid,
}))
}
_ => Ok(None),
}
for service in services.iter() {
trace!("Inspecting Service: {:#?}", service);
let characteristics = service.characteristics.to_owned();
let read_characteristic = characteristics.to_owned().into_iter().find(|c| {
c.properties.contains(CharPropFlags::NOTIFY)
&& c.properties.contains(CharPropFlags::READ)
});

let write_characteristic = characteristics.into_iter().find(|c| {
c.properties.contains(CharPropFlags::WRITE)
&& c.properties.contains(CharPropFlags::WRITE_WITHOUT_RESPONSE)
});

if let (Some(read_characteristic), Some(write_characteristic)) =
(read_characteristic, write_characteristic)
{
return Ok(Some(BLEConnectionUuidSet {
service_uuid: service.uuid,
read_uuid: read_characteristic.uuid,
write_uuid: write_characteristic.uuid,
}));
}
_ => {
trace!("No suitable service found for device: {:?}", peripheral.address());
trace!("Available services: {:#?}", services);
Ok(None)
},
}

trace!(
"No suitable service found for device: {:?}",
peripheral.address()
);
trace!("Available services: {:#?}", services);
Ok(None)
}

fn find_service_by_uuid(peripheral: &Peripheral, uuid: Uuid) -> Option<Service> {
Expand Down

0 comments on commit 46d8f6a

Please sign in to comment.