Skip to content

Commit a3beb00

Browse files
committed
fix build with client cts example
1 parent b66cfc8 commit a3beb00

File tree

2 files changed

+76
-22
lines changed

2 files changed

+76
-22
lines changed

libraries/Bluefruit52Lib/examples/Peripheral/client_cts/client_cts.ino

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ BLEClientCts bleCTime;
3333
void setup()
3434
{
3535
Serial.begin(115200);
36-
while ( !Serial ) delay(10); // for nrf52840 with native usb
36+
// while ( !Serial ) delay(10); // for nrf52840 with native usb
3737

3838
Serial.println("Bluefruit52 BLE Client Current Time Example");
3939
Serial.println("-------------------------------------------\n");
@@ -48,10 +48,13 @@ void setup()
4848

4949
Bluefruit.begin();
5050
Bluefruit.setTxPower(4); // Check bluefruit.h for supported values
51-
Bluefruit.setName("Bluefruit52");
51+
5252
Bluefruit.Periph.setConnectCallback(connect_callback);
5353
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
5454

55+
// Set connection secured callback, invoked when connection is encrypted
56+
Bluefruit.Pairing.setSecuredCallback(connection_secured_callback);
57+
5558
// Configure CTS client
5659
bleCTime.begin();
5760

@@ -94,12 +97,16 @@ void startAdv(void)
9497

9598
void loop()
9699
{
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+
97107
// Skip if service is not yet discovered
98108
if ( !bleCTime.discovered() ) return;
99109

100-
// Skip if service connection is not paired/secured
101-
if ( !Bluefruit.connPaired( bleCTime.connHandle() ) ) return;
102-
103110
// Get Time from iOS once per second
104111
// Note it is not advised to update this quickly
105112
// Application should use local clock and update time after
@@ -113,18 +120,40 @@ void loop()
113120

114121
void connect_callback(uint16_t conn_handle)
115122
{
123+
BLEConnection* conn = Bluefruit.Connection(conn_handle);
124+
116125
Serial.println("Connected");
117126

118127
Serial.print("Discovering CTS ... ");
119128
if ( bleCTime.discover(conn_handle) )
120129
{
121130
Serial.println("Discovered");
122131

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() )
126156
{
127-
Serial.println("Done");
128157
Serial.println("Enabling Time Adjust Notify");
129158
bleCTime.enableAdjust();
130159

@@ -136,8 +165,6 @@ void connect_callback(uint16_t conn_handle)
136165

137166
Serial.println();
138167
}
139-
140-
Serial.println();
141168
}
142169
}
143170

libraries/Bluefruit52Lib/examples/Peripheral/client_cts_oled/client_cts_oled.ino

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,13 @@ void setup()
5454

5555
Bluefruit.begin();
5656
Bluefruit.setTxPower(4); // Check bluefruit.h for supported values
57-
Bluefruit.setName("Bluefruit52");
57+
5858
Bluefruit.Periph.setConnectCallback(connect_callback);
5959
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
6060

61+
// Set connection secured callback, invoked when connection is encrypted
62+
Bluefruit.Pairing.setSecuredCallback(connection_secured_callback);
63+
6164
// Configure CTS client
6265
bleCTime.begin();
6366

@@ -103,12 +106,16 @@ void startAdv(void)
103106

104107
void loop()
105108
{
109+
// This example only support 1 connection
110+
uint16_t const conn_handle = 0;
111+
BLEConnection* conn = Bluefruit.Connection(conn_handle);
112+
113+
// connection exist, connected, and secured
114+
if ( !(conn && conn->connected() && conn->secured()) ) return;
115+
106116
// Skip if service is not yet discovered
107117
if ( !bleCTime.discovered() ) return;
108118

109-
// Skip if service connection is not paired/secured
110-
if ( !Bluefruit.connPaired( bleCTime.connHandle() ) ) return;
111-
112119
// Get Time from iOS once per second
113120
// Note it is not advised to update this quickly
114121
// Application should use local clock and update time after
@@ -123,6 +130,8 @@ void loop()
123130

124131
void connect_callback(uint16_t conn_handle)
125132
{
133+
BLEConnection* conn = Bluefruit.Connection(conn_handle);
134+
126135
oled.clearDisplay();
127136
oled.setCursor(0, 0);
128137
oled.println("Connected.");
@@ -133,24 +142,42 @@ void connect_callback(uint16_t conn_handle)
133142
{
134143
oled.println("OK");
135144

136-
// ANCS requires pairing to work
145+
// Current Time Service requires pairing to work
146+
// request Pairing if not bonded
137147
oled.print("Paring ... ");
138-
139148
oled.display();
140149

141-
if ( Bluefruit.requestPairing(conn_handle) )
142-
{
143-
oled.println("OK");
150+
conn->requestPairing();
151+
}
152+
}
144153

154+
void connection_secured_callback(uint16_t conn_handle)
155+
{
156+
BLEConnection* conn = Bluefruit.Connection(conn_handle);
157+
158+
if ( !conn->secured() )
159+
{
160+
// It is possible that connection is still not secured by this time.
161+
// This happens (central only) when we try to encrypt connection using stored bond keys
162+
// but peer reject it (probably it remove its stored key).
163+
// Therefore we will request an pairing again --> callback again when encrypted
164+
conn->requestPairing();
165+
}
166+
else
167+
{
168+
oled.println("Secured");
169+
170+
if ( bleCTime.discovered() )
171+
{
145172
bleCTime.enableAdjust();
146173

147174
oled.println("Receiving Time...");
175+
oled.display();
176+
148177
bleCTime.getCurrentTime();
149178
bleCTime.getLocalTimeInfo();
150179
}
151180
}
152-
153-
oled.display();
154181
}
155182

156183
void printTime(void)

0 commit comments

Comments
 (0)