Skip to content

Commit d65525f

Browse files
committed
use arcada for pairing passkey
1 parent 3615893 commit d65525f

File tree

1 file changed

+88
-45
lines changed

1 file changed

+88
-45
lines changed

libraries/Bluefruit52Lib/examples/Peripheral/pairing_passkey/pairing_passkey.ino

Lines changed: 88 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,33 @@
1616
#define USE_ARCADA
1717
#endif
1818

19-
#ifdef USE_ARCADA
20-
#include <Adafruit_Arcada.h>
21-
22-
Adafruit_Arcada arcada;
23-
Adafruit_SPITFT* tft;
24-
#endif
25-
26-
#include <SPI.h>
27-
#include <Adafruit_GFX.h>
2819
#include <bluefruit.h>
2920
#include <Adafruit_LittleFS.h>
3021
#include <InternalFileSystem.h>
3122
#include <Adafruit_nRFCrypto.h>
3223

24+
#ifdef USE_ARCADA
25+
#include <Adafruit_Arcada.h>
26+
27+
Adafruit_Arcada arcada;
28+
Adafruit_SPITFT* tft;
29+
30+
#else
31+
// Use built-in buttons if available, else use A0, A1
32+
#ifdef PIN_BUTTON1
33+
#define BUTTON_YES PIN_BUTTON1
34+
#else
35+
#define BUTTON_YES A0
36+
#endif
37+
38+
#ifdef PIN_BUTTON2
39+
#define BUTTON_NO PIN_BUTTON2
40+
#else
41+
#define BUTTON_NO A1
42+
#endif
43+
#endif
44+
45+
3346
/* This sketch demonstrates Pairing process using dynamic Passkey.
3447
* This sketch is essentially the same as bleuart.ino except the BLE Uart
3548
* service requires Security Mode with Man-In-The-Middle protection i.e
@@ -59,6 +72,7 @@ void setup()
5972
Serial.println("Bluefruit52 Pairing Display Example");
6073
Serial.println("-----------------------------------\n");
6174

75+
#ifdef USE_ARCADA
6276
arcada.arcadaBegin();
6377
arcada.displayBegin();
6478
arcada.setBacklight(255);
@@ -67,6 +81,10 @@ void setup()
6781
tft->setCursor(0, 0);
6882
tft->setTextWrap(true);
6983
tft->setTextSize(2);
84+
#else
85+
pinMode(BUTTON_YES, INPUT_PULLUP);
86+
pinMode(BUTTON_NO, INPUT_PULLUP);
87+
#endif
7088

7189
// Setup the BLE LED to be enabled on CONNECT
7290
// Note: This is actually the default behavior, but provided
@@ -82,19 +100,23 @@ void setup()
82100
Bluefruit.setTxPower(4); // Check bluefruit.h for supported values
83101

84102
// clear bonds if BUTTON A is pressed
85-
Serial.println("Hold button A to clear bonds ..... ");
86-
delay(2000);
87-
if (0 == digitalRead(PIN_BUTTON1))
88-
{
89-
Serial.println("Clear all central bonds");
90-
Bluefruit.Periph.clearBonds();
91-
}
92-
93-
// To use dynamic PassKey for pairing, we need to have
94-
// - IO capacities at least DISPPLAY
95-
// - Register callback to display/print dynamic passkey for central
96-
// For complete mapping of the IO Capabilities to Key Generation Method, check out this article
97-
// https://www.bluetooth.com/blog/bluetooth-pairing-part-2-key-generation-methods/
103+
// Serial.println("Hold button A to clear bonds ..... ");
104+
// delay(2000);
105+
// if (0 == digitalRead(PIN_BUTTON1))
106+
// {
107+
// Serial.println("Clear all central bonds");
108+
// Bluefruit.Periph.clearBonds();
109+
// }
110+
111+
/* To use dynamic PassKey for pairing, we need to have
112+
* - IO capacities at least DISPPLAY
113+
* - Display only: user have to enter 6-digit passkey on their phone
114+
* - DIsplay + Yes/No: user ony need to press Accept on both central and device
115+
* - Register callback to display/print dynamic passkey for central
116+
*
117+
* For complete mapping of the IO Capabilities to Key Generation Method, check out this article
118+
* https://www.bluetooth.com/blog/bluetooth-pairing-part-2-key-generation-methods/
119+
*/
98120
Bluefruit.Pairing.setIOCaps(true, true, false); // display = true, yes/no = true, keyboard = false
99121
Bluefruit.Pairing.setPasskeyCallback(pairing_passkey_callback);
100122

