ESP32 Bluetooth Connection - Disconnect and Reconnect

Ahmad Amine 0 Reputation points
2023-10-11T02:10:03.3366667+00:00

Service: NimBLE Bluetooth Connection

Scenario:

I am trying to program an ESP32 that can connect to Windows via Bluetooth using NimBLE. The device should be capable of the following:

  1. Successfully connect to Windows via Bluetooth and Pair.
  2. Have the ability to reconnect to Windows after disconnecting (i.e after shutdown).
  3. Have the ability to disconnect from Windows if the user wishes to. I.E, get a similar prompt in Windows 11:image

Extra Details:

  1. ESP-IDF Version : release/V4.4
  2. Hardware: ESP32-S3-WROOM-1

Hello,

I had been facing issues getting a stable NimBLE Bluetooth connection between my ESP32 and Windows. The reference ESP-IDF example was not working. After a lot of experimenting, I managed to get a stable connection with Windows using NimBLE. These are the steps that I took in debugging the problem:

  1. Looked for any Bluetooth example in ESP-IDF connected successfully to Windows. I found one example that worked: ble_hid_device_demo
  2. After a bit of experimentation, I found that the services and characteristics that were needed to get the connection to work were HID Service (Characteristics: Report, Report Map, HID Information, HID Control Point), Battery Service (Characteristic: Battery Value), and Device Information Service.
  3. This finding was confirmed after some investigation into Windows documentation (here) and a nice reference that summarizes these requirements (here - see Table 3.1, 2.1).
  4. Accordingly, I implemented these services and characteristics in nimble, utilizing the UUIDs that I got from here.
  5. After implementing these services in NimBLE, I was able to connect successfully to the esp32-s3 without any disconnection.

With all that said, the connection is not yet perfect as I still have the following two issues:

  1. I am unable to disconnect from the device once a connection has been made. I suspect I need to add a characteristic or service to enable this behavior, but I am not sure which to add. For reference, see the photos below:
    These are the connection options that I get for my esp32:
    image
  2. Since the device is paired, Windows attempts to connect to the device once it has been detected via Bluetooth. However, I get stuck in a connect-disconnect loop where Windows attempts to connect to the ESP but then fails and disconnects. I get the following logs:
    I (6122) NimBLE: PHY updated rx 1, tx 1
    I (6367) NimBLE: disconnect; reason=531

I realize that resolving this issue might have to do with ESP-IDF related code, and as such I have already asked a similar question in an ESP-IDF issue thread.

What I need help with are the following questions:

  1. Is there a reference by Windows on the services and characteristics that are expected by Windows for a general Bluetooth device? Something similar to the Windows guide for Pen Devices.
  2. Can you confirm if the lack of a "Disconnect" button for my device has to do with a missing Bluetooth service? If it is not, what should be implemented on my end to enable this feature?
  3. Windows seems to be only updating PHY when the paired device is detected. Does Windows expect the reconnect to be handled in that event?

I will shortly add logs and code for replication of the scenario.

Thank you for your time.

Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
8,934 questions
0 comments No comments
{count} votes