11.Bluetooth low energy (BLE) serial port pass-through
Bluetooth Low Energy (BLE, Bluetooth Low Energy) serial port pass-through is widely used. On Apple's iOS platform, Classic Bluetooth requires MFi certification to connect with Apple's iOS devices. The Bluetooth low energy device does not have this restriction.
The protocol stack and principle of Bluetooth low energy will not be repeated here, there are many related articles and videos. In short, the bluetooth service is provided in the form of a profile, and there are N characters with independent IDs (UUID) under the profile of each service. Each character has different permissions (read, write, notify, indicate). After the user defines the character and combines it with the authority, a complete service can be provided.
What BLE pass-through is actually to establish a BLE Service, and there are 2 characters under this profile.
1
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
2
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
3
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
Copied!
One for TX (transmit data) and one for RX (receive data). For this they have different permissions. The following code is to create a new service and character:
1
// Create the BLE Service
2
BLEService *pService = pServer->createService(SERVICE_UUID);
3
4
// Create a BLE Characteristic
5
pTxCharacteristic = pService->createCharacteristic(
6
CHARACTERISTIC_UUID_TX,
7
BLECharacteristic::PROPERTY_NOTIFY
8
);
9
10
pTxCharacteristic->addDescriptor(new BLE2902());
11
12
BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(
13
CHARACTERISTIC_UUID_RX,
14
BLECharacteristic::PROPERTY_WRITE
15
);
Copied!
Next are two callback functions, which are performed when there is a connection and when there is a write RX character:
1
class MyServerCallbacks: public BLEServerCallbacks {
2
void onConnect(BLEServer* pServer) {
3
deviceConnected = true;
4
};
5
6
void onDisconnect(BLEServer* pServer) {
7
deviceConnected = false;
8
}
9
};
10
11
class MyCallbacks: public BLECharacteristicCallbacks {
12
void onWrite(BLECharacteristic *pCharacteristic) {
13
std::string rxValue = pCharacteristic->getValue();
14
15
if (rxValue.length() > 0) {
16
Serial.println("*********");
17
Serial.print("Received Value: ");
18
for (int i = 0; i < rxValue.length(); i++)
19
Serial.print(rxValue[i]);
20
21
Serial.println();
22
Serial.println("*********");
23
}
24
}
25
};
Copied!
Finally, the main loop is the control of the connection, which determines whether there is a connection and whether it is disconnected.
1
if (deviceConnected) {
2
pTxCharacteristic->setValue(&txValue, 1);
3
pTxCharacteristic->notify();
4
txValue++;
5
delay(10); // bluetooth stack will go into congestion, if too many packets are sent
6
}
7
8
// disconnecting
9
if (!deviceConnected && oldDeviceConnected) {
10
delay(500); // give the bluetooth stack the chance to get things ready
11
pServer->startAdvertising(); // restart advertising
12
Serial.println("start advertising");
13
oldDeviceConnected = deviceConnected;
14
}
15
// connecting
16
if (deviceConnected && !oldDeviceConnected) {
17
// do stuff here on connecting
18
oldDeviceConnected = deviceConnected;
19
}
Copied!
For the complete code, see the example of the official library: ble_uart, and the debugging tool can use LightBlue.
Export as PDF
Copy link