Skip to content

Commit

Permalink
Added Self parameter to BLEScan::on_result.
Browse files Browse the repository at this point in the history
  • Loading branch information
taks committed Oct 24, 2023
1 parent 87fdceb commit 30ab1ff
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
4 changes: 2 additions & 2 deletions examples/ble_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ fn main() {
.active_scan(true)
.interval(100)
.window(99)
.on_result(move |device| {
.on_result(move |scan, device| {
if device.name().contains("ESP32") {
BLEDevice::take().get_scan().stop().unwrap();
scan.stop().unwrap();
(*device0.lock()) = Some(device.clone());
}
});
Expand Down
2 changes: 1 addition & 1 deletion examples/ble_scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn main() {
.active_scan(true)
.interval(100)
.window(99)
.on_result(|param| {
.on_result(|_scan, param| {
info!("Advertised Device: {:?}", param);
});
ble_scan.start(5000).await.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions examples/ble_secure_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ fn main() {
.active_scan(true)
.interval(100)
.window(99)
.on_result(move |device| {
.on_result(move |scan, device| {
if device.is_advertising_service(&SERVICE_UUID) {
BLEDevice::take().get_scan().stop().unwrap();
scan.stop().unwrap();
(*device0.lock()) = Some(device.clone());
}
});
Expand Down
10 changes: 7 additions & 3 deletions src/client/ble_scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::ffi::c_void;

pub struct BLEScan {
#[allow(clippy::type_complexity)]
on_result: Option<Box<dyn FnMut(&BLEAdvertisedDevice) + Send + Sync>>,
on_result: Option<Box<dyn FnMut(&mut Self, &BLEAdvertisedDevice) + Send + Sync>>,
on_completed: Option<Box<dyn FnMut() + Send + Sync>>,
scan_params: esp_idf_sys::ble_gap_disc_params,
stopped: bool,
Expand Down Expand Up @@ -63,9 +63,12 @@ impl BLEScan {
self
}

/// Set a callback to be called when a new scan result is detected.
/// * callback first parameter: The reference to `Self`
/// * callback second parameter: Newly found device
pub fn on_result(
&mut self,
callback: impl FnMut(&BLEAdvertisedDevice) + Send + Sync + 'static,
callback: impl FnMut(&mut Self, &BLEAdvertisedDevice) + Send + Sync + 'static,
) -> &mut Self {
self.on_result = Some(Box::new(callback));
self
Expand Down Expand Up @@ -150,7 +153,8 @@ impl BLEScan {
&& advertised_device.adv_type() != esp_idf_sys::BLE_HCI_ADV_TYPE_ADV_IND as _)
|| disc.event_type == esp_idf_sys::BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP as _
{
callback(advertised_device);
let scan = unsafe { &mut *(arg as *mut Self) };
callback(scan, advertised_device);
}
}
}
Expand Down

0 comments on commit 30ab1ff

Please sign in to comment.