ESP-IDF BLE

gandalfsbeard
Posts: 1
Joined: Mon May 06, 2024 3:33 pm

ESP-IDF BLE

Postby gandalfsbeard » Mon May 06, 2024 3:52 pm

Trying to connect to a peripheral which requires PIN for bonding.

At connection attempt, I get an ESP_GAP_BLE_SEC_REQ_EVT, which is followed by ESP_GAP_BLE_AUTH_CMPL_EVT with fail_reason: 80. I couldn't find a reference for this error code, I don't know what it means.

Debug output of the connection attempt and full code are attached below.

Please help if you can, this is driving me nuts. Thank you.

Code: Select all

[ 11887][V][BLEClient.cpp:98] connect(): >> connect(00:00:00:12:00:00)
[ 11887][I][BLEDevice.cpp:622] addPeerDevice(): add conn_id: 0, GATT role: client
[ 11892][V][FreeRTOS.cpp:189] take(): Semaphore taking: name: RegEvt (0x3ffdc5ac), owner: <N/A> for connect
[ 11902][V][FreeRTOS.cpp:198] take(): Semaphore taken:  name: RegEvt (0x3ffdc5ac), owner: connect
[ 11910][V][FreeRTOS.cpp:63] wait(): >> wait: Semaphore waiting: name: RegEvt (0x3ffdc5ac), owner: connect for connect
[ 11911][D][BLEDevice.cpp:148] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... ESP_GATTC_REG_EVT
[ 11931][V][BLEUtils.cpp:1284] dumpGattClientEvent(): GATT Event: ESP_GATTC_REG_EVT
[ 11939][V][BLEUtils.cpp:1445] dumpGattClientEvent(): [status: ESP_GATT_OK, app_id: 0x0]
[ 11946][D][BLEClient.cpp:178] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... ESP_GATTC_REG_EVT
[ 11957][V][FreeRTOS.cpp:143] give(): Semaphore giving: name: RegEvt (0x3ffdc5ac), owner: connect
[ 11965][V][FreeRTOS.cpp:77] wait(): << wait: Semaphore released: name: RegEvt (0x3ffdc5ac), owner: <N/A>
[ 11975][V][FreeRTOS.cpp:189] take(): Semaphore taking: name: OpenEvt (0x3ffdc610), owner: <N/A> for connect
[ 11984][V][FreeRTOS.cpp:198] take(): Semaphore taken:  name: OpenEvt (0x3ffdc610), owner: connect
[ 11993][V][FreeRTOS.cpp:63] wait(): >> wait: Semaphore waiting: name: OpenEvt (0x3ffdc610), owner: connect for connect
[ 12134][V][BLEUtils.cpp:1049] dumpGapEvent(): Received a GAP event: ESP_GAP_BLE_SEC_REQ_EVT
[ 12136][V][BLEUtils.cpp:1260] dumpGapEvent(): [bd_addr: 00:00:00:12:00:00]
[ 12140][I][BLEDevice.cpp:222] gapEventHandler(): ESP_GAP_BLE_SEC_REQ_EVT
onSecurityRequest
[ 12157][D][BLEClient.cpp:493] handleGAPEvent(): BLEClient ... handling GAP event!
[ 12158][D][BLEDevice.cpp:148] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... ESP_GATTC_CONNECT_EVT
[ 12169][V][BLEUtils.cpp:1284] dumpGattClientEvent(): GATT Event: ESP_GATTC_CONNECT_EVT
[ 12176][V][BLEUtils.cpp:1312] dumpGattClientEvent(): [conn_id: 0, remote_bda: 00:00:00:12:00:00]
[ 12185][D][BLEClient.cpp:178] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... ESP_GATTC_CONNECT_EVT
[ 12196][D][BLEDevice.cpp:148] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... ESP_GATTC_OPEN_EVT
[ 12208][V][BLEUtils.cpp:1284] dumpGattClientEvent(): GATT Event: ESP_GATTC_OPEN_EVT
[ 12214][V][BLEUtils.cpp:1403] dumpGattClientEvent(): [status: ESP_GATT_OK, conn_id: 0, remote_bda: 00:00:00:12:00:00, mtu: 23]
[ 12225][D][BLEClient.cpp:178] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... ESP_GATTC_OPEN_EVT
onConnect
[ 12246][V][FreeRTOS.cpp:143] give(): Semaphore giving: name: OpenEvt (0x3ffdc610), owner: connect
[ 12246][V][FreeRTOS.cpp:77] wait(): << wait: Semaphore released: name: OpenEvt (0x3ffdc610), owner: <N/A>
[ 12254][V][BLEClient.cpp:149] connect(): << connect(), rc=1
[ 12408][V][BLEUtils.cpp:1049] dumpGapEvent(): Received a GAP event: ESP_GAP_BLE_AUTH_CMPL_EVT
[ 12409][V][BLEUtils.cpp:1106] dumpGapEvent(): [bd_addr: 00:00:00:12:00:00, key_present: 0, key: ***, key_type: 0, success: 0, fail_reason: 80, addr_type: ***, 
dev_type: ESP_BT_DEVICE_TYPE_BLE]
[ 12425][I][BLEDevice.cpp:253] gapEventHandler(): ESP_GAP_BLE_AUTH_CMPL_EVT
onAuthenticationComplete 0
[ 12443][D][BLEClient.cpp:493] handleGAPEvent(): BLEClient ... handling GAP event!
[ 12444][D][BLEDevice.cpp:148] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... ESP_GATTC_DISCONNECT_EVT
[ 12453][V][BLEUtils.cpp:1284] dumpGattClientEvent(): GATT Event: ESP_GATTC_DISCONNECT_EVT
[ 12461][V][BLEUtils.cpp:1327] dumpGattClientEvent(): [reason: Unknown, conn_id: 0, remote_bda: 00:00:00:12:00:00]
[ 12471][D][BLEClient.cpp:178] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... ESP_GATTC_DISCONNECT_EVT
[ 12482][V][FreeRTOS.cpp:143] give(): Semaphore giving: name: OpenEvt (0x3ffdc610), owner: <N/A>
[ 12491][V][FreeRTOS.cpp:143] give(): Semaphore giving: name: RssiCmplEvt (0x3ffdc6d8), owner: <N/A>
[ 12499][V][FreeRTOS.cpp:143] give(): Semaphore giving: name: SearchCmplEvt (0x3ffdc674), owner: <N/A>
[ 12508][I][BLEDevice.cpp:637] removePeerDevice(): remove: 0, GATT role client
onDisconnect
[ 12526][D][BLEDevice.cpp:148] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... ESP_GATTC_CLOSE_EVT
[ 12527][V][BLEUtils.cpp:1284] dumpGattClientEvent(): GATT Event: ESP_GATTC_CLOSE_EVT
[ 12535][V][BLEUtils.cpp:1298] dumpGattClientEvent(): [status: ESP_GATT_OK, reason:Unknown, conn_id: 0]
[ 12544][D][BLEDevice.cpp:148] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... ESP_GATTC_UNREG_EVT
[ 12554][V][BLEUtils.cpp:1284] dumpGattClientEvent(): GATT Event: ESP_GATTC_UNREG_EVT

