BLE Callback handler for READ and WRITE events causes the DISCONNECTION



  • Hi,
    I am using GPY in Expansion Board 3.1

    Pycom MicroPython 1.20.2.rc7 [v1.11-6d01270] on 2020-05-04; GPy with ESP32
    Pybytes Version: 1.4.0

    Callback handler is working when events are either Bluetooth.CLIENT_CONNECTED and Bluetooth.CLIENT_DISCONNECTED.

    Callback handler is NOT working when events are either Bluetooth.CHAR_READ_EVENT and Bluetooth.CHAR_WRITE_EVENT.

    My objective is simple; to READ & WRITE Service.Characteristics Values.

    from network import Bluetooth
    
    bluetooth = Bluetooth()
    bluetooth.set_advertisement(name='GPy', service_uuid=0x1815)
    
    def conn_cb (bt_o):
        events = bt_o.events()
        if  events & Bluetooth.CLIENT_CONNECTED:
            print("Client connected")
        elif events & Bluetooth.CLIENT_DISCONNECTED:
            print("Client disconnected")
    
    bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb)
    
    bluetooth.advertise(True)
    
    srv1 = bluetooth.service(uuid=0x1815, isprimary=True)
    
    chr1 = srv1.characteristic(uuid=0x2A56, permissions=0x180A, properties=Bluetooth.PROP_READ, value=0b01)
    
    def char1_cb_handler(chr):
        events = chr.events()
        if events & Bluetooth.CHAR_READ_EVENT:
            print('Read request on char 1')
        else:
            print('invalid chr1 event')
    
    char1_cb = chr1.callback(trigger=Bluetooth.CHAR_READ_EVENT, handler=char1_cb_handler)


  • FIXED:

    from network import Bluetooth
    
    bluetooth = Bluetooth()
    bluetooth.set_advertisement(name='GPy', service_uuid=0x1815)
    
    def conn_cb (bt_o):
        events = bt_o.events()
        if  events & Bluetooth.CLIENT_CONNECTED:
            print("Client connected")
        elif events & Bluetooth.CLIENT_DISCONNECTED:
            print("Client disconnected")
    
    bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb)
    
    bluetooth.advertise(True)
    
    srv1 = bluetooth.service(uuid=0x1815, isprimary=True)
    chr1 = srv1.characteristic(uuid=0x2A56, properties=Bluetooth.PROP_READ, value=0x01)
    
    def char1_cb_handler(chr, *args):
        events = chr.events()
        if events & Bluetooth.CHAR_READ_EVENT:
            print("Read request on char 1")
        else:
            print("invalid chr1 event")
    
    char1_cb = chr1.callback(trigger=Bluetooth.CHAR_READ_EVENT, handler=char1_cb_handler)
    

Log in to reply
 

Pycom on Twitter