1818#include < ArduinoBLE.h>
1919
2020
21- #define PAIR_BUTTON D3 // 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
2424#define PAIR_INTERVAL 30000 // interval for pairing after button press in ms
@@ -42,6 +42,7 @@ int oldBatteryLevel = 0; // last battery level reading from analog input
4242unsigned long previousMillis = 0 ; // last time the battery level was checked, in ms
4343unsigned long pairingStarted = 0 ; // pairing start time when button is pressed
4444bool wasConnected = 0 ;
45+ bool acceptOrReject = true ;
4546
4647void setup () {
4748 Serial.begin (9600 ); // initialize serial communication
@@ -53,6 +54,30 @@ void setup() {
5354
5455
5556 Serial.println (" Serial connected" );
57+
58+ // Callback function with confirmation code when new device is pairing.
59+ BLE.setDisplayCode ([](uint32_t confirmCode){
60+ Serial.println (" New device pairing request." );
61+ Serial.print (" Confirm code matches pairing device: " );
62+ char code[6 ];
63+ sprintf (code, " %06d" , confirmCode);
64+ Serial.println (code);
65+ });
66+
67+ // Callback to allow accepting or rejecting pairing
68+ BLE.setBinaryConfirmPairing ([&acceptOrReject](){
69+ Serial.print (" Should we confirm pairing? " );
70+ delay (5000 );
71+ if (acceptOrReject){
72+ acceptOrReject = false ;
73+ Serial.println (" yes" );
74+ return true ;
75+ }else {
76+ acceptOrReject = true ;
77+ Serial.println (" no" );
78+ return false ;
79+ }
80+ });
5681
5782 // IRKs are keys that identify the true owner of a random mac address.
5883 // Add IRKs of devices you are bonded with.
0 commit comments