h2zero / NimBLE-Arduino

A fork of the NimBLE library structured for compilation with Arduino, for use with ESP32, nRF5x.
https://h2zero.github.io/NimBLE-Arduino/
Apache License 2.0
670 stars 138 forks source link

Server can be found in scan, but connecting fails #569

Closed kinimodkoch closed 11 months ago

kinimodkoch commented 11 months ago

Hi h2zero, I have been using the nimble library as a client on an ESP32 for some time. Now after updating my server (ESP32-C3 with zephyr environment) to BLE 5.4, I can not connect any more. I can still receive the advertisement of the server, but the 'connect' function returns 'false'. I have the impression it might be an issue in the nimble library, because I can also not connect to a server I set up with the NRF connect app.

I created a minimal code example to reproduce the issue:

  String addr = "dd:a3:4a:e0:8a:a6";//esp32-C3 zephyr

  Serial.begin(115200);
  NimBLEDevice::init("");

  //scan to verify that we can see the the server
  Serial.println("Performing scan");
  NimBLEScan *pScan = NimBLEDevice::getScan();
  pScan->setActiveScan(true); // active scan uses more power, but get results faster
  pScan->setInterval(105);
  pScan->setWindow(104);
  NimBLEScanResults foundDevices = pScan->start(5, false);
  for (int i = 0; i < foundDevices.getCount(); i++)
  {
    if (strcmp(foundDevices.getDevice(i).getAddress().toString().c_str(), addr.c_str()) == 0)
    {
      Serial.println("I can see the device!");
    }
  }

  //now try to connect
  Serial.printf("Setting up client\n");
  NimBLEClient *blemClient = BLEDevice::createClient();
  blemClient->setClientCallbacks(&clientCallbacks, false);
  Serial.printf("Connecting to: %s\n", addr.c_str());
  bool success = blemClient->connect(BLEAddress(addr.c_str()));
  Serial.printf("Connect returned %i\n", success);

The full platformio project with this example: nimble-connect-issue-platformio-project.zip

With an ESP32 Arduino Nimble Server the program works fine:

rst:0x1 (POWERON_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:10124
load:0x40080400,len:5828
entry 0x400806a8
Performing scan
I can see the device!
Setting up client
Connecting to: 3c:61:05:0d:e3:5a
BLE connected!
3c:61:05:0d:e3:5aMTU: 255  Auth: 0  Bond: 0
Connect returned 1

With other servers, I can not connect. The servers I tested:

All these servers accept connections with NRF connect without any issue.

The output:

rst:0x1 (POWERON_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:10124
load:0x40080400,len:5828
entry 0x400806a8
Performing scan
I can see the device!
Setting up client
Connecting to: dd:a3:4a:e0:8a:a6
Connect returned 0

Between the last two lines ('connecting to' and 'connect returned 0'), there were a few seconds delay where it seemed to try to connect.

Note: connecting to a nimble server with any of my clients works.

Thanks in advance for your help!

Best regards Dominik

Screenshot_20230714-172415

kinimodkoch commented 11 months ago

This issue was about "Random Resolvable" MAC addresses that were enabled on the target server. I had to add the address type to the constructor of BLEAddress to make it work: BLEAddress("12:34:56:78:90", BLE_ADDR_RANDOM)