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

Streamlined BLE Device Scanning Process #55

Merged
merged 4 commits into from
Dec 4, 2023

Conversation

oleander
Copy link
Contributor

This PR simplifies the BLE device scanning process.

Before:

let connect_device = Arc::new(Mutex::new(None));
let device0 = connect_device.clone();
scan.on_result(move |scan, device| {
  if device.name().contains("ESP32") {
    scan.stop().unwrap();
    (*device0.lock()) = Some(device.clone());
  }
});

scan.start(10000).await.unwrap();
let device = &*connect_device.lock();
if let Some(device) = device {
  info!("Advertised Device: {:?}", device);
}

After:

scan.on_result(|device| {
  if device.name().contains("Shelly") {
    Some(device)
  } else {
    None
  }
});

if let Some(device) = scan.start(10000).await.unwrap() {
  info!("Advertised Device: {:?}", device);
}

The device returned by on_result is the return value for start. I removed the scan argument to on_result, but I'm happy to add it again.

I'm open to suggestions on how to improve the scan API further :)

@taks
Copy link
Owner

taks commented Nov 29, 2023

Thanks for sending a pull request.
I think your proposal is good, but there are situations where scan is used other than to acquire a device.

Therefore, instead of changing existing functions, how about adding one more find_device function as shown below?

let result = scan.find_device(1000, |device| device.name().contains("Shelly")).await.unwrap();

if let Some(device) = result {
  info!("Advertised Device: {:?}", device);
}

@oleander oleander force-pushed the feature/scan_result branch from e7823b4 to 433e2f4 Compare December 3, 2023 17:39
@oleander
Copy link
Contributor Author

oleander commented Dec 3, 2023

find_device is a good idea! I updated the code accordingly.

@taks
Copy link
Owner

taks commented Dec 4, 2023

Thanks for the change.

@taks taks merged commit aae5624 into taks:develop Dec 4, 2023
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants