pycom / pycom-micropython-sigfox

A fork of MicroPython with the ESP32 port customized to run on Pycom's IoT multi-network modules.
MIT License
196 stars 167 forks source link

BLE notifications don't seem to be working on the LoPy #40

Closed SgAu closed 6 years ago

SgAu commented 6 years ago

BLE notifications don't seem to be working on the LoPy, when the LoPy is running as a BLE server (firmware version: 1.7.6.b1). Subscribing to notifications for a characteristic being advertised by a LoPy does not seem to trigger any notifications.

Sample code:

from network import Bluetooth
import pycom
import time

print('Advertising Bluetooth...')
bluetooth = Bluetooth()
bluetooth.set_advertisement(name='LoPy', service_uuid=b'1234567890abcdef')

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)

#Create service and characteristic
srv1 = bluetooth.service(uuid='0000000000000000', isprimary=True)
chr1 = srv1.characteristic(uuid='0000000000000002', properties=Bluetooth.PROP_INDICATE | Bluetooth.PROP_BROADCAST | Bluetooth.PROP_READ | Bluetooth.PROP_NOTIFY, value='InitialValue')

#Create callback for read events on the characteristic
def char1_cb(chr):
    events = chr.events()
    if events & Bluetooth.CHAR_READ_EVENT :
        print('Bluetooth.CHAR_READ_EVENT')

chr1.callback(trigger=Bluetooth.CHAR_READ_EVENT, handler=char1_cb)

#Update the characteristic value very few seconds
i=0
while True:
    val = 'Value{}'.format(i)
    print('Setting value: {}'.format(val))
    chr1.value(val) #Set the characteristic value - should trigger notification is a client has registered for notifications
    time.sleep(3)
    i+=1

Steps to reproduce:

  1. The above code running on a LoPy advertises a BLE service and characteristic with Read, Indicate & Notify properties. The LoPy is acting as the Bluetooth server / peripheral in this case. The value of the characteristic will be changed every few seconds.
  2. Connect to the Lopy using a BLE app on a smartphone (e.g. BLE Scanner for Android or LightBlue Explorer for iOS).
  3. Test that manually triggering a read value works - it should
  4. Press the button on the app to subscribe to notifications. Expected: Every update in value on the LoPy should be notified to the app. Actual: No value updates are notified to the app. Only manually triggered reads seem to work.

Is this a known issue or am I missing something? Thanks!

SgAu commented 6 years ago

Issue resolved as of firmware release v1.7.7.b1