diff --git a/bluezero/bluezutils.py b/bluezero/bluezutils.py index 0acbb40..a238115 100755 --- a/bluezero/bluezutils.py +++ b/bluezero/bluezutils.py @@ -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 diff --git a/bluezero/peripheral.py b/bluezero/peripheral.py index adc8fd3..854e5be 100755 --- a/bluezero/peripheral.py +++ b/bluezero/peripheral.py @@ -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. @@ -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 diff --git a/examples/sensehat_pressure.py b/examples/sensehat_pressure.py index 144769c..a8b11db 100644 --- a/examples/sensehat_pressure.py +++ b/examples/sensehat_pressure.py @@ -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)