18
18
#include < ArduinoBLE.h>
19
19
20
20
21
- #define PAIR_BUTTON D3 // button for pairing
21
+ #define PAIR_BUTTON 3 // button for pairing
22
22
#define PAIR_LED 24 // LED used to signal pairing
23
23
#define PAIR_LED_ON LOW // Blue LED on Nano BLE has inverted logic
24
24
#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
42
42
unsigned long previousMillis = 0 ; // last time the battery level was checked, in ms
43
43
unsigned long pairingStarted = 0 ; // pairing start time when button is pressed
44
44
bool wasConnected = 0 ;
45
+ bool acceptOrReject = true ;
45
46
46
47
void setup () {
47
48
Serial.begin (9600 ); // initialize serial communication
@@ -53,6 +54,30 @@ void setup() {
53
54
54
55
55
56
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
+ });
56
81
57
82
// IRKs are keys that identify the true owner of a random mac address.
58
83
// Add IRKs of devices you are bonded with.
0 commit comments