ESP32 Bluetooth Connection - Disconnect and Reconnect
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:
- Successfully connect to Windows via Bluetooth and Pair.
- Have the ability to reconnect to Windows after disconnecting (i.e after shutdown).
- Have the ability to disconnect from Windows if the user wishes to. I.E, get a similar prompt in Windows 11:
Extra Details:
- ESP-IDF Version : release/V4.4
- 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:
- Looked for any Bluetooth example in ESP-IDF connected successfully to Windows. I found one example that worked: ble_hid_device_demo
- 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.
- 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).
- Accordingly, I implemented these services and characteristics in nimble, utilizing the UUIDs that I got from here.
- 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:
- 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:
- 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:
- 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.
- 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?
- 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.