Code: Select all

#include <Arduino.h>

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>

#define MY_TARGET_DEVICE_NAME "SPL556852"
#define PIN 776623

BLEClient *pBLEClient = nullptr;
BLEScan *pBLEScan = nullptr;
BLEAdvertisedDevice *pBLEDevice = nullptr;

class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
{

  void onResult(BLEAdvertisedDevice advertisedDevice)
  {
    Serial.println("onResult");
    if (advertisedDevice.getName() == MY_TARGET_DEVICE_NAME)
    {
      pBLEDevice = new BLEAdvertisedDevice(advertisedDevice);
    }
  }
};

class MyClientCallback : public BLEClientCallbacks
{
  void onConnect(BLEClient *pclient)
  {
    Serial.println("onConnect");
  }

  void onDisconnect(BLEClient *pclient)
  {
    Serial.println("onDisconnect");
  }
};

class MySecurityCallback : public BLESecurityCallbacks
{
  uint32_t onPassKeyRequest()
  {
    Serial.println("onPassKeyRequest");
    return PIN;
  }

  void onPassKeyNotify(uint32_t pass_key)
  {
    Serial.println("onPassKeyNotify");
  }

  bool onSecurityRequest()
  {
    Serial.println("onSecurityRequest");
    return true;
  }

  void onAuthenticationComplete(esp_ble_auth_cmpl_t t)
  {
    Serial.print("onAuthenticationComplete ");
    Serial.println(t.success);
  }

  bool onConfirmPIN(uint32_t pin)
  {
    Serial.println("onConfirmPIN");
    return true;
  }
};

void scan()
{
  Serial.println("scan");
  pBLEScan->start(1, false);
}

void connect()
{
  Serial.println("connect");
  pBLEClient->connect(pBLEDevice);
}

void setup()
{
  Serial.begin(115200);
  Serial.println("setup");

  BLEDevice::init("");
  BLEDevice::setSecurityCallbacks(new MySecurityCallback());

  pBLEScan = BLEDevice::getScan();
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setActiveScan(true);

  pBLEClient = BLEDevice::createClient();
  pBLEClient->setClientCallbacks(new MyClientCallback());

  scan();

  if (pBLEDevice != nullptr) {
    connect();
  }
}

void loop()
{

}

Who is online

Users browsing this forum: Bing [Bot] and 94 guests