getsenic / gatt-python

Bluetooth GATT SDK for Python
MIT License
318 stars 86 forks source link

stop main loop #32

Open rafifajar22 opened 6 years ago

rafifajar22 commented 6 years ago

how to stop main loop, because there is blocking, and and I can't call stop function, this my code

import gatt

manager = gatt.DeviceManager(adapter_name='hci0')

class AnyDevice(gatt.Device): def services_resolved(self): super().services_resolved()

    device_proprietary_service = next(
        s for s in self.services
        if s.uuid == 'd0cbda9a-9a69-49ab-b4d1-a96d6fbd10c6')

    psp = next(
        c for c in device_proprietary_service.characteristics
        if c.uuid == 'd0cb6c99-9a69-49ab-b4d1-a96d6fbd10c6')

psp.write_value(bytearray(b'\x0a')) tes="\x1D\x5A\x02" best=tes.encode() psp.write_value(best) kay="\x1B\x5A\x00\x4C\x08\x11\x00\x43\x44\x3A\x42\x30\x3A\x32\x31\x3A\x32\x31\x3A\x34\x30\x3A\x42\x33" best=kay.encode() psp.write_value(best)

def characteristic_value_updated(self, characteristic, value):
    print(str(characteristic)+", value: "+value.decode("utf-8"))

def characteristic_write_value_succeeded(self, characteristic):
    print("Write successful.")

def characteristic_write_value_failed(self, characteristic, error):
    print("Write failed. "+str(error))

class Manager(gatt.DeviceManager): def run(self): print("Running Manager") super().run() def quit(self): print("Stopping Manager") super().stop()

manager = Manager(adapter_name = 'hci0') device = AnyDevice(mac_address='CD:B0:21:21:40:B3', manager=manager) device.connect()

manager.run() time.sleep(2) manager.quit()

JlnWntr commented 5 years ago

This is a good question.

Snevzor commented 5 years ago

Hello, please see #28

JlnWntr commented 5 years ago

Thank you, but this refers to KeyboardInterrupts. How can you stop() the manager via python code some seconds after calling manager.run() ?

JlnWntr commented 5 years ago

38

I've added a function for a timeout.