Cannot connect BLE in LoPy



  • Hi all,

    I have a LoPy device and I want to connect a device via Bluetooth. It can read the advertises, but it cannot connect the device. Is there any Bluetooth setting I need to set in LoPy device or the corresponding device, or is there any way to debug it? The adv_type I am receiving is 0. Here is the code I am trying:

    from network import Bluetooth
    import time
    bt = Bluetooth()
    bt.start_scan(-1)
    
    while True:
        adv = bt.get_adv()
    
        if adv and bt.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL) == 'BBC micro:bit [zaput]':
            print(adv.adv_type)
    
            try:
                conn = bt.connect(adv.mac)
                print('connected')
                services = conn.services()
                for service in services:
                    time.sleep(0.050)
                    if type(service.uuid()) == bytes:
                        print('Reading chars from service = {}'.format(service.uuid()))
                    else:
                        print('Reading chars from service = %x' % service.uuid())
                    chars = service.characteristics()
                    for char in chars:
                        if (char.properties() & Bluetooth.PROP_READ):
                            print('char {} value = {}'.format(char.uuid(), char.read()))
                conn.disconnect()
                break
            except:
                print("Error while connecting or reading from the BLE device")
                break
        else:
            time.sleep(0.050)
    

    Also, I am getting these lines as the output and firmware version:

    0
    Error while connecting or reading from the BLE device
    Pycom MicroPython 1.20.2.r6 [v1.11-c5a0a97] on 2021-10-28; LoPy with ESP32
    Pybytes Version: 1.7.1
    


  • It says OSError: connection refused after trying to connect with connect(adv.mac)



  • This post is deleted!


  • @jcaron I can connect other device with app on the smart phone. Also, I could connect LoPy when I use it as server.



  • @Fikret-Avci Are you able to connect to the device using a different device, like a smartphone with nRF Connect for instance (or one of the many other BLE scanners)?


Log in to reply
 

Pycom on Twitter