radiacode icon indicating copy to clipboard operation
radiacode copied to clipboard

Support for Bluetooth on all platforms

Open RamonBeast opened this issue 1 year ago • 1 comments

Edit: check my last comment as with the last commit, no code changes are necessary and the library supports async as well.

I'd like to request a review of my commits, the aim of these changes is to add bluetooth support for all platforms since the original version doesn't work on Mac OS. In order to achieve that I had to remove bluepy and replace it with bleak.

  • I've added a new script find-radiacode.py to help users find their Radiacode via Bluetooth and show its MAC Address or UUID (depending on the platform).
  • For an example of synchronous use, check basic.py in the examples folder
  • For an example of asynchronous use, check webserver.py in the examples folder

Note: I couldn't make the library work (not the original nor my fork) on WSL. Bluetooth is not available on WSL and I couldn't make it work even over USB.

I've tested the three scripts on the following platforms.

Windows (both Bluetooth and USB)

Radiacode-windows-bt

Apple M1 and Intel (both Bluetooth and USB)

Radiacode-M1-USB Radiacode-M1_BT

Linux (Raspberry PI and Ubuntu, both Bluetooth and USB)

Radiacode_Raspberry_USB Radiacode_Raspberry_BT

Further Testing & Feedback

Please let me know if others would be willing to test it to confirm that I haven't missed anything. I tried to be thorough but I had to work on a restricted schedule, so it's more than likely that I've missed something that didn't come up during my own tests.

I would really appreciate knowing if the community and the maintainer welcome these changes or if they prefer to keep this as a separate fork.

Documentation

I haven't gotten to the documentation but it would be great if the original maintainer could expand a bit on it. I'm happy to write the document but if they could share notes about the protocol that would be very helpful.

I have expanded significantly the README file to provide more guidelines.

Logger

I've added a small Logger() class to print logs in a nicer way, it's really just cosmetic but it looks nicer on terminal.

RamonBeast avatar May 09 '24 07:05 RamonBeast

I refactored the main Radiacode() class, I think I've found a solution that's not bad to allow the use of the library both in a synchronous and asynchronous way. With this last change I think we have gained quite some flexibility, I'll try to list everything here:

  • All examples seem to work correctly (I've only had time to test on Mac with both USB and BT)
  • For synchronous use, there is no need to change the code of existing apps, they should work out of the box (but we have new params in the constructor of the Radiacode() class that are useful)
  • Asynchronous apps are now fully supported as well and very easy to implement

Synchronous usage (unchanged):

rc = RadiaCode()
serial = rc.serial_number()

Asynchronous usage:

rc = await RadiaCode.async_init()
serial = await rc.async_serial_number()

In short, to use the library with async the main object must be created by calling RadiaCode.async_() (it supports the same exact parameters of the sync RadiaCode()) and all functions names are the same with a prefix async_ so spectrum() becomes async_spectrum() and so on. Of course those have to be awaited.

This should remove the main concern I initially had that was to adjust existing apps to support asyncs. 🎉

RamonBeast avatar May 11 '24 07:05 RamonBeast