Skip to content

Commit

Permalink
Allow different types of write
Browse files Browse the repository at this point in the history
Some devices are sensitive about the type of write performed. Add support
for choosing whether or not a response is requested.
  • Loading branch information
mjg59 committed Jan 27, 2020
1 parent e1b147d commit c562e2d
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions gatt/gatt_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,21 +596,28 @@ def read_value(self, offset=0):
error = _error_from_dbus_error(e)
self.service.device.characteristic_read_value_failed(self, error=error)

def write_value(self, value, offset=0):
def write_value(self, value, offset=0, with_response=True):
"""
Attempts to write a value to the characteristic.
Success or failure will be notified by calls to `write_value_succeeded` or `write_value_failed` respectively.
:param value: array of bytes to be written
:param offset: offset from where to start writing the bytes (defaults to 0)
:param with_response: whether a response should be requested for the write (defaults to True)
"""
bytes = [dbus.Byte(b) for b in value]

if with_response == True:
write_type = "request"
else:
write_type = "command"

try:
self._object.WriteValue(
bytes,
{'offset': dbus.UInt16(offset, variant_level=1)},
{'offset': dbus.UInt16(offset, variant_level=1),
'type': write_type},
reply_handler=self._write_value_succeeded,
error_handler=self._write_value_failed,
dbus_interface='org.bluez.GattCharacteristic1')
Expand Down

0 comments on commit c562e2d

Please sign in to comment.