ukBaz / BLE_GATT

Python package for using BlueZ D-Bus API to create a Client
MIT License
29 stars 5 forks source link

Ruby BLE API for comparison #3

Closed WayneKeenan closed 3 years ago

WayneKeenan commented 3 years ago

I like the goal of this project, light on its toes.

I was playing with SonicPi (which is Ruby based) and just from a single gem install command I could talk to a Micro:bit from within SonicPi.

I don't think I even had to enable bluetoothd --experimental, would need to double check, although, notifications aren't working, but I digress...

How to set some micro:bit LEDs in Ruby:

# Microbit LED service
LED_SRV = 'E95DD91D-251D-470A-A062-FA1922DFA9A8'
LED_STATE = 'E95D7B77-251D-470A-A062-FA1922DFA9A8'

adapter = BLE::Adapter.new('hci0')
device = a['FC:6A:D8:32:92:87']         
device.connect

device.services.each {|uuid|
 puts "Found service: %s\n" % [uuid]
}

device.write(LED_SRV, LED_STATE, [65,66,67,68,69].pack('c*'), raw:true )
ukBaz commented 3 years ago

Thanks @WayneKeenan .

Always interesting to see how other libraries do things. I took a couple of minutes to implement what I thought was the same functionality in BLE_GATT:

import BLE_GATT

LED_STATE = 'E95D7B77-251D-470A-A062-FA1922DFA9A8'

device = BLE_GATT.Central('E9:06:4D:45:FC:8D')
device.connect()

for uuid in device.chrcs:
    print(f'Found service {uuid}')

device.char_write(LED_STATE, [65, 66, 67, 68, 69])

device.disconnect()

image

The main difference to my eye is that the Ruby implementation has an adapter and a service UUID. While that is the "right" thing to do I've not exposed those items by default as in the majority of cases these ended up being boiler plate code.