Reconnect if the BLE connection dies

This commit is contained in:
Odd Stråbø 2021-11-02 23:48:06 +01:00
parent 2578fdca75
commit 06453f90e1

View file

@ -230,24 +230,13 @@ def write(fh, data):
dlgt = Delegate() dlgt = Delegate()
prev = time.time() - INTERVAL prev = time.time() - INTERVAL
while True:
try:
with btle.Peripheral(MAC).withDelegate(dlgt) as dev: with btle.Peripheral(MAC).withDelegate(dlgt) as dev:
# for svc in dev.services:
# print(svc, svc.uuid)
# print(dir(dev))
# wd = dev.getServiceByUUID(write_service)
# print(wd)
wd = dev.getCharacteristics(uuid=write_device)[0] wd = dev.getCharacteristics(uuid=write_device)[0]
# print(cs)
# print(dir(cs))
# print(wd.write(b'\xf0\x03\x00\x0c\x00\x08\x91\xd1'))
# rd = dev.getCharacteristics(uuid=read_device)[0]
# print(rd.read())
while True: while True:
dev.waitForNotifications(1) dev.waitForNotifications(1)
@ -267,3 +256,7 @@ with btle.Peripheral(MAC).withDelegate(dlgt) as dev:
# write(wd, CMD_DISABLE_LOAD) # write(wd, CMD_DISABLE_LOAD)
# else: # else:
# write(wd, CMD_ENABLE_LOAD) # write(wd, CMD_ENABLE_LOAD)
except btle.BTLEDisconnectError:
print(datetime.datetime.utcnow().isoformat(" "), "ERROR: Disconnected")
time.sleep(1)