18
18
#include < ArduinoBLE.h>
19
19
20
20
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
+
21
29
// BLE Battery Service
22
30
BLEService batteryService (" 180F" );
23
31
@@ -31,13 +39,17 @@ BLEStringCharacteristic stringcharacteristic("183E", BLERead | BLEWrite, 31);
31
39
BLEUnsignedCharCharacteristic secretValue (" 2a3F" , BLERead | BLEWrite | BLEEncryption);
32
40
33
41
int 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 ;
35
45
36
46
void setup () {
37
47
Serial.begin (9600 ); // initialize serial communication
38
48
while (!Serial);
39
49
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);
41
53
42
54
43
55
Serial.println (" Serial connected" );
@@ -145,6 +157,9 @@ void setup() {
145
157
secretValue.writeValue (0 );
146
158
147
159
delay (1000 );
160
+
161
+ // prevent pairing until button is pressed (will show a pairing rejected message)
162
+ BLE.setPairable (false );
148
163
149
164
/* Start advertising BLE. It will start continuously transmitting BLE
150
165
advertising packets and will be visible to remote BLE central devices
@@ -169,30 +184,44 @@ void loop() {
169
184
// wait for a BLE central
170
185
BLEDevice central = BLE.central ();
171
186
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
+
172
201
// 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
+ }
177
209
178
210
// check the battery level every 200ms
179
211
// 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);
192
218
}
219
+ } else if (wasConnected){
220
+ wasConnected = false ;
193
221
Serial.print (" Disconnected from central: " );
194
222
Serial.println (central.address ());
195
223
}
224
+
196
225
}
197
226
198
227
void updateBatteryLevel () {
@@ -208,4 +237,4 @@ void updateBatteryLevel() {
208
237
batteryLevelChar.writeValue (batteryLevel); // and update the battery level characteristic
209
238
oldBatteryLevel = batteryLevel; // save the level for next comparison
210
239
}
211
- }
240
+ }
0 commit comments