Two Lopy communicate via BLE, LOSE connection, CANNOT read service.



  • Hello, I am trying communicate two lopy use BLE, one is server, another is client.
    The server success advertising and client could notice and connect. But we I am trying to for loop the service, it goes wrong and of course, I cannot read the characteristic.

    Here is the server code.

    from network import Bluetooth
    bluetooth = Bluetooth()
    bluetooth.init()
    #config data before send
    bluetooth.set_advertisement(name='LoPy', manufacturer_data = 'lopy_server_1',  service_data = b'19950922' ,service_uuid=b'1000000010000000')
    #bluetooth.set_advertisement(name='LoPy', service_data = b'19950922' , service_uuid=b'1234567890123456')
    def conn_cb (bt_o):
        events = bt_o.events() # this method returns the flags and clears the internal registry
        if  events & Bluetooth.CLIENT_CONNECTED:
            print("Client connected")
            #not sure
            bt_o.advertise(False)
        elif events & Bluetooth.CLIENT_DISCONNECTED:
            print("Client disconnected")
            #not sure
            bt_o.advertise(True)
    bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb)
    #start advertise 
    bluetooth.advertise(True)
    #Create a new service on the internal GATT server. Returns a object of type BluetoothServerService.
    #bluetooth.service(uuid, *, isprimary=True, nbr_chars=1, start=True)
    # start = True service start immediately
    srv1 = bluetooth.service(uuid=b'1000000010000000', isprimary=True, nbr_chars=1, start = True)
    chr1 = srv1.characteristic(uuid = b'ab34567890123456', properties = Bluetooth.PROP_READ, value=99999,  permissions = (1 << 0) | (1 << 4))
    #property = Bluetooth.PROP_READ | Bluetooth.PROP_WRITE
    char1_read_counter = 0
    def char1_cb_handler(chr):
        global char1_read_counter
        char1_read_counter += 1
        events = chr.events()
        if  events & Bluetooth.CHAR_WRITE_EVENT:
            print("Write request with value = {}".format(chr.value()))
        else:
            print('Read request on char 1')       
    char1_cb = chr1.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=char1_cb_handler)
    #char1_cb = chr1.callback(trigger=Bluetooth.CHAR_NOTIFY_EVENT, handler=char1_cb_handler)
    

    Here is the client code

    from network import Bluetooth
    import time
    bt = Bluetooth()
    bt.start_scan(-1)
    
    while True:
        adv = bt.get_adv()
        # resolve_adv_data(data , datatype)
        #bt.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL) == 'Heart Rate'
        #  if adv and bt.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL) == 'LoPy':
        if adv and bt.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL) == 'LoPy':
            # stop scan before connect
            bt.stop_scan()
            print('333')
            try:
                connection = bt.connect(adv.mac)  
                if(connection.isconnected()):
                    print("Connection opened!")
         
                # a list of services
                services = connection.services()    
                print('555')
                time.sleep(0.050)
                for service in services:
                    print('444')
                    time.sleep(0.050)
                    print('Reading chars from service = {}'.format(service.uuid()))
                        
                    chars = service.characteristics()
                    for char in chars:
                        print('char {} value = {}'.format(char.uuid(), char.read()))
                connection.disconnect()
                break
     
            except:
                print("Error while connecting or reading from the BLE device")
                break
        else:
            time.sleep(0.050)
    

    I already add stop scan, but connection.isconnected() never work.
    I also try to move connection.isconnected() but after connect, the service never show.



Pycom on Twitter