44#include < BLEUtils.h>
55#include < BLE2902.h>
66
7+ #define SERVICE_UUID " 0693C92E-8A68-41AA-83E2-AA0B17F70168"
8+ #define CHARACTERISTIC_UUID " 1F5FF96C-AA4A-4159-BCE4-6C25350CB78B"
9+
710BLEServer *pServer = NULL ;
11+ BLECharacteristic *pCharacteristic = NULL ;
12+ bool deviceConnected = false ;
813
914class MyServerCallbacks : public BLEServerCallbacks
1015{
11- void onConnect (BLEServer *pServer)
12- {
13- Serial.println (" Client connected" );
14- }
16+ void onConnect (BLEServer *pServer)
17+ {
18+ Serial.println (" Connected" );
19+ deviceConnected = true ;
20+ }
1521
16- void onDisconnect (BLEServer *pServer)
17- {
18- Serial.println (" Client disconnected" );
19- }
22+ void onDisconnect (BLEServer *pServer)
23+ {
24+ Serial.println (" Disconnected" );
25+ deviceConnected = false ;
26+ }
2027};
2128
2229class MyCallbacks : public BLECharacteristicCallbacks
2330{
24- void onWrite (BLECharacteristic *pCharacteristic)
25- {
26- std::string value = pCharacteristic->getValue ();
27- Serial.print (" Received data: " );
28- Serial.println (value.c_str ());
29- }
31+ void onWrite (BLECharacteristic *pCharacteristic)
32+ {
33+ std::string value = pCharacteristic->getValue ();
34+
35+ // Handle received data
36+ Serial.print (" Received data: " );
37+ Serial.println (value.c_str ());
38+ }
3039};
3140
3241void setup ()
3342{
34- M5.begin ();
35- Serial.begin (9600 );
43+ Serial.begin (9600 );
3644
37- // Set the advertising name
38- BLEDevice::init (" M5-Stack" );
39- pServer = BLEDevice::createServer ();
40- pServer->setCallbacks (new MyServerCallbacks ());
45+ // Initialize BLE
46+ BLEDevice::init (" M5-Stack" );
47+ pServer = BLEDevice::createServer ();
48+ pServer->setCallbacks (new MyServerCallbacks ());
4149
42- // Create a service
43- BLEService *pService = pServer->createService (" 4fafc201-1fb5-459e-8fcc-c5c9c331914b" );
50+ // Create service and characteristic
51+ BLEService *pService = pServer->createService (SERVICE_UUID);
52+ pCharacteristic = pService->createCharacteristic (
53+ CHARACTERISTIC_UUID,
54+ BLECharacteristic::PROPERTY_READ |
55+ BLECharacteristic::PROPERTY_WRITE |
56+ BLECharacteristic::PROPERTY_NOTIFY |
57+ BLECharacteristic::PROPERTY_INDICATE);
58+ pCharacteristic->setCallbacks (new MyCallbacks ());
59+ pCharacteristic->addDescriptor (new BLE2902 ());
4460
45- // Create a characteristic
46- BLECharacteristic *pCharacteristic = pService->createCharacteristic (
47- " beb5483e-36e1-4688-b7f5-ea07361b26a8" ,
48- BLECharacteristic::PROPERTY_READ |
49- BLECharacteristic::PROPERTY_WRITE |
50- BLECharacteristic::PROPERTY_NOTIFY |
51- BLECharacteristic::PROPERTY_INDICATE);
61+ // Start the service
62+ pService->start ();
5263
53- // Add a descriptor
54- pCharacteristic->setCallbacks (new MyCallbacks ());
55- pCharacteristic->addDescriptor (new BLE2902 ());
64+ // Start advertising
65+ BLEAdvertising *pAdvertising = pServer->getAdvertising ();
66+ pAdvertising->addServiceUUID (SERVICE_UUID);
67+ pAdvertising->start ();
5668
57- // Start the service
58- pService->start ();
59- // Start advertising
60- pServer->getAdvertising ()->start ();
61-
62- Serial.println (" BLE server started" );
69+ Serial.println (" BLE server started" );
6370}
6471
6572void loop ()
6673{
67- // Handle BLE events
74+ // Check if device is connected
75+ if (deviceConnected)
76+ {
77+ M5.Lcd .println (" Device connected" );
78+ // Do something when device is connected
79+ }
80+
81+ delay (1000 );
6882}
0 commit comments