LoPy connect BLE

  • Hello,

    Some days ago I bought 2 LoPys and now I tried to communicate with them via BLE with my computer/smartphone.

    The code used is:

    from network import Bluetooth
    bluetooth = Bluetooth()
    bluetooth.set_advertisement(name='LoPy', 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")
    elif events & Bluetooth.CLIENT_DISCONNECTED:
    print("Client disconnected")

    bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb)


    The problem is that, whatever device I use (eg computer or smartphone), I can't connect to LoPy. The code prints "Client connected", but the computer/smartphone fails to connect, showing "Can't connect to device". I tried with both LoPys.

    Any sugestions?

  • @jmarcelino Is this problem fixed?

  • @jmarcelino First link dont work....
    I want to send data bewtween 2 lopy doyou know how to do that? I tried alot of diffrent things but not sure what to look for anymore..

  • @jmarcelino

    Hello, I try with two lopy as well. Also add stop scan after connect. But it seems, after connect, the connection loose immediately. I can not read the service :)

  • @ghilinta.daniel

    Since the LoPy is your GATT server you can't write to your phone from it, it's the phone that can read or write to the LoPy.

    The callback you wrote is called when you phone writes to a characteristic on the LoPy and not the other way around.

    If you want to send some information from the LoPy to your phone have your Android app read from a characteristic on the LoPy and use the CHAR_READ_EVENT callback on the LoPy to detect it.

    Hope this helps, BLE and its roles can be confusing.

  • @jmarcelino
    I write the application on Android and I can send information on the characteristic used.

    Now my question is how I can get the characteristic from the client in order to write some informtion to the Android device.

    I used this section of code:
    def char1_cb(chr):
    print("Write request with value = {}".format(chr.value()))

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


  • @ghilinta.daniel
    Once setup with the services you want and started advertising - bluetooth.advertise(True) -the LoPy accepts any BLE connection automatically, there's no need to explicitly accept it.

    When the phone connects you can have a callback on the LoPy as in the example:

    def conn_cb (bt_o):
        events = bt_o.events()
        if  events & Bluetooth.CLIENT_CONNECTED:
            print("Client connected")
    bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED, handler=conn_cb)

    You can take any other action instead of printing that message (for example record that a client is connected or even kick it out..)

  • Yes, this is what I want, creating a GATT server on LoPy and using Android as a client, the central device.

    I will try your advices on Android, but I would want to know if there is a method to verify on LoPy if a client wants to connect to my server and if I have to accept the request for connection.

    Thank you!

  • @ghilinta.daniel

    The GATTSCharacteristic example below is to create a GATT server on the LoPy which I think will be the most usual for mobile app (where phone is Central device)

    If you want to have a GATT server on Android instead see the 'Heart Rate' example on how to scan for your phone, connect and read from a characteristic: https://docs.pycom.io/pycom_esp32/pycom_esp32/tutorial/includes/bluetooth.html

    The code you linked to is very incomplete, after starting a connection you need to wait for the onConnectionStateChange callback before calling discoverServices (which then arrive via the onServicesDiscovered callback, it doesn't simply return it)

    Try following the example at https://github.com/googlesamples/android-BluetoothLeGatt

  • I tried with the given example, runnning one or two services.

    Can you give me a particular example for sending some data to a GATT server running on Android or verify if another device is connected to my LoPy? Also, do I have to accept a connection?

    On Android, I run this code 1 and it does not find any service.

  • Yes, I know the main differences between RN4870 and LoPy bluetooth.

    I will try to use your suggestions and I will reply with the results.

    Thank you!

  • @ghilinta.daniel
    Well the Pycom boards don't accept pairing requests yet, but you don't need to pair to have your Android application communicate with it over BLE.

    Simple set your desired BLE services/characteristics according to the example I linked below and your Android application can then scan for the LoPy inside your own app, connect to it and then read/write from the characteristics you set as needed.

    The RN4870 comes with a bunch of preset services/characteristics already as it abstracts a lot of the BLE protocol into simple - but in other ways limited and proprietary - AT style commands so the operation will be different.

  • Thanks for reply!

    I simply tried to pair the LoPy with computer/smartphone, but this does not work.
    I want to make an application on Android that can exchange some information with LoPy.

    Also, I tried to use BLE RN4870 from Microchip and to pair this chip with my smartphone/laptop, without sending any commands to RN4870 (only powering it) and it works perfect.

  • What app are you trying to connect with? What are you trying to do with BLE?

    At the least you'll need to start some BLE services/characteristics running on the LoPy as otherwise there's nothing to connect to.

    The simplest would be to follow the GATTSCharacteristic example code: https://docs.pycom.io/pycom_esp32/library/network.Bluetooth.html#class-gattscharacteristic

Pycom on Twitter