55 updated by chegewara
66
77 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+
811 The service advertises itself as: 4fafc201-1fb5-459e-8fcc-c5c9c331914b
912 And has a characteristic of: beb5483e-36e1-4688-b7f5-ea07361b26a8
1013
2629
2730BLEServer *pServer = NULL ;
2831BLECharacteristic *pCharacteristic = NULL ;
32+ int connectedClients = 0 ;
2933bool deviceConnected = false ;
30- bool oldDeviceConnected = false ;
3134uint32_t value = 0 ;
3235
3336// See the following for generating UUIDs:
@@ -38,12 +41,17 @@ uint32_t value = 0;
3841
3942class MyServerCallbacks : public BLEServerCallbacks {
4043 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
4248 BLEDevice::startAdvertising ();
4349 };
4450
4551 void onDisconnect (BLEServer *pServer) {
46- deviceConnected = false ;
52+ connectedClients--;
53+ Serial.print (" Client disconnected. Total clients: " );
54+ Serial.println (connectedClients);
4755 }
4856};
4957
@@ -66,8 +74,7 @@ void setup() {
6674 BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_INDICATE
6775 );
6876
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
7178 pCharacteristic->addDescriptor (new BLE2902 ());
7279
7380 // Start the service
@@ -79,27 +86,38 @@ void setup() {
7986 pAdvertising->setScanResponse (false );
8087 pAdvertising->setMinPreferred (0x0 ); // set value to 0x00 to not advertise this parameter
8188 BLEDevice::startAdvertising ();
82- Serial.println (" Waiting a client connection to notify..." );
89+ Serial.println (" Waiting for client connections to notify..." );
8390}
8491
8592void 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)" );
88100 pCharacteristic->setValue ((uint8_t *)&value, 4 );
89101 pCharacteristic->notify ();
90102 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 );
92108 }
93- // disconnecting
94- if (!deviceConnected && oldDeviceConnected) {
109+
110+ // Disconnecting - restart advertising when no clients are connected
111+ if (connectedClients == 0 && deviceConnected) {
95112 delay (500 ); // give the bluetooth stack the chance to get things ready
96113 pServer->startAdvertising (); // restart advertising
97- Serial.println (" start advertising" );
98- oldDeviceConnected = deviceConnected ;
114+ Serial.println (" No clients connected, restarting advertising" );
115+ deviceConnected = false ;
99116 }
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 ;
104122 }
105123}
0 commit comments