Add BLE support using DuplexStream
Summary
Added BLE support using tokio's DuplexStream.
The newly introduced build_ble_stream accepts a name or MAC address of a BLE device.
I've merged existing functions of BleHandler:
- filter_map and notifications
- is_disconnected_event and adapter_events
Related Issues #32
Proposed Changes
Added build_ble_stream function, it's similar to build_serial_stream and build_tcp_stream.
Will add an example next.
Checklist
- [x] Tests pass locally
- [x] Documentation updated if needed
This looks really good! Thanks for contributing all this work.
I'm trying this out in my project ... found I needed this to make it work:
------------------------ src/connections/ble_handler.rs ------------------------
index 3bf1359..2ce6eaa 100644
@@ -91,6 +91,7 @@ impl BleHandler {
services: vec![MSH_SERVICE],
})
.await?;
+ tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
adapter.peripherals().await
}
Should I make a new PR for this fix? Alternatively, I think I could make a device scanner that returned immediately on a match and would quit after a timeout but it would change this code a bit ... should I submit that instead?
Many thanks for this work!
@ksedgwic I'd like to understand the problem before adding a random timeout. Let me try to reproduce it first. There could be a better fix available.
Just wanted to point out it's not a timeout ... it's a "pause" to give the BLE scan a chance to find some devices. I found that without it my program was not finding devices. A good test would be with a slow BLE device ...
@ksedgwic I've added a scan_duration parameter which is used for calling tokio::time::sleep, just like in your patch.
Will merge this soon.
Awesome, that sounds perfect