rust icon indicating copy to clipboard operation
rust copied to clipboard

Add BLE support using DuplexStream

Open lukipuki opened this issue 1 year ago • 4 comments

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

lukipuki avatar Jan 21 '25 10:01 lukipuki

This looks really good! Thanks for contributing all this work.

danditomaso avatar Jan 25 '25 01:01 danditomaso

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 avatar Apr 10 '25 23:04 ksedgwic

@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.

lukipuki avatar Apr 15 '25 19:04 lukipuki

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 avatar Apr 22 '25 16:04 ksedgwic

@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.

lukipuki avatar Jun 13 '25 16:06 lukipuki

Awesome, that sounds perfect

ksedgwic avatar Jun 17 '25 16:06 ksedgwic