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

    How can I get connected phone's UUID or Device Name from LoPy4 BLE / Bluetooth ?

    Scheduled Pinned Locked Moved LoPy
    3 Posts 2 Posters 42 Views 3 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.
    • V Offline
      Valentin Marquès
      last edited by

      Hi,

      My Lopy4 is acting as a BLE server. I mean that it is the smartphone which takes the initative to connect to the available Lopy. Here is my program so it is easier to help :

      import time
      import pycom
      import ubinascii
      from network import Bluetooth
      import ujson
      import machine
      import binascii
      import os
      import socket
      
      def connectionCallback(e):
          events = e.events()
          global BLEConnected
          if events & Bluetooth.CLIENT_CONNECTED:
              BLEConnected = True
              print("Client connected")
          
          elif events & Bluetooth.CLIENT_DISCONNECTED:
              BLEConnected = False
              print("Client disconnected")
      
      def char1_cb_handler(chr, data):
          events, value = data
          if events & Bluetooth.CHAR_WRITE_EVENT:
              print("Read request")
              print(value.decode("utf-8"))
          elif events & Bluetooth.CHAR_READ_EVENT:
              print("Write Request")
              print(events)
      
      bt = Bluetooth()
      
      bt.set_advertisement(name='PolySimon', service_uuid=1234567890123456)
      bt.advertise(True)
      
      srv = Bluetooth().service(uuid=1234567890123456, isprimary=True, nbr_chars=1, start=True)
      
      bt.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=connectionCallback)
      
      charread = srv.characteristic(uuid=64321, properties=Bluetooth.PROP_WRITE | Bluetooth.PROP_READ, value=0Xff00)
      
      charreadcallback = charread.callback(trigger=Bluetooth.CHAR_WRITE_EVENT | Bluetooth.CHAR_READ_EVENT, handler=char1_cb_handler)
      

      When a phone gets connected, the "connectionCallback" gets executed. This works as expected and I can make it receive messages after that. But what I would need is a way to identify the phone that connects to it. I would need to get a service UUID, a phone name, or anything that is unique to the phone so I can identify which one is connected.

      I found no way to that, because my Lopy is advertising itself, but not reading advertisement data from the phones (which is normal, the phones should only act as clients and should not have to advertise themselves).

      Is there still a way to get an ID ?

      Thanks in advance for any answer,
      Have a great day

      J 1 Reply Last reply Reply Quote 0
      • J Offline
        jcaron @Valentin Marquès
        last edited by

        @Valentin-Marquès I suppose you are going to have your own app on the phone which will connect to your LoPy? Then setup a writable characteristic on your LoPy BLE server, and have your app write whatever ID you want to that characteristic when it connects.

        1 Reply Last reply Reply Quote 0
        • V Offline
          Valentin Marquès
          last edited by Valentin Marquès

          @jcaron Thanks a lot for your answer. Yes, I have my own app on the phone.

          What do you mean exactly by "writing to a characteristic" ? My current code is already able to receive messages. Is that already what you are talking about ? You mean that the phone should send a message using this feature as soon as it connects ? If that is what you are talking about, then yeah it could be solution. I'll look into that.

          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