|
| 1 | +//This example code is in the Public Domain (or CC0 licensed, at your option.) |
| 2 | +//By Victor Tchistiak - 2019 |
| 3 | +// |
| 4 | +//This example demonstrates reading and removing paired devices stored on the ESP32 flash memory |
| 5 | +//Sometimes you may find your ESP32 device could not connect to the remote device despite |
| 6 | +//many successful connections earlier. This is most likely a result of client replacing your paired |
| 7 | +//device info with new one from other device. The BT clients store connection info for paired devices, |
| 8 | +//but it is limited to a few devices only. When new device pairs and number of stored devices is exceeded, |
| 9 | +//one of the previously paired devices would be replaced with new one. |
| 10 | +//The only remedy is to delete this saved bound device from your device flash memory |
| 11 | +//and pair with the other device again. |
| 12 | +// |
| 13 | +#include "esp_bt_main.h" |
| 14 | +#include "esp_bt_device.h" |
| 15 | +#include"esp_gap_bt_api.h" |
| 16 | +#include "esp_err.h" |
| 17 | + |
| 18 | +#define REMOVE_BONDED_DEVICES 0 // <- Set to 0 to view all bonded devices addresses, set to 1 to remove |
| 19 | + |
| 20 | +#define PAIR_MAX_DEVICES 20 |
| 21 | +uint8_t pairedDeviceBtAddr[PAIR_MAX_DEVICES][6]; |
| 22 | +char bda_str[18]; |
| 23 | + |
| 24 | +bool initBluetooth() |
| 25 | +{ |
| 26 | + if(!btStart()) { |
| 27 | + Serial.println("Failed to initialize controller"); |
| 28 | + return false; |
| 29 | + } |
| 30 | + |
| 31 | + if(esp_bluedroid_init() != ESP_OK) { |
| 32 | + Serial.println("Failed to initialize bluedroid"); |
| 33 | + return false; |
| 34 | + } |
| 35 | + |
| 36 | + if(esp_bluedroid_enable() != ESP_OK) { |
| 37 | + Serial.println("Failed to enable bluedroid"); |
| 38 | + return false; |
| 39 | + } |
| 40 | + return true; |
| 41 | +} |
| 42 | + |
| 43 | +char *bda2str(const uint8_t* bda, char *str, size_t size) |
| 44 | +{ |
| 45 | + if (bda == NULL || str == NULL || size < 18) { |
| 46 | + return NULL; |
| 47 | + } |
| 48 | + sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x", |
| 49 | + bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]); |
| 50 | + return str; |
| 51 | +} |
| 52 | + |
| 53 | +void setup() { |
| 54 | + Serial.begin(115200); |
| 55 | + |
| 56 | + initBluetooth(); |
| 57 | + Serial.print("ESP32 bluetooth address: "); Serial.println(bda2str(esp_bt_dev_get_address(), bda_str, 18)); |
| 58 | + // Get the numbers of bonded/paired devices in the BT module |
| 59 | + int count = esp_bt_gap_get_bond_device_num(); |
| 60 | + if(!count) { |
| 61 | + Serial.println("No bonded device found."); |
| 62 | + } else { |
| 63 | + Serial.print("Bonded device count: "); Serial.println(count); |
| 64 | + if(PAIR_MAX_DEVICES < count) { |
| 65 | + count = PAIR_MAX_DEVICES; |
| 66 | + Serial.print("Reset bonded device count: "); Serial.println(count); |
| 67 | + } |
| 68 | + esp_err_t tError = esp_bt_gap_get_bond_device_list(&count, pairedDeviceBtAddr); |
| 69 | + if(ESP_OK == tError) { |
| 70 | + for(int i = 0; i < count; i++) { |
| 71 | + Serial.print("Found bonded device # "); Serial.print(i); Serial.print(" -> "); |
| 72 | + Serial.println(bda2str(pairedDeviceBtAddr[i], bda_str, 18)); |
| 73 | + if(REMOVE_BONDED_DEVICES) { |
| 74 | + esp_err_t tError = esp_bt_gap_remove_bond_device(pairedDeviceBtAddr[i]); |
| 75 | + if(ESP_OK == tError) { |
| 76 | + Serial.print("Removed bonded device # "); |
| 77 | + } else { |
| 78 | + Serial.print("Failed to remove bonded device # "); |
| 79 | + } |
| 80 | + Serial.println(i); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +void loop() {} |
0 commit comments