1818#include < ArduinoBLE.h>
1919
2020
21- #define PAIR_BUTTON 3 // button for pairing
21+ #define PAIR_BUTTON 3 // button for pairing
2222#define PAIR_LED 24 // LED used to signal pairing
2323#define PAIR_LED_ON LOW // Blue LED on Nano BLE has inverted logic
24+ #define PAIR_LED_OFF HIGH // ... so these are inverted as well
2425#define PAIR_INTERVAL 30000 // interval for pairing after button press in ms
2526
2627#define CTRL_LED LED_BUILTIN
@@ -59,13 +60,13 @@ void setup() {
5960 BLE.setDisplayCode ([](uint32_t confirmCode){
6061 Serial.println (" New device pairing request." );
6162 Serial.print (" Confirm code matches pairing device: " );
62- char code[6 ];
63+ char code[7 ];
6364 sprintf (code, " %06d" , confirmCode);
6465 Serial.println (code);
6566 });
6667
6768 // Callback to allow accepting or rejecting pairing
68- BLE.setBinaryConfirmPairing ([&acceptOrReject ](){
69+ BLE.setBinaryConfirmPairing ([](){
6970 Serial.print (" Should we confirm pairing? " );
7071 delay (5000 );
7172 if (acceptOrReject){
@@ -176,8 +177,7 @@ void setup() {
176177
177178 BLE.addService (batteryService); // Add the battery service
178179 batteryLevelChar.writeValue (oldBatteryLevel); // set initial value for this characteristic
179- char * stringCharValue = new char [32 ];
180- stringCharValue = " string" ;
180+ const char * stringCharValue = " string" ;
181181 stringcharacteristic.writeValue (stringCharValue);
182182 secretValue.writeValue (0 );
183183
@@ -219,9 +219,10 @@ void loop() {
219219 BLE.setPairable (false );
220220 Serial.println (" No longer accepting pairing" );
221221 }
222- // Make LED blink while pairing is allowed
223- digitalWrite (PAIR_LED, (BLE.pairable () ? (millis ()%400 )<200 : BLE.paired ()) ? PAIR_LED_ON : !PAIR_LED_ON);
224222
223+ // Make LED blink while pairing is allowed, steady ON when paired
224+ bool led_status = BLE.pairable () ? (millis ()%400 )<200 : BLE.paired ();
225+ digitalWrite (PAIR_LED, led_status ? PAIR_LED_ON : PAIR_LED_OFF);
225226
226227 // if a central is connected to the peripheral:
227228 if (central && central.connected ()) {
@@ -262,4 +263,4 @@ void updateBatteryLevel() {
262263 batteryLevelChar.writeValue (batteryLevel); // and update the battery level characteristic
263264 oldBatteryLevel = batteryLevel; // save the level for next comparison
264265 }
265- }
266+ }
0 commit comments