How can I get connected phone's UUID or Device Name from LoPy4 BLE / Bluetooth ?
-
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
-
@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.
-
@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.