Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix manufacturer data lost issue (see #267) #358

Closed
wants to merge 8 commits into from
1 change: 1 addition & 0 deletions src/winrtble/ble/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impl BLEWatcher {
self.watcher
.SetScanningMode(BluetoothLEScanningMode::Active)
.unwrap();
self.watcher.SetAllowExtendedAdvertisements(true)?;
let handler: TypedEventHandler<
BluetoothLEAdvertisementWatcher,
BluetoothLEAdvertisementReceivedEventArgs,
Expand Down
37 changes: 22 additions & 15 deletions src/winrtble/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,26 +143,30 @@ impl Peripheral {

let mut local_name_guard = self.shared.local_name.write().unwrap();
*local_name_guard = Some(name.to_string());
drop(local_name_guard);
qwandor marked this conversation as resolved.
Show resolved Hide resolved
}
}
if let Ok(manufacturer_data) = advertisement.ManufacturerData() {
let mut manufacturer_data_guard = self.shared.latest_manufacturer_data.write().unwrap();

*manufacturer_data_guard = manufacturer_data
.into_iter()
.map(|d| {
let manufacturer_id = d.CompanyId().unwrap();
let data = utils::to_vec(&d.Data().unwrap());

(manufacturer_id, data)
})
.collect();

// Emit event of newly received advertisement
self.emit_event(CentralEvent::ManufacturerDataAdvertisement {
id: self.shared.address.into(),
manufacturer_data: manufacturer_data_guard.clone(),
});
if manufacturer_data.Size().unwrap() > 0 {
*manufacturer_data_guard = manufacturer_data
.into_iter()
.map(|d| {
let manufacturer_id = d.CompanyId().unwrap();
let data = utils::to_vec(&d.Data().unwrap());

(manufacturer_id, data)
})
.collect();

// Emit event of newly received advertisement
self.emit_event(CentralEvent::ManufacturerDataAdvertisement {
id: self.shared.address.into(),
manufacturer_data: manufacturer_data_guard.clone(),
});
drop(manufacturer_data_guard);
qwandor marked this conversation as resolved.
Show resolved Hide resolved
}
}

// The Windows Runtime API (as of 19041) does not directly expose Service Data as a friendly API (like Manufacturer Data above)
Expand Down Expand Up @@ -217,6 +221,7 @@ impl Peripheral {
id: self.shared.address.into(),
service_data: service_data_guard.clone(),
});
drop(service_data_guard);
qwandor marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -253,6 +258,7 @@ impl Peripheral {
id: self.shared.address.into(),
services: services_guard.iter().copied().collect(),
});
drop(services_guard);
qwandor marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -263,6 +269,7 @@ impl Peripheral {
BluetoothAddressType::Random => Some(AddressType::Random),
_ => None,
};
drop(address_type_guard);
qwandor marked this conversation as resolved.
Show resolved Hide resolved
}

if let Ok(tx_reference) = args.TransmitPowerLevelInDBm() {
Expand Down
Loading