@@ -149,6 +149,15 @@ void setup()
149
149
Bluefruit.setTxPower (4 ); // Check bluefruit.h for supported values
150
150
Bluefruit.setName (DEVICE_NAME);
151
151
152
+ // clear bonds if BUTTON A is pressed
153
+ Serial.println (" Hold button A to clear bonds ..... " );
154
+ delay (2000 );
155
+ if (0 == digitalRead (PIN_BUTTON1))
156
+ {
157
+ Serial.println (" Clear all central bonds" );
158
+ Bluefruit.Periph .clearBonds ();
159
+ }
160
+
152
161
// To use dynamic PassKey for pairing, we need to have
153
162
// - IO capacities at least DISPPLAY
154
163
// - Register callback to display/print dynamic passkey for central
@@ -160,6 +169,9 @@ void setup()
160
169
// Set complete callback to print the pairing result
161
170
Bluefruit.Pairing .setCompleteCallback (pairing_complete_callback);
162
171
172
+ // Set connection secured callback, invoked when connection is encrypted
173
+ Bluefruit.Pairing .setSecuredCallback (connection_secured_callback);
174
+
163
175
Bluefruit.Periph .setConnectCallback (connect_callback);
164
176
Bluefruit.Periph .setDisconnectCallback (disconnect_callback);
165
177
@@ -170,19 +182,20 @@ void setup()
170
182
bleuart.setPermission (SECMODE_ENC_WITH_MITM);
171
183
bleuart.begin ();
172
184
173
- // Set up and start advertising
174
- startAdv ();
175
-
176
- Serial.println (" Please use Adafruit's Bluefruit LE app to connect in UART mode" );
177
- Serial.println (" Your phone should pop-up PIN input" );
178
- Serial.println (" Once connected, enter character(s) that you wish to send" );
179
-
180
185
#if TFT_IN_USE != TFT_NO_DISPLAY
181
186
tft.fillScreen (COLOR_BLACK);
182
187
tft.setTextColor (COLOR_WHITE);
183
188
tft.setTextSize (2 );
184
- tft.println (" Advertising ... " );
189
+ tft.setCursor (0 , 0 );
190
+ tft.print (" Advertising..." );
185
191
#endif
192
+
193
+ Serial.println (" Please use Adafruit's Bluefruit LE app to connect in UART mode" );
194
+ Serial.println (" Your phone should pop-up PIN input" );
195
+ Serial.println (" Once connected, enter character(s) that you wish to send" );
196
+
197
+ // Set up and start advertising
198
+ startAdv ();
186
199
}
187
200
188
201
void startAdv (void )
@@ -235,6 +248,27 @@ void loop()
235
248
}
236
249
}
237
250
251
+
252
+ // callback invoked when central connects
253
+ void connect_callback (uint16_t conn_handle)
254
+ {
255
+ // Get the reference to current connection
256
+ BLEConnection* connection = Bluefruit.Connection (conn_handle);
257
+
258
+ char central_name[32 ] = { 0 };
259
+ connection->getPeerName (central_name, sizeof (central_name));
260
+
261
+ Serial.print (" Connected to " );
262
+ Serial.println (central_name);
263
+
264
+ #if TFT_IN_USE != TFT_NO_DISPLAY
265
+ tft.fillScreen (COLOR_BLACK);
266
+ tft.setTextSize (2 );
267
+ tft.setCursor (0 , 0 );
268
+ tft.println (" Connected" );
269
+ #endif
270
+ }
271
+
238
272
// callback invoked when pairing passkey is generated
239
273
// - passkey: 6 keys (without null terminator) for displaying
240
274
// - match_request: true when authentication method is Numberic Comparison.
@@ -273,6 +307,7 @@ bool pairing_passkey_callback(uint16_t conn_handle, uint8_t const passkey[6], bo
273
307
274
308
tft.setTextColor (COLOR_WHITE);
275
309
tft.setTextSize (2 );
310
+ tft.println ();
276
311
#endif
277
312
278
313
// wait until either button is pressed
@@ -308,7 +343,7 @@ void pairing_complete_callback(uint16_t conn_handle, uint8_t auth_status)
308
343
Serial.println (" Failed" );
309
344
}
310
345
311
- #if TFT_IN_USE != TFT_NO_DISPLAY
346
+ #if TFT_IN_USE != TFT_NO_DISPLAY
312
347
if (auth_status == BLE_GAP_SEC_STATUS_SUCCESS)
313
348
{
314
349
tft.setTextColor (COLOR_GREEN);
@@ -321,27 +356,18 @@ void pairing_complete_callback(uint16_t conn_handle, uint8_t auth_status)
321
356
322
357
tft.setTextColor (COLOR_WHITE);
323
358
tft.setTextSize (2 );
324
- #endif
359
+ #endif
325
360
}
326
361
327
- // callback invoked when central connects
328
- void connect_callback (uint16_t conn_handle)
362
+ void connection_secured_callback (uint16_t conn_handle)
329
363
{
330
- // Get the reference to current connection
331
- BLEConnection* connection = Bluefruit.Connection (conn_handle);
364
+ Serial.println (" Secured" );
332
365
333
- char central_name[32 ] = { 0 };
334
- connection->getPeerName (central_name, sizeof (central_name));
335
-
336
- Serial.print (" Connected to " );
337
- Serial.println (central_name);
338
-
339
- #if TFT_IN_USE != TFT_NO_DISPLAY
340
- tft.fillScreen (COLOR_BLACK);
341
- tft.setTextSize (2 );
342
- tft.setCursor (0 , 0 );
343
- tft.println (" Connected" );
344
- #endif
366
+ #if TFT_IN_USE != TFT_NO_DISPLAY
367
+ tft.setTextColor (COLOR_YELLOW);
368
+ tft.println (" secured" );
369
+ tft.setTextColor (COLOR_WHITE);
370
+ #endif
345
371
}
346
372
347
373
/* *
0 commit comments