Skip to content

Commit e0cda21

Browse files
committed
remove AdafruitBluefruit::requestPairing
default CFG_DEFAULT_NAME to USB_PRODUCT
1 parent cc7de3d commit e0cda21

File tree

3 files changed

+38
-29
lines changed

3 files changed

+38
-29
lines changed

libraries/Bluefruit52Lib/examples/Central/central_hid/central_hid.ino

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ hid_mouse_report_t last_mse_report = { 0 };
3030
void setup()
3131
{
3232
Serial.begin(115200);
33-
while ( !Serial ) delay(10); // for nrf52840 with native usb
33+
// while ( !Serial ) delay(10); // for nrf52840 with native usb
3434

3535
Serial.println("Bluefruit52 Central HID (Keyboard + Mouse) Example");
3636
Serial.println("--------------------------------------------------\n");
@@ -55,6 +55,9 @@ void setup()
5555
Bluefruit.Central.setConnectCallback(connect_callback);
5656
Bluefruit.Central.setDisconnectCallback(disconnect_callback);
5757

58+
// Set connection secured callback, invoked when connection is encrypted
59+
Bluefruit.Pairing.setSecuredCallback(connection_secured_callback);
60+
5861
/* Start Central Scanning
5962
* - Enable auto scan if disconnected
6063
* - Interval = 100 ms, window = 80 ms
@@ -88,6 +91,8 @@ void scan_callback(ble_gap_evt_adv_report_t* report)
8891
*/
8992
void connect_callback(uint16_t conn_handle)
9093
{
94+
BLEConnection* conn = Bluefruit.Connection(conn_handle);
95+
9196
Serial.println("Connected");
9297

9398
Serial.print("Discovering HID Service ... ");
@@ -97,11 +102,31 @@ void connect_callback(uint16_t conn_handle)
97102
Serial.println("Found it");
98103

99104
// HID device mostly require pairing/bonding
100-
if ( !Bluefruit.requestPairing(conn_handle) )
101-
{
102-
Serial.print("Failed to paired");
103-
return;
104-
}
105+
conn->requestPairing();
106+
}else
107+
{
108+
Serial.println("Found NONE");
109+
110+
// disconnect since we couldn't find blehid service
111+
conn->disconnect();
112+
}
113+
}
114+
115+
void connection_secured_callback(uint16_t conn_handle)
116+
{
117+
BLEConnection* conn = Bluefruit.Connection(conn_handle);
118+
119+
if ( !conn->secured() )
120+
{
121+
// It is possible that connection is still not secured by this time.
122+
// This happens (central only) when we try to encrypt connection using stored bond keys
123+
// but peer reject it (probably it remove its stored key).
124+
// Therefore we will request an pairing again --> callback again when encrypted
125+
conn->requestPairing();
126+
}
127+
else
128+
{
129+
Serial.println("Secured");
105130

106131
// https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.hid_information.xml
107132
uint8_t hidInfo[4];
@@ -120,15 +145,9 @@ void connect_callback(uint16_t conn_handle)
120145

121146
// Enable Mouse report notification if present on prph
122147
if ( hid.mousePresent() ) hid.enableMouse();
123-
148+
124149
Serial.println("Ready to receive from peripheral");
125-
}else
126-
{
127-
Serial.println("Found NONE");
128-
129-
// disconnect since we couldn't find blehid service
130-
Bluefruit.disconnect(conn_handle);
131-
}
150+
}
132151
}
133152

134153
/**

libraries/Bluefruit52Lib/src/bluefruit.cpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@
4141
#define CFG_BLE_TX_POWER_LEVEL 0
4242
#endif
4343

44-
#ifndef CFG_DEFAULT_NAME
45-
#define CFG_DEFAULT_NAME "Bluefruit52"
44+
#ifdef USB_PRODUCT
45+
#define CFG_DEFAULT_NAME USB_PRODUCT
46+
#else
47+
#define CFG_DEFAULT_NAME "Feather nRF52832"
4648
#endif
4749

4850
#ifndef CFG_BLE_TASK_STACKSIZE
@@ -916,17 +918,6 @@ void AdafruitBluefruit::_setConnLed (bool on_off)
916918
}
917919
}
918920

919-
/*------------------------------------------------------------------*/
920-
/* Bonds
921-
*------------------------------------------------------------------*/
922-
bool AdafruitBluefruit::requestPairing(uint16_t conn_hdl)
923-
{
924-
BLEConnection* conn = this->Connection(conn_hdl);
925-
VERIFY(conn);
926-
927-
return conn->requestPairing();
928-
}
929-
930921
//--------------------------------------------------------------------+
931922
//
932923
//--------------------------------------------------------------------+

libraries/Bluefruit52Lib/src/bluefruit.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,10 @@ class AdafruitBluefruit
164164
bool connected (uint16_t conn_hdl);
165165

166166
uint16_t connHandle (void);
167-
bool connPaired (uint16_t conn_hdl);
168167

169168
// Alias to BLEConnection API()
169+
bool connPaired (uint16_t conn_hdl);
170170
bool disconnect (uint16_t conn_hdl);
171-
bool requestPairing (uint16_t conn_hdl);
172171

173172
uint16_t getMaxMtu(uint8_t role);
174173

0 commit comments

Comments
 (0)