@@ -30,7 +30,7 @@ hid_mouse_report_t last_mse_report = { 0 };
30
30
void setup ()
31
31
{
32
32
Serial.begin (115200 );
33
- while ( !Serial ) delay (10 ); // for nrf52840 with native usb
33
+ // while ( !Serial ) delay(10); // for nrf52840 with native usb
34
34
35
35
Serial.println (" Bluefruit52 Central HID (Keyboard + Mouse) Example" );
36
36
Serial.println (" --------------------------------------------------\n " );
@@ -55,6 +55,9 @@ void setup()
55
55
Bluefruit.Central .setConnectCallback (connect_callback);
56
56
Bluefruit.Central .setDisconnectCallback (disconnect_callback);
57
57
58
+ // Set connection secured callback, invoked when connection is encrypted
59
+ Bluefruit.Pairing .setSecuredCallback (connection_secured_callback);
60
+
58
61
/* Start Central Scanning
59
62
* - Enable auto scan if disconnected
60
63
* - Interval = 100 ms, window = 80 ms
@@ -88,6 +91,8 @@ void scan_callback(ble_gap_evt_adv_report_t* report)
88
91
*/
89
92
void connect_callback (uint16_t conn_handle)
90
93
{
94
+ BLEConnection* conn = Bluefruit.Connection (conn_handle);
95
+
91
96
Serial.println (" Connected" );
92
97
93
98
Serial.print (" Discovering HID Service ... " );
@@ -97,11 +102,31 @@ void connect_callback(uint16_t conn_handle)
97
102
Serial.println (" Found it" );
98
103
99
104
// 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" );
105
130
106
131
// https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.hid_information.xml
107
132
uint8_t hidInfo[4 ];
@@ -120,15 +145,9 @@ void connect_callback(uint16_t conn_handle)
120
145
121
146
// Enable Mouse report notification if present on prph
122
147
if ( hid.mousePresent () ) hid.enableMouse ();
123
-
148
+
124
149
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
+ }
132
151
}
133
152
134
153
/* *
0 commit comments