diff --git a/examples/ble_client.rs b/examples/ble_client.rs index 3e8c0d8..386ddfc 100644 --- a/examples/ble_client.rs +++ b/examples/ble_client.rs @@ -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()); } }); diff --git a/examples/ble_scan.rs b/examples/ble_scan.rs index 25567b6..45766ea 100644 --- a/examples/ble_scan.rs +++ b/examples/ble_scan.rs @@ -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(); diff --git a/examples/ble_secure_client.rs b/examples/ble_secure_client.rs index 534d792..5b761eb 100644 --- a/examples/ble_secure_client.rs +++ b/examples/ble_secure_client.rs @@ -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()); } }); diff --git a/src/client/ble_scan.rs b/src/client/ble_scan.rs index 5de114c..ff87849 100644 --- a/src/client/ble_scan.rs +++ b/src/client/ble_scan.rs @@ -4,7 +4,7 @@ use core::ffi::c_void; pub struct BLEScan { #[allow(clippy::type_complexity)] - on_result: Option>, + on_result: Option>, on_completed: Option>, scan_params: esp_idf_sys::ble_gap_disc_params, stopped: bool, @@ -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 @@ -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); } } }