BLE notifications with FIPY as client



  • Hi, can't get callback(trigger=Bluetooth.CHAR_NOTIFY_EVENT, handler=char_notify_callback,arg=None) to work. Read some threads and some say it is not possible to use notification (due to missing descriptor functions..) other report that it does work.

    Can anyone clarify this?



  • Hi Fredrik,

    First of all, CHAR_NOTIFY_EVENT works well on FiPy.
    Could you tell me which version you are using? I tried my example on different versions and I haven't seen a missing descriptor error.
    You can try the examples below. Please let me know about your experiment.

    BR,
    Meric

    Subscribe_Event

    def chr1_handler(chr, data):
        global battery
        global update
        events = chr.events()
        print("events: ",events)
        if events & (Bluetooth.CHAR_READ_EVENT | Bluetooth.CHAR_SUBSCRIBE_EVENT):
            chr.value(battery)
            print("transmitted :", battery)
            if (events & Bluetooth.CHAR_SUBSCRIBE_EVENT):
                update = ~update
    
    bluetooth = Bluetooth()
    bluetooth.set_advertisement(name='FiPy 45', manufacturer_data="Pycom", service_uuid=0xec00)
    
    bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb)
    bluetooth.advertise(True)
    
    srv1 = bluetooth.service(uuid=0xec00, isprimary=True,nbr_chars=1)
    
    chr1 = srv1.characteristic(uuid=0xec0e, value='read_from_here') #client reads from here
    
    chr1.callback(trigger=(Bluetooth.CHAR_READ_EVENT | Bluetooth.CHAR_SUBSCRIBE_EVENT), handler=chr1_handler)
    
    def update_handler(update_alarm):
        global battery
        global update
        battery-=1
        if battery == 1:
            battery = 100
        if update:
            chr1.value(battery)
    
    update_alarm = Timer.Alarm(update_handler, 1, periodic=True)
    

    Notify_Event

    def char_notify_callback(char, arg):
        char_value = (char.value())
        print("New value: {}".format(char_value))
    
    bt = Bluetooth()
    bt.start_scan(-1)
    adv = None
    while(True):
        adv = bt.get_adv()
        if adv:
            try:
                if bt.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL)=="FiPy 45":
                    conn = bt.connect(adv.mac)
                    try:
                        services = conn.services()
                        for service in services:
                            time.sleep(0.50)
                            chars = service.characteristics()
                            for char in chars:
                                c_uuid = char.uuid()
                                if c_uuid == 0xec0e:
                                    if (char.properties() & Bluetooth.PROP_NOTIFY):
                                        char.callback(trigger=Bluetooth.CHAR_NOTIFY_EVENT, handler=char_notify_callback)
                                        print(c_uuid)
                                        break
                    except:
                        continue
            except:
                continue
    
    bt.stop_scan()
    bt.disconnect_client()
    


Pycom on Twitter