13. Bluetooth
Bluetooth Low Energy (BLE) is a specific version of Bluetooth technology designed for low-power communication. Compared with traditional Bluetooth technology, BLE focuses on short-term communication, exchanging small amounts of data, and the time to establish a connection between devices is very short. These features make BLE very suitable for IoT (Internet of Things) devices, such as health monitoring devices, smart home control, and other application scenarios. Due to its low power consumption, BLE devices can be battery-powered and run for months or even years. BLE also supports broadcast communication mode, which provides convenience for location services and device discovery.
The following is an example:
from bluetooth import BLE
import bluetooth
bt = BLE() # Create a Bluetooth object
bt.active(1) # Turn on Bluetooth
SERVER_1_UUID = bluetooth.UUID(0x9011)
CHAR_A_UUID = bluetooth.UUID(0x9012)
CHAR_B_UUID = bluetooth.UUID(0x9013)
# Set the read/write permissions of the characteristics
CHAR_A = ( CHAR_A_UUID, bluetooth.FLAG_READ | bluetooth.FLAG_NOTIFY,)
CHAR_B = ( CHAR_B_UUID, bluetooth.FLAG_READ | bluetooth.FLAG_WRITE,)
# Put characteristic A and characteristic B into service 1
SERVER_1 = (SERVER_1_UUID, (CHAR_A, CHAR_B),)
# Put service 1 into the service set
SERVICES = (SERVER_1,)
# Register services to gatts
((char_a, char_b),) = bt.gatts_register_services(SERVICES)
# BLE advertising packet construction
def create_adv_data(name):
# Build the advertising packet
# The format is: length, type, data...
# Where type 0x01 means "Flags", and 0x09 means "Complete Local Name"
# The Flags data is usually set to 0x06 (general discoverable mode and BR/EDR not supported)
# More types can be found in the Bluetooth SIG advertising data type documentation
adv_data = bytearray([0x02, 0x01, 0x06, # Flags
len(name) + 1, 0x09]) + name.encode('utf-8')
return adv_data
# Set the advertising name
adv_data = create_adv_data('ESP32S3R8N8_BLE')
# Start advertising
bt.gap_advertise(100, adv_data)
# Bluetooth interrupt handler function
def ble_irq(event, data):
if event == 1: # Bluetooth connected
print('bluetooth connection')
pass
elif event == 2: # Bluetooth disconnected
print('Bluetooth disconnection')
bt.gap_advertise(100, adv_data)
elif event == 3: # Write event
print('Write event')
# Split the data element into two values
conn_handle, chan_handle = data
# Read the data of chan_handle
buffer = bt.gatts_read(chan_handle)
# Output the received data
print(buffer)
if buffer == b'A': # If the data is the single character A
print('fasong B\r\n')
# Send data to the phone channel (conn_handle): chan_handle single character B
bt.gatts_notify(1, char_a, b'B')
bt.irq(ble_irq)2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Because the ESP32-S3 only supports BLE Bluetooth and does not support classic Bluetooth, it cannot be found directly by searching with a phone's Bluetooth. This case uses the APP [eDebug] for testing. After downloading this routine, opening eDebug will find a Bluetooth device named "ESP32S3R8N8_BLE". Click the name to connect.
After connecting, open the Unknown Characteristic to start sending data.
The phone sends a single character A to the dev board, and the dev board replies with character B after receiving it.