Skip to content

Commit ec3257b

Browse files
committed
clean up example
1 parent a1ab1a3 commit ec3257b

File tree

2 files changed

+63
-34
lines changed

2 files changed

+63
-34
lines changed

libraries/Bluefruit52Lib/examples/Central/central_pairing/central_pairing.ino

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ void setup()
165165
tft.setTextColor(COLOR_WHITE);
166166
tft.setTextSize(2);
167167
tft.setCursor(0, 0);
168-
tft.print("Scanning...");
168+
tft.println("Scanning ...");
169169
#endif
170170

171171
/* Start Central Scanning
@@ -213,7 +213,10 @@ void connect_callback(uint16_t conn_handle)
213213
Serial.println("Connected");
214214

215215
#if TFT_IN_USE != TFT_NO_DISPLAY
216-
tft.println("connected");
216+
tft.fillScreen(COLOR_BLACK);
217+
tft.setTextSize(2);
218+
tft.setCursor(0, 0);
219+
tft.println("Connected");
217220
#endif
218221

219222
// If we are not bonded with peer previously -> send pairing request
@@ -235,6 +238,10 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason)
235238
(void) reason;
236239

237240
Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX);
241+
242+
#if TFT_IN_USE != TFT_NO_DISPLAY
243+
tft.println("Scanning ...");
244+
#endif
238245
}
239246

240247
/**
@@ -244,14 +251,10 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason)
244251
*/
245252
void bleuart_rx_callback(BLEClientUart& uart_svc)
246253
{
247-
Serial.print("[RX]: ");
248-
249254
while ( uart_svc.available() )
250255
{
251256
Serial.print( (char) uart_svc.read() );
252257
}
253-
254-
Serial.println();
255258
}
256259

257260
bool pairing_passkey_callback(uint16_t conn_handle, uint8_t const passkey[6], bool match_request)
@@ -342,14 +345,14 @@ void connection_secured_callback(uint16_t conn_handle)
342345
if ( !conn->secured() )
343346
{
344347
// It is possible that connection is still not secured by this time.
345-
// This happens when we try to encrypt connection using stored bond keys
348+
// This happens (central only) when we try to encrypt connection using stored bond keys
346349
// but peer reject it (probably it remove its stored key).
347350
// Therefore we will request an pairing again --> callback again when encrypted
348351
conn->requestPairing();
349352
}
350353
else
351354
{
352-
Serial.println(" Secured");
355+
Serial.println("Secured");
353356

354357
#if TFT_IN_USE != TFT_NO_DISPLAY
355358
tft.setTextColor(COLOR_YELLOW);

libraries/Bluefruit52Lib/examples/Peripheral/pairing_display/pairing_display.ino

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,15 @@ void setup()
149149
Bluefruit.setTxPower(4); // Check bluefruit.h for supported values
150150
Bluefruit.setName(DEVICE_NAME);
151151

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+
152161
// To use dynamic PassKey for pairing, we need to have
153162
// - IO capacities at least DISPPLAY
154163
// - Register callback to display/print dynamic passkey for central
@@ -160,6 +169,9 @@ void setup()
160169
// Set complete callback to print the pairing result
161170
Bluefruit.Pairing.setCompleteCallback(pairing_complete_callback);
162171

172+
// Set connection secured callback, invoked when connection is encrypted
173+
Bluefruit.Pairing.setSecuredCallback(connection_secured_callback);
174+
163175
Bluefruit.Periph.setConnectCallback(connect_callback);
164176
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
165177

@@ -170,19 +182,20 @@ void setup()
170182
bleuart.setPermission(SECMODE_ENC_WITH_MITM);
171183
bleuart.begin();
172184

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-
180185
#if TFT_IN_USE != TFT_NO_DISPLAY
181186
tft.fillScreen(COLOR_BLACK);
182187
tft.setTextColor(COLOR_WHITE);
183188
tft.setTextSize(2);
184-
tft.println("Advertising ... ");
189+
tft.setCursor(0, 0);
190+
tft.print("Advertising...");
185191
#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();
186199
}
187200

188201
void startAdv(void)
@@ -235,6 +248,27 @@ void loop()
235248
}
236249
}
237250

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+
238272
// callback invoked when pairing passkey is generated
239273
// - passkey: 6 keys (without null terminator) for displaying
240274
// - 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
273307

274308
tft.setTextColor(COLOR_WHITE);
275309
tft.setTextSize(2);
310+
tft.println();
276311
#endif
277312

278313
// wait until either button is pressed
@@ -308,7 +343,7 @@ void pairing_complete_callback(uint16_t conn_handle, uint8_t auth_status)
308343
Serial.println("Failed");
309344
}
310345

311-
#if TFT_IN_USE != TFT_NO_DISPLAY
346+
#if TFT_IN_USE != TFT_NO_DISPLAY
312347
if (auth_status == BLE_GAP_SEC_STATUS_SUCCESS)
313348
{
314349
tft.setTextColor(COLOR_GREEN);
@@ -321,27 +356,18 @@ void pairing_complete_callback(uint16_t conn_handle, uint8_t auth_status)
321356

322357
tft.setTextColor(COLOR_WHITE);
323358
tft.setTextSize(2);
324-
#endif
359+
#endif
325360
}
326361

327-
// callback invoked when central connects
328-
void connect_callback(uint16_t conn_handle)
362+
void connection_secured_callback(uint16_t conn_handle)
329363
{
330-
// Get the reference to current connection
331-
BLEConnection* connection = Bluefruit.Connection(conn_handle);
364+
Serial.println("Secured");
332365

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
345371
}
346372

347373
/**

0 commit comments

Comments
 (0)