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

Add clear_peripherals method to adapter #382

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,9 @@ pub trait Central: Send + Sync + Clone {
/// Add a [`Peripheral`] from a MAC address without a scan result. Not supported on all Bluetooth systems.
async fn add_peripheral(&self, address: &PeripheralId) -> Result<Self::Peripheral>;

/// Clear the list of [`Peripheral`]s that have been discovered so far.
async fn clear_peripherals(&self) -> Result<()>;
Copy link
Collaborator

Choose a reason for hiding this comment

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

As far as I can see there's no need for this to be async.

Copy link
Contributor Author

@danielstuart14 danielstuart14 May 8, 2024

Choose a reason for hiding this comment

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

I think it is best to keep it async, so that if a new platform (or even a linux solution later) needs it to be async we don't have to break the API. What do you think?


/// Get information about the Bluetooth adapter being used, such as the model or type.
///
/// The details of this are platform-specific andyou should not attempt to parse it, but it may
Expand Down
6 changes: 6 additions & 0 deletions src/bluez/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ impl Central for Adapter {
))
}

async fn clear_peripherals(&self) -> Result<()> {
Err(Error::NotSupported(
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm worried that this will result in people writing programs that crash on Linux as they only tested it on other platforms. What do you think about this just being a silent no-op on Linux? Not being able to clear the peripheral list shouldn't be a big issue in most cases I can think of.

Copy link
Contributor Author

@danielstuart14 danielstuart14 May 8, 2024

Choose a reason for hiding this comment

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

That sounds good, I'll change it. It may be possible to clear the bluez list, but the bluez-async lib doesn't seem to currently support it.

"Can't clear peripheral list on this platform".to_string(),
))
}

async fn adapter_info(&self) -> Result<String> {
let adapter_info = self.session.get_adapter_info(&self.adapter).await?;
Ok(format!("{} ({})", adapter_info.id, adapter_info.modalias))
Expand Down
4 changes: 4 additions & 0 deletions src/common/adapter_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ where
self.peripherals.insert(peripheral.id(), peripheral);
}

pub fn clear_peripherals(&self) {
self.peripherals.clear();
}

pub fn peripherals(&self) -> Vec<PeripheralType> {
self.peripherals
.iter()
Expand Down
5 changes: 5 additions & 0 deletions src/corebluetooth/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ impl Central for Adapter {
))
}

async fn clear_peripherals(&self) -> Result<()> {
self.manager.clear_peripherals();
Ok(())
}

async fn adapter_info(&self) -> Result<String> {
// TODO: Get information about the adapter.
Ok("CoreBluetooth".to_string())
Expand Down
5 changes: 5 additions & 0 deletions src/droidplug/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ impl Central for Adapter {
async fn add_peripheral(&self, address: &PeripheralId) -> Result<Peripheral> {
self.add(address.0)
}

async fn clear_peripherals(&self) -> Result<()> {
self.manager.clear_peripherals();
Ok(())
}
}

pub(crate) fn adapter_report_scan_result_internal(
Expand Down
5 changes: 5 additions & 0 deletions src/winrtble/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ impl Central for Adapter {
))
}

async fn clear_peripherals(&self) -> Result<()> {
self.manager.clear_peripherals();
Ok(())
}

async fn adapter_info(&self) -> Result<String> {
// TODO: Get information about the adapter.
Ok("WinRT".to_string())
Expand Down
Loading