def write_characteristic(self, sender: GattLocalCharacteristic, args: GattWriteRequestedEventArgs): """ Called by pythonnet when a write request is submitted Parameters ---------- sender : GattLocalCharacteristic The object representation of the gatt characteristic whose value we should write to args : GattWriteRequestedEventArgs The event arguments for the write request """ deferral: Deferral = args.GetDeferral() request: GattWriteRequest = sync_async_wrap(GattWriteRequest, args.GetRequestAsync) logger.debug("Request value: {}".format(request.Value)) reader: DataReader = DataReader.FromBuffer(request.Value) n_bytes: int = reader.UnconsumedBufferLength value: bytearray = bytearray() for n in range(0, n_bytes): next_byte: int = reader.ReadByte() value.append(next_byte) logger.debug("Written Value: {}".format(value)) self.write_request(str(sender.Uuid), value) if request.Option == GattWriteOption.WriteWithResponse: request.Respond() logger.debug("Write Complete") deferral.Complete()
def read_characteristic(self, sender: GattLocalCharacteristic, args: GattReadRequestedEventArgs): """ The is triggered by pythonnet when windows receives a read request for a given characteristic Parameters ---------- sender : GattLocalCharacteristic The characteristic Gatt object whose value was requested args : GattReadRequestedEventArgs Arguments for the read request """ logger.debug("Reading Characteristic") deferral: Deferral = args.GetDeferral() value: bytearray = self.read_request(str(sender.Uuid)) logger.debug(f"Current Characteristic value {value}") value = value if value is not None else b"\x00" writer: DataWriter = DataWriter() writer.WriteBytes(value) logger.debug("Getting request object {}".format(self)) request: GattReadRequest = sync_async_wrap(GattReadRequest, args.GetRequestAsync) logger.debug("Got request object {}".format(request)) request.RespondWithValue(writer.DetachBuffer()) deferral.Complete()
def read(sender: GattLocalCharacteristic, args: GattReadRequestedEventArgs): deferral: Deferral = args.GetDeferral() value = self.val writer: DataWriter = DataWriter() writer.WriteBytes(value) request: GattReadRequest = sync_async_wrap(GattReadRequest, args.GetRequestAsync) request.RespondWithValue(writer.DetachBuffer()) deferral.Complete()
def write(sender: GattLocalCharacteristic, args: GattWriteRequestedEventArgs): deferral: Deferral = args.GetDeferral() request: GattWriteRequest = sync_async_wrap( GattWriteRequest, args.GetRequestAsync) reader: DataReader = DataReader.FromBuffer(request.Value) n_bytes: int = reader.UnconsumedBufferLength value: bytearray = bytearray() for n in range(0, n_bytes): next_byte: int = reader.ReadByte() value.append(next_byte) self.val = value if request.Option == GattWriteOption.WriteWithResponse: request.Respond() deferral.Complete()