Bluetooth WiPy client connecting to WiPy peripheral



  • Hi,

    I have a WiPy setup as a BLE peripheral as per the example:

    import binascii
    from network import Bluetooth
    from machine import Timer
    
    bt_periph = Bluetooth()
    bt_periph.set_advertisement(name = 'mag_test', service_uuid = b'1234567890123456')
    
    def conn_cb (bt_o):
        events = bt_o.events()
        if  events & Bluetooth.CLIENT_CONNECTED:
            bt_o.advertise(False)
            print("Client connected")
        elif events & Bluetooth.CLIENT_DISCONNECTED:
            bt_o.advertise(True)
            print("Client disconnected")
    
    bt_periph.callback(trigger = Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler = conn_cb)
    bt_periph.advertise(True)
    srv_test = bt_periph.service(uuid = b'1234567890123456', isprimary = True, nbr_chars = 1)
    blaster = srv_test.characteristic(uuid = b'ab34567890123456', properties = Bluetooth.PROP_READ, value = 55)
    
    def blaster_cb(chr):
        print("Read request, returning value = {}".format(blaster.value()))
    
    blaster_cb = blaster.callback(trigger = Bluetooth.CHAR_READ_EVENT, handler = blaster_cb)
    

    I also have a WiPy setup as a client, trying to connect so I can read the service characteristic. However, whilst the peripheral is telling me that the client is connected, the client tells me that it is not:

    import binascii
    from network import Bluetooth
    import time
    
    bt_client = Bluetooth()
    bt_client.start_scan(-1)
    
    while True:
    	adv = bt_client.get_adv()
    	if adv and bt_client.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL) == 'mag_test':
    		print ("Found mag test peripheral")
    		print("RSSI : {}, MAC : {}".format(adv.rssi, binascii.hexlify(adv.mac)))
    		bt_client.stop_scan()
    		
    		try:
    			conn = bt_client.connect(adv.mac)
    			while not conn.isconnected():
    				conn = bt_client.connect(adv.mac)
    				time.sleep(0.100)
    
    			print("Connected...")
    
    			services = conn.services()
    		    
    			for service in services:
    				time.sleep(0.300)
    				if type(service.uuid()) == bytes:
    					print('Reading characteristics from service = {}'.format(service.uuid()))
    				else:
    					print('Reading characteristics from service = %x' % service.uuid())
    		      	
    				chars = service.characteristics()
    		      	
    				while conn.isconnected():
    					for idx, char in enumerate(chars):
    						if (char.properties() & Bluetooth.PROP_READ):
    							print("char.read : {}".format(char.read()))
    						time.sleep(1.000)
    		   	
    			print("Disconnected...")
    			break
    		
    		except Exception as ex:
    			print("Exception caught...")
    			print(ex)
    	
    	else:
    		time.sleep(0.050)
    

    The conn.isconnected() never returns True, despite the peripheral reporting a connection. I know the connection works, because I can connect and read the characteristic value with a smartphone application.

    If I ignore the False connection state and try to create a services object anyway services = conn.services() then an exception is generated with "connection is already closed"

    Any help for troubleshooting / hunting down the cause would be greatly appreciated.

    Lourens


  • administrators

    Hello,

    Thanks for pointing out this bug. It has been fixed on the latest release 1.7.1.b1.

    Cheers,
    Daniel



  • I have the same problem with two Lopy, I lose connection immediately:)



  • @Innocenzo
    That's OK. As we all know there is no magic, only great science and engineering. ;-)

    Seriously now, we all get that message it's related to storage of BLE bonding keys in the NVS ( non-volatile storage ) area which isn't ready yet. Nothing to worry about.



  • When power up the BLE I Have this messages:

    BTDM CONTROLLER VERSION: 010101
    BTDM ROM VERSION 0101
    BD_ADDR: 24:0A:C4:XX:XX:XX
    NVDS MAGIC FAILED
    RF Init OK with coex
    Enable Classic BT
    Enable Low Energy



  • @jmarcelino I try also to increase the sleep time (a connection every 2 seconds) but the problem persists



  • I understand you're re-trying to connect but triggering a new connection ( conn = bt_client.connect() ) ten times a second sounds like a bad idea to me. I can't see why you'd have to do that but I'll do some tests later today.



  • @lcgeldenhuys So the problem is the WiPy firmware? I actually use the release 1.6.7.b1 and your code does not work.

    There is a fix for that or the only way to perform a BLE connection is to downgrade the firmware?



  • I downgraded to 1.4.0.b1 as I was sure this worked some time ago, and indeed, it works again!



  • Also, tried using the example client code to connect to other peripherals, e.g. the Tile. Same behaviour, namely a mobile app (specifically https://itunes.apple.com/us/app/lightblue-explorer-bluetooth/id557428110?mt=8) is able to connect and interrogate the services and characteristics, but the WiPy fails.

    Using current firmware 1.6.5.b1


Log in to reply
 

Looks like your connection to Pycom Forum was lost, please wait while we try to reconnect.