Jakeler / ble-serial

"RFCOMM for BLE" a UART over Bluetooth low energy (4+) bridge for Linux, Mac and Windows
https://blog.ja-ke.tech/tags/#bluetooth
MIT License
251 stars 37 forks source link

Autoconnect w/ multiple devices? #96

Open snhirsch opened 6 months ago

snhirsch commented 6 months ago

I'm not sure if this is a bug or the intended behavior. I have two HM-10 devices active and have created a section in the autoconnect.ini file for each. One gets associated with the BLE virtual port and the other with BLE2. I have created both port pairs: BLE<-->COM7 and BL2<-->COM8. Both work individually if I connect manually, but with the autoconnect.py script only one of them (seemingly at random) is ever connected. After the connect it announces that it's entering the main loop and that's it! Shouldn't it continue the scan and also pickup the other device? Or, am I misunderstanding the purpose of the program?

Jakeler commented 6 months ago

It's intended behavior for the basic implementation, the scanner stops while a device is connected and only resumes afterwards. There was never much interest in the autoconnect tool, so a multi device feature was never requested there and I didn't touch it for almost 3 years. That being said, the architecture with asyncio tasks should work well with multiple subprocesses, it's probably enough to remove the scanner stop line. Some extensions are needed just to make this more practical.

  1. connected devices can show up again as scan result, to avoid multiple connections with the same device it should track connected devices and filter the scans against it.
  2. currently logs from ble-serial are passed into the same terminal, this would get messy and confusing with multiple runs in parallel. An idea is to prefix each line with a process/device id. Or always capture it and write into files. Or use some external terminal multiplexer like tmux, multiple terminal tabs/windows etc. this would be nice, but very OS specific, also makes (1) more difficult with the wrapper not immediately returning or not passing the exit code.
snhirsch commented 6 months ago

Thanks for the explanation! I looked at the code briefly and it doesn't look too bad to implement what you suggest. I am not familiar with Python threading, but I assume it has all the "usual" primitives for inter-thread serialization. Does the runtime quantize print output by line? If so, then your suggestion for prefixing the lines will be fine. I'd prefer them to be labeled by endpoint (e.g. BLE, BLE2, etc) since that's better for human interpretation than MAC addresses. Given the need for cross-platform support, the simpler the better.