1818#include < ArduinoBLE.h>
1919
2020
21+ #define PAIR_BUTTON D3 // button for pairing
22+ #define PAIR_LED 24 // LED used to signal pairing
23+ #define PAIR_LED_ON LOW // Blue LED on Nano BLE has inverted logic
24+ #define PAIR_INTERVAL 30000 // interval for pairing after button press in ms
25+
26+ #define CTRL_LED LED_BUILTIN
27+
28+
2129 // BLE Battery Service
2230BLEService batteryService (" 180F" );
2331
@@ -31,13 +39,17 @@ BLEStringCharacteristic stringcharacteristic("183E", BLERead | BLEWrite, 31);
3139BLEUnsignedCharCharacteristic secretValue (" 2a3F" , BLERead | BLEWrite | BLEEncryption);
3240
3341int oldBatteryLevel = 0 ; // last battery level reading from analog input
34- long previousMillis = 0 ; // last time the battery level was checked, in ms
42+ unsigned long previousMillis = 0 ; // last time the battery level was checked, in ms
43+ unsigned long pairingStarted = 0 ; // pairing start time when button is pressed
44+ bool wasConnected = 0 ;
3545
3646void setup () {
3747 Serial.begin (9600 ); // initialize serial communication
3848 while (!Serial);
3949
40- pinMode (LED_BUILTIN, OUTPUT); // initialize the built-in LED pin to indicate when a central is connected
50+ pinMode (CTRL_LED, OUTPUT); // initialize the built-in LED pin to indicate when a central is connected
51+ pinMode (PAIR_LED, OUTPUT);
52+ pinMode (PAIR_BUTTON, INPUT_PULLUP);
4153
4254
4355 Serial.println (" Serial connected" );
@@ -145,6 +157,9 @@ void setup() {
145157 secretValue.writeValue (0 );
146158
147159 delay (1000 );
160+
161+ // prevent pairing until button is pressed (will show a pairing rejected message)
162+ BLE.setPairable (false );
148163
149164 /* Start advertising BLE. It will start continuously transmitting BLE
150165 advertising packets and will be visible to remote BLE central devices
@@ -169,30 +184,44 @@ void loop() {
169184 // wait for a BLE central
170185 BLEDevice central = BLE.central ();
171186
187+
188+ // If button is pressed, allow pairing for 30 sec
189+ if (!BLE.pairable () && digitalRead (PAIR_BUTTON) == LOW){
190+ pairingStarted = millis ();
191+ BLE.setPairable (Pairable::ONCE);
192+ Serial.println (" Accepting pairing for 30s" );
193+ } else if (BLE.pairable () && millis () > pairingStarted + PAIR_INTERVAL){
194+ BLE.setPairable (false );
195+ Serial.println (" No longer accepting pairing" );
196+ }
197+ // Make LED blink while pairing is allowed
198+ digitalWrite (PAIR_LED, (BLE.pairable () ? (millis ()%400 )<200 : BLE.paired ()) ? PAIR_LED_ON : !PAIR_LED_ON);
199+
200+
172201 // if a central is connected to the peripheral:
173- if (central) {
174- Serial.print (" Connected to central: " );
175- // print the central's BT address:
176- Serial.println (central.address ());
202+ if (central && central.connected ()) {
203+ if (!wasConnected){
204+ wasConnected = true ;
205+ Serial.print (" Connected to central: " );
206+ // print the central's BT address:
207+ Serial.println (central.address ());
208+ }
177209
178210 // check the battery level every 200ms
179211 // while the central is connected:
180- while (central.connected ()) {
181- long currentMillis = millis ();
182- // if 200ms have passed, check the battery level:
183- if (currentMillis - previousMillis >= 1000 ) {
184- previousMillis = currentMillis;
185- updateBatteryLevel ();
186- if (secretValue.value ()>0 ){
187- digitalWrite (13 ,HIGH);
188- }else {
189- digitalWrite (13 ,LOW);
190- }
191- }
212+ long currentMillis = millis ();
213+ // if 200ms have passed, check the battery level:
214+ if (currentMillis - previousMillis >= 1000 ) {
215+ previousMillis = currentMillis;
216+ updateBatteryLevel ();
217+ digitalWrite (CTRL_LED, secretValue.value ()>0 ? HIGH : LOW);
192218 }
219+ } else if (wasConnected){
220+ wasConnected = false ;
193221 Serial.print (" Disconnected from central: " );
194222 Serial.println (central.address ());
195223 }
224+
196225}
197226
198227void updateBatteryLevel () {
@@ -208,4 +237,4 @@ void updateBatteryLevel() {
208237 batteryLevelChar.writeValue (batteryLevel); // and update the battery level characteristic
209238 oldBatteryLevel = batteryLevel; // save the level for next comparison
210239 }
211- }
240+ }
0 commit comments