My requirement is only accept one bonding, reject other bondings unless the existing one removed from flash(client address)
Code: Select all
BLESecurity* pSecurity = new BLESecurity();
pSecurity->setAuthenticationMode(ESP_LE_AUTH_REQ_SC_MITM_BOND);
pSecurity->setCapability(ESP_IO_CAP_NONE);
BLEDevice::setSecurityCallbacks(new SecBLESecurityCallbacks());
uint32_t SecBLESecurityCallbacks::onPassKeyRequest() {
return 0;
}
void SecBLESecurityCallbacks::onPassKeyNotify(uint32_t pass_key) {
}
bool SecBLESecurityCallbacks::onSecurityRequest() {
Serial.println("SecBLESecurityCallbacks, onSecurityRequest");
int dev_num = esp_ble_get_bond_device_num();
if (dev_num > 0) {
Serial.println("rejected");
return false;
} else {
Serial.println("accepted");
return true;
}
}
void SecBLESecurityCallbacks::onAuthenticationComplete(esp_ble_auth_cmpl_t auth_cmpl) {
Serial.println("SecBLESecurityCallbacks, onAuthenticationComplete");
Serial.printf("addr: %s, success: %d\n",
BLEAddress(auth_cmpl.bd_addr).toString().c_str(),
auth_cmpl.success);
}
bool SecBLESecurityCallbacks::onConfirmPIN(uint32_t pin) {
return true;
}