BLE: Correct way to reject new bonding?

rockllei
Posts: 3
Joined: Tue Mar 15, 2022 5:19 am

BLE: Correct way to reject new bonding?

Postby rockllei » Tue Mar 15, 2022 5:31 am

I am using the BLESecurityCallbacks::onSecurityRequest() to reject new bonding, but it seems that BLEServerCallbacks::onConnect will still be called event BLESecurityCallbacks::onSecurityRequest() returns false, and somethimes this will break Mac's SystemPreferences, i am testing on Mac M1 Pro.

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;
}
  
What's the correct way to reject new bonding? Thanks!

Who is online

Users browsing this forum: No registered users and 124 guests