Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    BLE notifications with FIPY as client

    Scheduled Pinned Locked Moved Discussion
    blenotifycharacteristiccallback
    2 Posts 2 Posters 64 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • F Offline
      Fredrik Andersson
      last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • M Offline
        Meriç SARIIŞIK
        last edited by

        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()
        
        1 Reply Last reply Reply Quote 0

        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

        With your input, this post could be even better 💗

        Register Login
        • 1 / 1
        • First post
          Last post

        Pycom on Twitter

        Pycom