WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ impl AddressType {
pub struct ValueNotification {
/// UUID of the characteristic that fired the notification.
pub uuid: Uuid,
/// UUID of the service that contains the characteristic.
pub service_uuid: Uuid,
/// The new value of the characteristic.
pub value: Vec<u8>,
}
Expand Down
9 changes: 7 additions & 2 deletions src/bluez/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,13 @@ fn value_notification(
event: CharacteristicEvent::Value { value },
} if id.service().device() == *device_id => {
let services = services.lock().unwrap();
let uuid = find_characteristic_by_id(&services, id)?.uuid;
Some(ValueNotification { uuid, value })
let info = find_characteristic_by_id(&services, id.clone())?;
let service = services.iter().find(|(_, s)| s.info.id == id.service())?.0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than looping through services a second time, how about making find_characteristic_by_id return the ServiceInfo as well as the characteristic?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to return a tuple with characteristic and service info.

Some(ValueNotification {
uuid: info.uuid,
service_uuid: *service,
value,
})
}
_ => None,
}
Expand Down
3 changes: 2 additions & 1 deletion src/corebluetooth/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ pub enum CoreBluetoothReply {
#[derive(Debug)]
pub enum PeripheralEventInternal {
Disconnected,
Notification(Uuid, Vec<u8>),
Notification(Uuid, Uuid, Vec<u8>),
ManufacturerData(u16, Vec<u8>, i16),
ServiceData(HashMap<Uuid, Vec<u8>>, i16),
Services(Vec<Uuid>, i16),
Expand Down Expand Up @@ -818,6 +818,7 @@ impl CoreBluetoothInternal {
.event_sender
.send(PeripheralEventInternal::Notification(
characteristic_uuid,
service_uuid,
data,
))
.await
Expand Down
8 changes: 6 additions & 2 deletions src/corebluetooth/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,12 @@ impl Peripheral {

loop {
match event_receiver.next().await {
Some(PeripheralEventInternal::Notification(uuid, data)) => {
let notification = ValueNotification { uuid, value: data };
Some(PeripheralEventInternal::Notification(uuid, service_uuid, data)) => {
let notification = ValueNotification {
uuid,
service_uuid,
value: data,
};

// Note: we ignore send errors here which may happen while there are no
// receivers...
Expand Down
7 changes: 6 additions & 1 deletion src/droidplug/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use std::{
pin::Pin,
sync::{Arc, Mutex},
};
use uuid::Uuid;

use super::jni::{
global_jvm,
Expand Down Expand Up @@ -367,7 +368,11 @@ impl api::Peripheral for Peripheral {
let characteristic = JBluetoothGattCharacteristic::from_env(&env, item)?;
let uuid = characteristic.get_uuid()?;
let value = characteristic.get_value()?;
Ok(ValueNotification { uuid, value })
Ok(ValueNotification {
uuid,
service_uuid: Uuid::default(),
value,
}) // TODO: get service UUID
}
Err(err) => Err(err),
})
Expand Down
7 changes: 6 additions & 1 deletion src/winrtble/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,9 +509,14 @@ impl ApiPeripheral for Peripheral {
.ok_or_else(|| Error::NotSupported("Characteristic not found for subscribe".into()))?;
let notifications_sender = self.shared.notifications_channel.clone();
let uuid = characteristic.uuid;
let service_uuid = characteristic.service_uuid;
ble_characteristic
.subscribe(Box::new(move |value| {
let notification = ValueNotification { uuid, value };
let notification = ValueNotification {
uuid,
service_uuid,
value,
};
// Note: we ignore send errors here which may happen while there are no
// receivers...
let _ = notifications_sender.send(notification);
Expand Down
Loading