I can pair the devices individually fine using the GAP API to scan for them, implementing the ESP_BT_GAP_PIN_REQ_EVT GAP callback to respond with the appropriate PIN, and then using esp_hidh_dev_open to open them.
Once the devices are paired they will automatically try to reconnect to the ESP32 when they are switched back on. If I am only using a single Wii remote this happens perfectly; I don't need to do anything special beyond handle ESP_HIDH_OPEN_EVENT to make sure my application knows about the connected device.
If two Wii remotes try to connect at the same time, i.e. one starts reconnecting before the other one has fully reconnected then neither of them will connect. One way to trigger this is to have two devices actively connected (having switched them on one after another) and then reset the ESP32. If only one Wii remote was connected before it will reconnected properly, however if two are connected both Wii remotes will try to reconnect almost immediately, triggering the issue.
What is odd is that it looks like the Wii remotes seem to think they are connected, but because the ESP32 Bluetooth library hasn't reported a successful connection I can't do anything with them from my application code.
Here's an example log of the failure (scroll to the bottom for errors):
Code: Select all
I (27) boot: ESP-IDF v5.0 2nd stage bootloader
I (27) boot: compile time 15:38:22
I (28) boot: chip revision: v3.0
I (30) boot_comm: chip revision: 3, min. bootloader chip revision: 0
I (37) boot.esp32: SPI Speed : 40MHz
I (42) boot.esp32: SPI Mode : DIO
I (46) boot.esp32: SPI Flash Size : 4MB
I (51) boot: Enabling RNG early entropy source...
I (56) boot: Partition Table:
I (60) boot: ## Label Usage Type ST Offset Length
I (67) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (75) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (82) boot: 2 factory factory app 00 00 00010000 00100000
I (90) boot: End of partition table
I (94) boot_comm: chip revision: 3, min. application chip revision: 0
I (101) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=20688h (132744) map
I (137) esp_image: segment 1: paddr=000306b0 vaddr=3ffbdb60 size=049d0h ( 18896) load
I (142) esp_image: segment 2: paddr=00035088 vaddr=40080000 size=0af90h ( 44944) load
I (154) esp_image: segment 3: paddr=00040020 vaddr=400d0020 size=7b678h (505464) map
I (258) esp_image: segment 4: paddr=000bb6a0 vaddr=4008af90 size=0e340h ( 58176) load
I (274) esp_image: segment 5: paddr=000c99e8 vaddr=50000000 size=00010h ( 16) load
I (287) boot: Loaded app from partition at offset 0x10000
I (287) boot: Disabling RNG early entropy source...
I (298) cpu_start: Pro cpu up.
I (299) cpu_start: Starting app cpu, entry point is 0x400814b0
0x400814b0: call_start_cpu1 at D:/Documents/Documents/Electronics/ESP/esp-idf/components/esp_system/port/cpu_start.c:142
I (0) cpu_start: App cpu up.
I (315) cpu_start: Pro cpu start user code
I (315) cpu_start: cpu freq: 160000000 Hz
I (315) cpu_start: Application information:
I (320) cpu_start: Project name: wii_remote_host
I (325) cpu_start: App version: 82e6602-dirty
I (331) cpu_start: Compile time: Dec 28 2022 15:38:07
I (337) cpu_start: ELF file SHA256: e127f984b20ef496...
I (343) cpu_start: ESP-IDF: v5.0
I (348) heap_init: Initializing. RAM available for dynamic allocation:
I (355) heap_init: At 3FFAFF10 len 000000F0 (0 KiB): DRAM
I (361) heap_init: At 3FFB6388 len 00001C78 (7 KiB): DRAM
I (367) heap_init: At 3FFB9A20 len 00004108 (16 KiB): DRAM
I (373) heap_init: At 3FFC91A8 len 00016E58 (91 KiB): DRAM
I (379) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (386) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (392) heap_init: At 400992D0 len 00006D30 (27 KiB): IRAM
I (400) spi_flash: detected chip: generic
I (403) spi_flash: flash io: dio
I (408) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (441) BTDM_INIT: BT controller compile version [8020d24]
I (451) system_api: Base MAC address is not set
I (451) system_api: read default base MAC address from EFUSE
I (451) phy_init: phy_version 4670,719f9f6,Feb 18 2021,17:07:07
I (1131) BT: HIDH stack started
W (7021) BT_HCI: hcif conn complete: hdl 0x80, st 0x0
W (8091) BT_HCI: hcif conn complete: hdl 0x81, st 0x0
E (8171) BT_BTM: tBTM_SEC_DEV:0x3ffd17a8 rs_disc_pending=0
E (8471) BT_HIDH: OPEN ERROR: ERR_SDP
E (8471) BT: HIDH open failed (-1)
W (8471) BT_HIDH: HID-Host Rcvd L2CAP disc cfm, unknown CID: 0x44
W (8501) BT_SDP: process_service_attr_rsp
W (8831) BT_SDP: process_service_attr_rsp
Code: Select all
I (27) boot: ESP-IDF v5.0 2nd stage bootloader
I (27) boot: compile time 15:38:22
I (27) boot: chip revision: v3.0
I (30) boot_comm: chip revision: 3, min. bootloader chip revision: 0
I (37) boot.esp32: SPI Speed : 40MHz
I (42) boot.esp32: SPI Mode : DIO
I (46) boot.esp32: SPI Flash Size : 4MB
I (51) boot: Enabling RNG early entropy source...
I (56) boot: Partition Table:
I (60) boot: ## Label Usage Type ST Offset Length
I (67) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (75) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (82) boot: 2 factory factory app 00 00 00010000 00100000
I (90) boot: End of partition table
I (94) boot_comm: chip revision: 3, min. application chip revision: 0
I (101) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=20688h (132744) map
I (137) esp_image: segment 1: paddr=000306b0 vaddr=3ffbdb60 size=049d0h ( 18896) load
I (142) esp_image: segment 2: paddr=00035088 vaddr=40080000 size=0af90h ( 44944) load
I (154) esp_image: segment 3: paddr=00040020 vaddr=400d0020 size=7b678h (505464) map
I (258) esp_image: segment 4: paddr=000bb6a0 vaddr=4008af90 size=0e340h ( 58176) load
I (274) esp_image: segment 5: paddr=000c99e8 vaddr=50000000 size=00010h ( 16) load
I (287) boot: Loaded app from partition at offset 0x10000
I (287) boot: Disabling RNG early entropy source...
I (298) cpu_start: Pro cpu up.
I (299) cpu_start: Starting app cpu, entry point is 0x400814b0
0x400814b0: call_start_cpu1 at D:/Documents/Documents/Electronics/ESP/esp-idf/components/esp_system/port/cpu_start.c:142
I (0) cpu_start: App cpu up.
I (315) cpu_start: Pro cpu start user code
I (315) cpu_start: cpu freq: 160000000 Hz
I (315) cpu_start: Application information:
I (320) cpu_start: Project name: wii_remote_host
I (325) cpu_start: App version: 82e6602-dirty
I (331) cpu_start: Compile time: Dec 28 2022 15:38:07
I (337) cpu_start: ELF file SHA256: e127f984b20ef496...
I (343) cpu_start: ESP-IDF: v5.0
I (348) heap_init: Initializing. RAM available for dynamic allocation:
I (355) heap_init: At 3FFAFF10 len 000000F0 (0 KiB): DRAM
I (361) heap_init: At 3FFB6388 len 00001C78 (7 KiB): DRAM
I (367) heap_init: At 3FFB9A20 len 00004108 (16 KiB): DRAM
I (373) heap_init: At 3FFC91A8 len 00016E58 (91 KiB): DRAM
I (379) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (386) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (392) heap_init: At 400992D0 len 00006D30 (27 KiB): IRAM
I (400) spi_flash: detected chip: generic
I (403) spi_flash: flash io: dio
I (408) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (441) BTDM_INIT: BT controller compile version [8020d24]
I (451) system_api: Base MAC address is not set
I (451) system_api: read default base MAC address from EFUSE
I (451) phy_init: phy_version 4670,719f9f6,Feb 18 2021,17:07:07
I (1131) BT: HIDH stack started
W (19971) BT_HCI: hcif conn complete: hdl 0x80, st 0x0
W (20911) BT_HCI: hcif conn complete: hdl 0x81, st 0x0
E (21131) BT_BTM: tBTM_SEC_DEV:0x3ffd186c rs_disc_pending=0
W (21261) BT_SDP: process_service_attr_rsp
E (21381) BT_HIDH: OPEN ERROR: ERR_SDP
E (21381) BT: HIDH open failed (-1)
W (21391) BT_HIDH: HID-Host Rcvd L2CAP disc cfm, unknown CID: 0x42
W (21611) BT_SDP: process_service_attr_rsp
Code: Select all
W (93351) BT_APPL: bta_hh_co_close: dev_handle = 1, app_id = 5
W (93351) BT_APPL: bta_dm_act no entry for connected service cbs
E (93361) BT_BTC: Error: cannot find device with handle 1
E (93361) BT_HIDH: Device Not Found
E (93371) BT_HIDH: CLOSE ERROR: NO_CONNECTION
Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC : 0x400db050 PS : 0x00060130 A0 : 0x8014991c A1 : 0x3ffd3d30
0x400db050: hidh_callback at D:/Documents/Documents/GitHub/LightGunCommando/hosts/wii_remote/firmware/main/bt.c:217
A2 : 0x00000000 A3 : 0x3f405718 A4 : 0x00000004 A5 : 0x3ffd7894
A6 : 0x000000fd A7 : 0x0000abab A8 : 0x800db04e A9 : 0x3ffd3d10
A10 : 0x00016cbb A11 : 0x00000000 A12 : 0x00000000 A13 : 0xff000000
A14 : 0x00000004 A15 : 0x0000abab SAR : 0x00000018 EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000000 LBEG : 0x4000c2e0 LEND : 0x4000c2f6 LCOUNT : 0x00000000
Backtrace: 0x400db04d:0x3ffd3d30 0x40149919:0x3ffd3d90 0x401494b6:0x3ffd3dc0 0x4014958d:0x3ffd3e00 0x4009503d:0x3ffd3e20
0x400db04d: hidh_callback at D:/Documents/Documents/GitHub/LightGunCommando/hosts/wii_remote/firmware/main/bt.c:217
0x40149919: handler_execute at D:/Documents/Documents/Electronics/ESP/esp-idf/components/esp_event/esp_event.c:137
0x401494b6: esp_event_loop_run at D:/Documents/Documents/Electronics/ESP/esp-idf/components/esp_event/esp_event.c:582 (discriminator 3)
0x4014958d: esp_event_loop_run_task at D:/Documents/Documents/Electronics/ESP/esp-idf/components/esp_event/esp_event.c:107 (discriminator 15)
0x4009503d: vPortTaskWrapper at D:/Documents/Documents/Electronics/ESP/esp-idf/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:151
ELF file SHA256: e127f984b20ef496
Rebooting...