Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    LoPy connect BLE

    Scheduled Pinned Locked Moved LoPy
    bluetoothbleproblemissueslopy
    14 Posts 4 Posters 7.5k Views 5 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J Offline
      jmarcelino
      last edited by jmarcelino

      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

      Y 1 Reply Last reply Reply Quote 0
      • G Offline
        ghilinta.daniel
        last edited by

        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.

        J 1 Reply Last reply Reply Quote 0
        • J Offline
          jmarcelino @ghilinta.daniel
          last edited by jmarcelino

          @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.

          S 1 Reply Last reply Reply Quote 0
          • G Offline
            ghilinta.daniel
            last edited by

            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!

            1 Reply Last reply Reply Quote 0
            • G Offline
              ghilinta.daniel
              last edited by

              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.

              J 1 Reply Last reply Reply Quote 0
              • J Offline
                jmarcelino @ghilinta.daniel
                last edited by jmarcelino

                @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

                S 1 Reply Last reply Reply Quote 0
                • G Offline
                  ghilinta.daniel
                  last edited by

                  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!

                  J 1 Reply Last reply Reply Quote 0
                  • J Offline
                    jmarcelino @ghilinta.daniel
                    last edited by jmarcelino

                    @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..)

                    G 1 Reply Last reply Reply Quote 0
                    • G Offline
                      ghilinta.daniel @jmarcelino
                      last edited by ghilinta.daniel

                      @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)

                      Thanks!

                      J 1 Reply Last reply Reply Quote 0
                      • J Offline
                        jmarcelino @ghilinta.daniel
                        last edited by jmarcelino

                        @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.

                        1 Reply Last reply Reply Quote 0
                        • Y Offline
                          yunying @jmarcelino
                          last edited by

                          @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 :)

                          1 Reply Last reply Reply Quote 0
                          • S Offline
                            stefan85e @jmarcelino
                            last edited by

                            @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..

                            1 Reply Last reply Reply Quote 0
                            • S Offline
                              stefan85e @jmarcelino
                              last edited by

                              @jmarcelino Is this problem fixed?

                              1 Reply Last reply Reply Quote 0

                              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                              With your input, this post could be even better 💗

                              Register Login
                              • 1 / 1
                              • First post
                                Last post

                              Pycom on Twitter

                              Pycom