MCPcopy Create free account
hub / github.com/deviceplug/btleplug / ensure_clean_state

Function ensure_clean_state

tests/common/peripheral_finder.rs:65–81  ·  view source on GitHub ↗

Best-effort cleanup: disconnect any connected peripherals. This prevents state leakage between tests when running in a shared process (Android). The background scan is intentionally kept running to avoid Android scan throttling.

(adapter: &Adapter)

Source from the content-addressed store, hash-verified

63/// This prevents state leakage between tests when running in a shared process (Android).
64/// The background scan is intentionally kept running to avoid Android scan throttling.
65async fn ensure_clean_state(adapter: &Adapter) {
66 // Use timeouts on all operations — a hung disconnect from a prior test
67 // must not block subsequent tests forever.
68 if let Ok(Ok(peripherals)) =
69 tokio::time::timeout(Duration::from_secs(2), adapter.peripherals()).await
70 {
71 for p in peripherals {
72 if let Ok(Ok(true)) =
73 tokio::time::timeout(Duration::from_secs(1), p.is_connected()).await
74 {
75 let _ = tokio::time::timeout(Duration::from_secs(5), p.disconnect()).await;
76 }
77 }
78 }
79 // Give the BLE stack time to settle after disconnection.
80 tokio::time::sleep(Duration::from_secs(2)).await;
81}
82
83/// Discover the test peripheral by name, connect to it, and discover its services.
84///

Callers 1

find_and_connectFunction · 0.85

Calls 3

peripheralsMethod · 0.45
is_connectedMethod · 0.45
disconnectMethod · 0.45

Tested by

no test coverage detected