33
33
#define BUTTON_B 30
34
34
#define BUTTON_C 27
35
35
36
- #elif defined ARDUINO_NRF52840_CIRCUITPLAY
37
- // Circuit Playground nRF52840 - FYI doesnt work probably because of button polarity!
38
- #define BUTTON_A 4 // left button
39
- #define BUTTON_B 7 // center switch
40
- #define BUTTON_C 5 // right button
41
-
42
36
#else
43
37
// Default for others
44
38
#define BUTTON_A 9
@@ -67,7 +61,7 @@ MyNotif_t myNotifs[MAX_COUNT] = { 0 };
67
61
int notifCount = 0 ;
68
62
69
63
/* ------------- Display Management -------------*/
70
- #define ONSCREEN_TIME 5000 // On-screen time for each notification
64
+ #define ONSCREEN_TIME 10000 // On-screen time for each notification
71
65
72
66
int activeIndex = 0 ; // Index of currently displayed notification
73
67
int displayIndex = -1 ; // Index of notification about to display
@@ -94,14 +88,17 @@ void setup()
94
88
// Config the peripheral connection with maximum bandwidth
95
89
// more SRAM required by SoftDevice
96
90
// Note: All config***() function must be called before begin()
97
- // Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
91
+ Bluefruit.configPrphBandwidth (BANDWIDTH_MAX);
98
92
99
93
Bluefruit.begin ();
100
94
Bluefruit.setTxPower (4 ); // Check bluefruit.h for supported values
101
95
Bluefruit.setName (" Bluefruit52" );
102
96
Bluefruit.Periph .setConnectCallback (connect_callback);
103
97
Bluefruit.Periph .setDisconnectCallback (disconnect_callback);
104
98
99
+ // Set connection secured callback, invoked when connection is encrypted
100
+ Bluefruit.Pairing .setSecuredCallback (connection_secured_callback);
101
+
105
102
// Configure and Start Service
106
103
bleancs.begin ();
107
104
bleancs.setNotificationCallback (ancs_notification_callback);
@@ -148,6 +145,13 @@ void startAdv(void)
148
145
149
146
void loop ()
150
147
{
148
+ // This example only support 1 connection
149
+ uint16_t const conn_handle = 0 ;
150
+ BLEConnection* conn = Bluefruit.Connection (conn_handle);
151
+
152
+ // connection exist, connected, and secured
153
+ if ( !(conn && conn->connected () && conn->secured ()) ) return ;
154
+
151
155
// If service is not yet discovered
152
156
if ( !bleancs.discovered () ) return ;
153
157
@@ -261,6 +265,8 @@ void displayNotification(int index)
261
265
*/
262
266
void connect_callback (uint16_t conn_handle)
263
267
{
268
+ BLEConnection* conn = Bluefruit.Connection (conn_handle);
269
+
264
270
oled.clearDisplay ();
265
271
oled.setCursor (0 , 0 );
266
272
oled.println (" Connected." );
@@ -272,20 +278,9 @@ void connect_callback(uint16_t conn_handle)
272
278
oled.println (" OK" );
273
279
274
280
// ANCS requires pairing to work
275
- oled.print (" Paring ... " );
276
-
277
- oled.display ();
278
-
279
- if ( Bluefruit.requestPairing (conn_handle) )
280
- {
281
- oled.println (" OK" );
282
-
283
- bleancs.enableNotification ();
284
- oled.println (" Receiving ..." );
285
- }else
286
- {
287
- oled.println (" Failed" );
288
- }
281
+ // request Pairing if not bonded
282
+ oled.println (" Paring ... " );
283
+ conn->requestPairing ();
289
284
}else
290
285
{
291
286
oled.println (" Failed" );
@@ -294,6 +289,31 @@ void connect_callback(uint16_t conn_handle)
294
289
oled.display ();
295
290
}
296
291
292
+ void connection_secured_callback (uint16_t conn_handle)
293
+ {
294
+ BLEConnection* conn = Bluefruit.Connection (conn_handle);
295
+
296
+ if ( !conn->secured () )
297
+ {
298
+ // It is possible that connection is still not secured by this time.
299
+ // This happens (central only) when we try to encrypt connection using stored bond keys
300
+ // but peer reject it (probably it remove its stored key).
301
+ // Therefore we will request an pairing again --> callback again when encrypted
302
+ conn->requestPairing ();
303
+ }
304
+ else
305
+ {
306
+ Serial.println (" Secured" );
307
+
308
+ if ( bleancs.discovered () )
309
+ {
310
+ bleancs.enableNotification ();
311
+ oled.println (" Ready to receive" );
312
+ oled.display ();
313
+ }
314
+ }
315
+ }
316
+
297
317
/* *
298
318
* Notification callback
299
319
* @param notif Notification from iDevice
0 commit comments