5
5
updated by chegewara
6
6
7
7
Create a BLE server that, once we receive a connection, will send periodic notifications.
8
+ The server will continue advertising for more connections after the first one and will notify
9
+ the value of a counter to all connected clients.
10
+
8
11
The service advertises itself as: 4fafc201-1fb5-459e-8fcc-c5c9c331914b
9
12
And has a characteristic of: beb5483e-36e1-4688-b7f5-ea07361b26a8
10
13
26
29
27
30
BLEServer *pServer = NULL ;
28
31
BLECharacteristic *pCharacteristic = NULL ;
32
+ int connectedClients = 0 ;
29
33
bool deviceConnected = false ;
30
- bool oldDeviceConnected = false ;
31
34
uint32_t value = 0 ;
32
35
33
36
// See the following for generating UUIDs:
@@ -38,12 +41,17 @@ uint32_t value = 0;
38
41
39
42
class MyServerCallbacks : public BLEServerCallbacks {
40
43
void onConnect (BLEServer *pServer) {
41
- deviceConnected = true ;
44
+ connectedClients++;
45
+ Serial.print (" Client connected. Total clients: " );
46
+ Serial.println (connectedClients);
47
+ // Continue advertising for more connections
42
48
BLEDevice::startAdvertising ();
43
49
};
44
50
45
51
void onDisconnect (BLEServer *pServer) {
46
- deviceConnected = false ;
52
+ connectedClients--;
53
+ Serial.print (" Client disconnected. Total clients: " );
54
+ Serial.println (connectedClients);
47
55
}
48
56
};
49
57
@@ -66,8 +74,7 @@ void setup() {
66
74
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_INDICATE
67
75
);
68
76
69
- // https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
70
- // Create a BLE Descriptor
77
+ // Descriptor 2902 is not required when using NimBLE as it is automatically added based on the characteristic properties
71
78
pCharacteristic->addDescriptor (new BLE2902 ());
72
79
73
80
// Start the service
@@ -79,27 +86,38 @@ void setup() {
79
86
pAdvertising->setScanResponse (false );
80
87
pAdvertising->setMinPreferred (0x0 ); // set value to 0x00 to not advertise this parameter
81
88
BLEDevice::startAdvertising ();
82
- Serial.println (" Waiting a client connection to notify..." );
89
+ Serial.println (" Waiting for client connections to notify..." );
83
90
}
84
91
85
92
void loop () {
86
- // notify changed value
87
- if (deviceConnected) {
93
+ // Notify changed value to all connected clients
94
+ if (connectedClients > 0 ) {
95
+ Serial.print (" Notifying value: " );
96
+ Serial.print (value);
97
+ Serial.print (" to " );
98
+ Serial.print (connectedClients);
99
+ Serial.println (" client(s)" );
88
100
pCharacteristic->setValue ((uint8_t *)&value, 4 );
89
101
pCharacteristic->notify ();
90
102
value++;
91
- delay (10 ); // bluetooth stack will go into congestion, if too many packets are sent, in 6 hours test i was able to go as low as 3ms
103
+ // Bluetooth stack will go into congestion, if too many packets are sent.
104
+ // In 6 hours of testing, I was able to go as low as 3ms.
105
+ // When using core debug level "debug" or "verbose", the delay can be increased in
106
+ // order to reduce the number of debug messages in the serial monitor.
107
+ delay (100 );
92
108
}
93
- // disconnecting
94
- if (!deviceConnected && oldDeviceConnected) {
109
+
110
+ // Disconnecting - restart advertising when no clients are connected
111
+ if (connectedClients == 0 && deviceConnected) {
95
112
delay (500 ); // give the bluetooth stack the chance to get things ready
96
113
pServer->startAdvertising (); // restart advertising
97
- Serial.println (" start advertising" );
98
- oldDeviceConnected = deviceConnected ;
114
+ Serial.println (" No clients connected, restarting advertising" );
115
+ deviceConnected = false ;
99
116
}
100
- // connecting
101
- if (deviceConnected && !oldDeviceConnected) {
102
- // do stuff here on connecting
103
- oldDeviceConnected = deviceConnected;
117
+
118
+ // Connecting - update state when first client connects
119
+ if (connectedClients > 0 && !deviceConnected) {
120
+ // do stuff here on first connecting
121
+ deviceConnected = true ;
104
122
}
105
123
}
0 commit comments