@@ -222,42 +244,63 @@ bool pairing_passkey_callback(uint16_t conn_handle, uint8_t const passkey[6], bo
222244
tft->setTextSize(2);
223245
#endif
224246

225-
247+
// match_request means peer wait for our approval (return true)
226248
if (match_request)
227249
{
228250
Serial.println("Do you want to pair");
229-
Serial.println("Press Button A to accept, Button B to reject");
251+
Serial.println("Press Button Left to decline, Button Right to Accept");
230252

231-
#ifdef USE_ARCADA
232-
tft->println("\nDo you accept ?\n\n");
253+
// timeout for pressing button
254+
uint32_t start_time = millis();
233255

256+
#ifdef USE_ARCADA
257+
tft->println("\nDo you accept ?\n\n");
234258
tft->setTextSize(3);
235-
tft->setTextColor(ARCADA_GREEN);
236-
tft->print("<< Yes");
237-
tft->setTextColor(ARCADA_RED);
238-
tft->println(" No >>");
259+
260+
// Yes <-> No on CPB is reversed since GIZMO TFT is on the back of CPB
261+
#if ARDUINO_NRF52840_CIRCUITPLAY
262+
tft->setTextColor(ARCADA_GREEN);
263+
tft->print("< Yes");
264+
tft->setTextColor(ARCADA_RED);
265+
tft->println(" No >");
266+
#else
267+
tft->setTextColor(ARCADA_RED);
268+
tft->print("< No");
269+
tft->setTextColor(ARCADA_GREEN);
270+
tft->println(" Yes >");
271+
#endif
239272

240273
tft->setTextColor(ARCADA_WHITE);
241274
tft->setTextSize(2);
242275
tft->println();
243-
#endif
244276

245-
// wait until either button is pressed
246-
while( digitalRead(PIN_BUTTON1) && digitalRead(PIN_BUTTON2) ) { }
277+
// wait until either button is pressed (30 seconds timeout)
278+
uint32_t justReleased;
279+
do
280+
{
281+
if ( millis() > start_time + 30000 ) break;
247282

248-
// wait until either button is pressed
249-
uint32_t start_time = millis();
250-
while( digitalRead(PIN_BUTTON1) && digitalRead(PIN_BUTTON2) )
283+
arcada.readButtons();
284+
justReleased = arcada.justReleasedButtons();
285+
} while ( !(justReleased & (ARCADA_BUTTONMASK_LEFT | ARCADA_BUTTONMASK_RIGHT) ) );
286+
287+
// Right = accept
288+
if (justReleased & ARCADA_BUTTONMASK_RIGHT) return true;
289+
290+
// Left = decline
291+
if (justReleased & ARCADA_BUTTONMASK_LEFT) return false;
292+
293+
#else
294+
// wait until either button is pressed (30 seconds timeout)
295+
while( digitalRead(BUTTON_YES) && digitalRead(BUTTON_NO) )
251296
{
252-
// 30 seconds timeout
253297
if ( millis() > start_time + 30000 ) break;
254298
}
255299

256-
// A = accept
257-
if ( 0 == digitalRead(PIN_BUTTON1) ) return true;
300+
if ( 0 == digitalRead(BUTTON_YES) ) return true;
258301

259-
// B = reject
260-
if ( 0 == digitalRead(PIN_BUTTON2) ) return false;
302+
if ( 0 == digitalRead(BUTTON_NO) ) return false;
303+
#endif
261304

262305
return false;
263306
}
@@ -275,7 +318,7 @@ void pairing_complete_callback(uint16_t conn_handle, uint8_t auth_status)
275318
Serial.println("Failed");
276319
}
277320

278-
#ifdef USE_ARCADA
321+
#ifdef USE_ARCADA
279322
if (auth_status == BLE_GAP_SEC_STATUS_SUCCESS)
280323
{
281324
tft->setTextColor(ARCADA_GREEN);
@@ -288,18 +331,18 @@ void pairing_complete_callback(uint16_t conn_handle, uint8_t auth_status)
288331

289332
tft->setTextColor(ARCADA_WHITE);
290333
tft->setTextSize(2);
291-
#endif
334+
#endif
292335
}
293336

294337
void connection_secured_callback(uint16_t conn_handle)
295338
{
296339
Serial.println("Secured");
297340

298-
#ifdef USE_ARCADA
341+
#ifdef USE_ARCADA
299342
tft->setTextColor(ARCADA_YELLOW);
300343
tft->println("secured");
301344
tft->setTextColor(ARCADA_WHITE);
302-
#endif
345+
#endif
303346
}
304347

305348
/**

0 commit comments

Comments
 (0)