Skip to content

Commit

Permalink
Numbers bigger than 1 byte working
Browse files Browse the repository at this point in the history
  • Loading branch information
ukBaz committed Jun 1, 2016
1 parent 7a242d9 commit 4af7aac
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
9 changes: 9 additions & 0 deletions bluezero/bluezutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,12 @@ def url_to_advert(url, frame_type, tx_power):
service_data.extend([ord(url[x])])

return service_data


def int_to_dbus_bytes(value):
as_bytes = value.to_bytes(
(value.bit_length() // 8) + 1, byteorder='little')
return_val = []
for byte in as_bytes:
return_val.append(dbus.Byte(byte))
return return_val
10 changes: 4 additions & 6 deletions bluezero/peripheral.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,20 +521,18 @@ def GetAll(self, interface):

@dbus.service.method(constants.GATT_CHRC_IFACE,
in_signature='a{sv}',
out_signature='aya{sy}')
out_signature='ay')
def ReadValue(self, options):
"""Return the characteristic value.
This method is registered with the D-Bus at
``org.bluez.GattCharacteristic1``.
"""
# print('Reading Characteristic', self.value)
if self.value is None:
self.value = 0
return dbus.ByteArray([(self.value).to_bytes((self.value.bit_length() // 8) + 1, byteorder='little')])
# return self.value
return bluezutils.int_to_dbus_bytes(self.value)

@dbus.service.method(constants.GATT_CHRC_IFACE, in_signature='aya{sy}')
@dbus.service.method(constants.GATT_CHRC_IFACE, in_signature='aya{sv}')
def WriteValue(self, value, options):
"""Set the characteristic value.
Expand Down Expand Up @@ -631,7 +629,7 @@ def send_notify_event(self, value):
# print('Update prop')
self.PropertiesChanged(
constants.GATT_CHRC_IFACE,
{'Value': [dbus.Int16(self.value)]}, [])
{'Value': bluezutils.int_to_dbus_bytes(self.value)}, [])

####################
# Descriptor Classes
Expand Down
19 changes: 10 additions & 9 deletions examples/sensehat_pressure.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,38 @@ def get_sensehat_pressure():
else:
return False


def ble_change_update_rate():
peripheral.GObject.timeout_add(1000, get_sensehat_pressure)



# Bluetooth: Define service
pressure_service = peripheral.Service(
'11118000-2222-3333-4444-555566667777',
True)


# Pressure
# Pressure characteristic
pressure_characteristic = peripheral.Characteristic(
'11118010-2222-3333-4444-555566667777',
['read', 'notify'],
['notify'],
pressure_service,
value=0)

pressure_characteristic.add_notify_event(ble_change_update_rate)
# Descriptor
pressure_descriptor = peripheral.UserDescriptor('Pressure', pressure_characteristic)
pressure_descriptor = peripheral.UserDescriptor('Pressure',
pressure_characteristic)
pressure_characteristic.add_descriptor(pressure_descriptor)

# Update Frequency
# Update Frequency characteristic
update_characteristic = peripheral.Characteristic(
'11118011-2222-3333-4444-555566667777',
['read', 'write'],
pressure_service,
value=1020)
value=1000)
update_characteristic.add_write_event(ble_change_update_rate)
# Descriptor
update_descriptor = peripheral.UserDescriptor('update rate', update_characteristic)
update_descriptor = peripheral.UserDescriptor('update rate',
update_characteristic)
update_characteristic.add_descriptor(update_descriptor)

pressure_service.add_characteristic(pressure_characteristic)
Expand Down

1 comment on commit 4af7aac

@WayneKeenan
Copy link
Contributor

@WayneKeenan WayneKeenan commented on 4af7aac Oct 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I missed this, and although the peripheral.py I'm looking at today will be replaced by the new Peripheral work I think the auto-conversation to an int should not be in the library.

IMHO the bytes should be passed as the DBus byte array at level 100 and as a python array of bytes for levels 1 & 10, e.g. for level 100 I'm 'fixing' this for my app, which needs to accept a multi-byte command, simply as:

diff --git a/bluezero/peripheral.py b/bluezero/peripheral.py
index c39a41c..9cac58c 100755
--- a/bluezero/peripheral.py
+++ b/bluezero/peripheral.py
@@ -553,7 +553,7 @@ class Characteristic(dbus.service.Object):
         # print('Writing Characteristic', value)
         # if not self.writable:
         #     raise NotPermittedException()
-        self.value = int.from_bytes(value, byteorder='little', signed=False)
+        self.value = value
         if self.write_cb is not None:
             # print('Write callback')
             self.write_cb()

Please sign in to comment.