Sending Data with BLE #17352
Answered
by
shariltumin
cemalgnlts
asked this question in
ESP32
-
Hi, I want to simply start BLE from my ESP-32 and connect with my phone, but my phone sees ble and gives this warning when I try to connect: import bluetooth
from time import sleep
class BLE():
def __init__(self, name):
self.name = name
self.isConnected = False
self.ble = bluetooth.BLE()
self.ble.active(True)
self.ble.irq(self.ble_irq)
self.register()
self.advertiser()
# BLE interrupt request handler.
def ble_irq(self, event, data):
if event == 1:
# _IRQ_CENTRAL_CONNECT: a central has connected
self.isConnected = True
print('Connected')
elif event == 2:
# _IRQ_CENTRAL_DISCONNECT: central has disconnected
self.isConnected = False
self.advertiser()
print('Disconnected')
elif event == 3:
# _IRQ_GATTS_WRITE: client has written to RX characteristic
buffer = self.ble.gatts_read(self.rx)
message = buffer.decode('UTF-8').strip()
print(message)
def register(self):
# Nordic UART Service (NUS)
NUS_UUID = '6E400001-B5A3-F393-E0A9-E50E24DCCA9E'
RX_UUID = '6E400002-B5A3-F393-E0A9-E50E24DCCA9E'
TX_UUID = '6E400003-B5A3-F393-E0A9-E50E24DCCA9E'
BLE_NUS = bluetooth.UUID(NUS_UUID)
BLE_RX = (bluetooth.UUID(RX_UUID), bluetooth.FLAG_WRITE)
BLE_TX = (bluetooth.UUID(TX_UUID), bluetooth.FLAG_READ | bluetooth.FLAG_NOTIFY)
BLE_UART = (BLE_NUS, (BLE_TX, BLE_RX,))
SERVICES = (BLE_UART, )
((self.tx, self.rx,), ) = self.ble.gatts_register_services(SERVICES)
def send(self, data):
self.ble.gatts_notify(0, self.tx, data + '\n')
def advertiser(self):
name = bytes(self.name, 'UTF-8')
adv = b'\x02\x01\x06' + bytearray((len(name) + 1, 0x09)) + name
self.ble.gap_advertise(100, adv)
ble = BLE("ESP32")
print("BT on")
while True:
if ble.isConnected:
ble.send("Hi")
sleep(100) |
Beta Was this translation helpful? Give feedback.
Answered by
shariltumin
May 23, 2025
Replies: 1 comment 3 replies
-
You can try including these lines in your init: def __init__(self, name):
self.name = name
self.isConnected = False
self.ble = bluetooth.BLE()
#-----------------------------------
# warning insecure - just work mode
self.ble.config(bond=False)
self.ble.config(le_secure=False)
self.ble.config(mitm=False)
self.ble.config(io=0) # IO_CAP_DISPLAY_ONLY
#-----------------------------------
self.ble.active(True)
self.ble.irq(self.ble_irq)
self.register()
self.advertiser() |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bluetooth Classic and Bluetooth Low Energy (BLE) are not the same thing. They are not compatible and cannot communicate with each other.
If you have a BLE peripheral (server), then you must also have a BLE central (client).
The ESP32 is the only MCU that supports Bluetooth Classic. All others support BLE only.
Standard MicroPython only supports BLE.
If you need Bluetooth Classic on an ESP32, you must build your own custom MicroPython firmware.