`class TxCharacteristic(Characteristic):
def __init__(self, bus, index, service):
Characteristic.__init__(self, bus, index, UART_TX_CHARACTERISTIC_UUID,
['notify'], service)
self.notifying = False
GLib.io_add_watch(sys.stdin, GLib.IO_IN, self.on_console_input)
def on_console_input(self, fd, condition):
s = fd.readline()
if s.isspace():
pass
else:
self.send_tx(s)
return True
#Send
def send_tx(self, s):
if not self.notifying:
return
value = []
if s == "Sync\n":
milliseconds = int(round(time.time() * 1000))
s= "Time: " + str(milliseconds) + "\n"
print(str(milliseconds))
for c in s:
value.append(dbus.Byte(c.encode()))
self.PropertiesChanged(GATT_CHRC_IFACE, {'Value': value}, [])
def StartNotify(self):
if self.notifying:
return
self.notifying = True
def StopNotify(self):
if not self.notifying:
return
self.notifying = False
class RxCharacteristic(Characteristic):
def __init__(self, bus, index, service):
Characteristic.__init__(self, bus, index, UART_RX_CHARACTERISTIC_UUID,
['write'], service)
def WriteValue(self, value, options):
print('remote: {}'.format(bytearray(value).decode()))
class UartService(Service):
def __init__(self, bus, index):
Service.__init__(self, bus, index, UART_SERVICE_UUID, True)
self.add_characteristic(TxCharacteristic(bus, 0, self))
self.add_characteristic(RxCharacteristic(bus, 1, self))`
I have 2 characteristics classes RxCharacteristic and TxCharacteristic. I would like to modify the WriteValue method in class RxCharacteristic to be able to call method send_tx in TxCharacteristic. Is there any way to do this?
based on: https://github.com/mengguang/pi-ble-uart-server
I tried calling the TxCharacteristic directly and it did not work
RxCharacteristic instance needs to know TxCharacteristic instance to use it. There are lots of ways to do this. An example (minimising modifications to your provided code) is to modify the TxCharacteristic and UartService classes:
class RxCharacteristic(Characteristic):
def __init__(self, bus, index, service, tx_char):
Characteristic.__init__(self, bus, index, UART_RX_CHARACTERISTIC_UUID,
['write'], service)
self.tx_char = tx_char
def WriteValue(self, value, options):
print('remote: {}'.format(bytearray(value).decode()))
self.tx_char.send_tx("test string")
class UartService(Service):
def __init__(self, bus, index):
Service.__init__(self, bus, index, UART_SERVICE_UUID, True)
tx_char = TxCharacteristic(bus, 0, self)
self.add_characteristic(tx_char)
self.add_characteristic(RxCharacteristic(bus, 1, self, tx_char))