1+ #include < BLESerial.h>
2+
3+ // FOR ETL: Uncomment the following lines
4+ // #include <Embedded_Template_Library.h>
5+ // #include <etl/queue.h>
6+ // #include <etl/circular_buffer.h>
7+
8+ BLESerial SerialBLE;
9+ // If you are using older version of Arduino IDE, you may need to use
10+ // empty template argument (<>), due to the old C++ compiler version (<=std=c++17).
11+ // https://www.cppreference.com/w/cpp/language/ctad.html
12+ //
13+ // Uncomment the line below if you are using an older version of Arduino IDE/C++ compiler
14+ // BLESerial<> SerialBLE;
15+
16+ // FOR ETL: Uncomment one of the following lines
17+ // BLESerial<etl::queue<uint8_t, 255, etl::memory_model::MEMORY_MODEL_SMALL>> SerialBLE;
18+ // OR
19+ // BLESerial<etl::circular_buffer<uint8_t, 255>> SerialBLE;
20+
21+ void setup () {
22+ BLEDevice::init (" ESP32-BLE-Slave" );
23+
24+ BLEServer* pServer = BLEDevice::createServer ();
25+
26+ // Transparent UART Service
27+ // https://developerhelp.microchip.com/xwiki/bin/view/applications/ble/android-development-for-bm70rn4870/transparent-uart-service-for-bm70rn4870/
28+ auto pService = pServer->createService (" 49535343-FE7D-4AE5-8FA9-9FAFD205E455" );
29+
30+ auto pRxCharacteristic = pService->createCharacteristic (" 49535343-1E4D-4BD9-BA61-23C647249616" , BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_WRITE_NR | BLECharacteristic::PROPERTY_NOTIFY);
31+ auto pTxCharacteristic = pService->createCharacteristic (" 49535343-8841-43F4-A8D4-ECBE34729BB3" , BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
32+
33+ SerialBLE.begin (pRxCharacteristic, pTxCharacteristic);
34+
35+ BLEAdvertising* pAdvertising = pServer->getAdvertising ();
36+ pAdvertising->start ();
37+ }
38+
39+ void loop () {
40+ if (Serial.available ()) {
41+ SerialBLE.write (Serial.read ());
42+ SerialBLE.flush ();
43+ }
44+ if (SerialBLE.available ()) {
45+ Serial.write (SerialBLE.read ());
46+ }
47+ }
0 commit comments