Skip to content

Commit 3987377

Browse files
committed
Add section on handling re-encryption failures in README.md
1 parent 3b78dd0 commit 3987377

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,75 @@ void setup() {
200200
}
201201
```
202202
203+
## Handling Re-encryption Failures
204+
205+
### Problem
206+
207+
When a Pico device has been bonded with a central device (like a smartphone) and then the Pico is flashed with new firmware, the following error sequence may occur when attempting to reconnect:
208+
209+
```
210+
Re-encryption started with bonded device
211+
Pairing started
212+
Re-encryption failed, status: 61
213+
Pairing failed
214+
```
215+
216+
This occurs because:
217+
1. The central device (smartphone) still has the bonding information
218+
2. The Pico has lost all bonding data after being flashed
219+
3. The central device attempts to use the old bonding keys to re-establish a secure connection
220+
4. The pairing fails because the Pico no longer recognizes these keys
221+
222+
### Solutions
223+
224+
There are several approaches to handle this situation:
225+
226+
#### Option 1: Remove Bonding on Central Device
227+
228+
The simplest approach for development is to remove the bonded device from your smartphone/central device:
229+
230+
- **On Android**: Go to Settings → Bluetooth → Previously Connected Devices → Tap the gear or arrow icon next to your device → "Forget" or "Unpair"
231+
- **On iOS**: Go to Settings → Bluetooth → Tap the "i" next to your device → "Forget This Device"
232+
233+
#### Option 2: Implement Persistent Bonding Storage
234+
235+
For production devices, consider implementing persistent storage of bonding information:
236+
- Store bonding keys in flash memory (e.g., using the LittleFS on Pico)
237+
- Restore bonding information after firmware updates
238+
- See the [arduino-pico documentation](https://arduino-pico.readthedocs.io/en/latest/fs.html) for file system usage
239+
240+
#### Option 3: Clear Bond on Reconnection Failure
241+
242+
Implement automatic bond clearing after a failed reconnection attempt:
243+
244+
```cpp
245+
// In your onPairingStatus callback
246+
void onPairingStatus(BLEPairingStatus status, BLEDevice *device) {
247+
switch (status) {
248+
case PAIRING_FAILED:
249+
Serial.println("Pairing failed - attempting to clear existing bond");
250+
// In a real application, you might want to:
251+
// 1. Request the central device to forget the bond
252+
// 2. Try a fresh pairing after a delay
253+
254+
// For now, we can just disconnect to force a fresh connection
255+
if (device) {
256+
gap_disconnect(device->getHandle());
257+
}
258+
break;
259+
// Other cases...
260+
}
261+
}
262+
```
263+
264+
### Recommendation for Development
265+
266+
During development, use Option 1 (removing the bond on the central device) for simplicity. For production devices, consider implementing a more robust solution with persistent storage (Option 2).
267+
268+
### Important Note
269+
270+
This behavior is common in BLE development and not specific to this library. Any time bonding information is lost on either the peripheral or central device, re-encryption will fail, and the bond must be reestablished.
271+
203272
## Migration Guide
204273

205274
### Version 1.X to 2.X

0 commit comments

Comments
 (0)