0

I'm need write data to Bluetooth device with DBus and Bluez using Python dbus library.

But only know how to connect and disconnect device:

import dbus

system_bus = dbus.SystemBus()
device = system_bus.get_object('org.bluez','/org/bluez/hci0/dev_FF_FF_99_96_64_60')
object = dbus.Interface(device, dbus_interface='org.bluez.Device1')
object.Connect()

object.Disconnect()

That's method description from DFeet app:

<method name="WriteValue">
    <arg name="value" type="ay" direction="in"/>
    <arg name="options" type="a{sv}" direction="in"/>
</method>

UPDATE: Added code which write converted bytes

import dbus
import binascii

system_bus = dbus.SystemBus()
device = system_bus.get_object('org.bluez','/org/bluez/hci0/dev_FF_FF_99_96_64_60')
object = dbus.Interface(device, dbus_interface='org.bluez.Device1')
object.Connect()

device_2 = system_bus.get_object('org.bluez','/org/bluez/hci0/dev_FF_FF_99_96_64_60/service000c/char000d')
object_2 = dbus.Interface(device_2, dbus_interface='org.bluez.GattCharacteristic1')

#bytes = dbus.ByteArray(binascii.unhexlify("025a06000000"))
bytes = binascii.unhexlify("025a06000000")

object_2.WriteValue(bytes,{})

object.Disconnect()
TwentySix
  • 61
  • 4
  • 1
    Is this a Bluetooth LE device or Bluetooth Classic device? Assuming it is BLE, you will need to find the DBus path of the GattCharacteristic1 interface you want to write to. There is some more information that might be helpful at: https://stackoverflow.com/a/63751113/7721752 – ukBaz Oct 24 '21 at 13:46
  • @ukBaz Yes it's BLE. I've already find path and interface, My core problem is how prepare bytes or bytes array (example "025a06000000") and send it. – TwentySix Oct 24 '21 at 14:24
  • 1
    You might want to update your question showing how your doing the write and how you are converting to bytes. For the example you gave here: `binascii.unhexlify("025a06000000")` = `b'\x02\x5a\x06\x00\x00\x00'` – ukBaz Oct 24 '21 at 14:32
  • @ukBaz Thanks for the answer. Added write command but not sure, should it be "bytearray" or just result of "unhexlify". I'll check it soon with BtleJuice Framework. – TwentySix Oct 24 '21 at 19:10

1 Answers1

0

After some tests with BtleJuice Framework can confirm this is enough for bytes sending:

bytes = binascii.unhexlify("025a06000000")
object_2.WriteValue(bytes,{})

In methods described like this:

<method name="WriteValue">
    <arg name="value" type="ay" direction="in"/>
    <arg name="options" type="a{sv}" direction="in"/>
</method>

At least it work in my case.

TwentySix
  • 61
  • 4