@@ -33,7 +33,7 @@ BLEClientCts bleCTime;
33
33
void setup ()
34
34
{
35
35
Serial.begin (115200 );
36
- while ( !Serial ) delay (10 ); // for nrf52840 with native usb
36
+ // while ( !Serial ) delay(10); // for nrf52840 with native usb
37
37
38
38
Serial.println (" Bluefruit52 BLE Client Current Time Example" );
39
39
Serial.println (" -------------------------------------------\n " );
@@ -48,10 +48,13 @@ void setup()
48
48
49
49
Bluefruit.begin ();
50
50
Bluefruit.setTxPower (4 ); // Check bluefruit.h for supported values
51
- Bluefruit. setName ( " Bluefruit52 " );
51
+
52
52
Bluefruit.Periph .setConnectCallback (connect_callback);
53
53
Bluefruit.Periph .setDisconnectCallback (disconnect_callback);
54
54
55
+ // Set connection secured callback, invoked when connection is encrypted
56
+ Bluefruit.Pairing .setSecuredCallback (connection_secured_callback);
57
+
55
58
// Configure CTS client
56
59
bleCTime.begin ();
57
60
@@ -94,12 +97,16 @@ void startAdv(void)
94
97
95
98
void loop ()
96
99
{
100
+ // This example only support 1 connection
101
+ uint16_t const conn_handle = 0 ;
102
+ BLEConnection* conn = Bluefruit.Connection (conn_handle);
103
+
104
+ // connection exist, connected, and secured
105
+ if ( !(conn && conn->connected () && conn->secured ()) ) return ;
106
+
97
107
// Skip if service is not yet discovered
98
108
if ( !bleCTime.discovered () ) return ;
99
109
100
- // Skip if service connection is not paired/secured
101
- if ( !Bluefruit.connPaired ( bleCTime.connHandle () ) ) return ;
102
-
103
110
// Get Time from iOS once per second
104
111
// Note it is not advised to update this quickly
105
112
// Application should use local clock and update time after
@@ -113,18 +120,40 @@ void loop()
113
120
114
121
void connect_callback (uint16_t conn_handle)
115
122
{
123
+ BLEConnection* conn = Bluefruit.Connection (conn_handle);
124
+
116
125
Serial.println (" Connected" );
117
126
118
127
Serial.print (" Discovering CTS ... " );
119
128
if ( bleCTime.discover (conn_handle) )
120
129
{
121
130
Serial.println (" Discovered" );
122
131
123
- // iOS requires pairing to work, it makes sense to request security here as well
124
- Serial.print (" Attempting to PAIR with the iOS device, please press PAIR on your phone ... " );
125
- if ( Bluefruit.requestPairing (conn_handle) )
132
+ // Current Time Service requires pairing to work
133
+ // request Pairing if not bonded
134
+ Serial.println (" Attempting to PAIR with the iOS device, please press PAIR on your phone ... " );
135
+ conn->requestPairing ();
136
+ }
137
+ }
138
+
139
+ void connection_secured_callback (uint16_t conn_handle)
140
+ {
141
+ BLEConnection* conn = Bluefruit.Connection (conn_handle);
142
+
143
+ if ( !conn->secured () )
144
+ {
145
+ // It is possible that connection is still not secured by this time.
146
+ // This happens (central only) when we try to encrypt connection using stored bond keys
147
+ // but peer reject it (probably it remove its stored key).
148
+ // Therefore we will request an pairing again --> callback again when encrypted
149
+ conn->requestPairing ();
150
+ }
151
+ else
152
+ {
153
+ Serial.println (" Secured" );
154
+
155
+ if ( bleCTime.discovered () )
126
156
{
127
- Serial.println (" Done" );
128
157
Serial.println (" Enabling Time Adjust Notify" );
129
158
bleCTime.enableAdjust ();
130
159
@@ -136,8 +165,6 @@ void connect_callback(uint16_t conn_handle)
136
165
137
166
Serial.println ();
138
167
}
139
-
140
- Serial.println ();
141
168
}
142
169
}
143
170
0 commit comments