I want to open a L2CAP channel with my iOS device. The Swift app want’s to open a channel with an ESP32 (running with ESP-IDF code). But when trying to open a channel, I get an unknown error.
The ESP32 device gives me the log:
I (2000) L2CAP: L2CAP server successfully created with PSM 0x0193
When I try to open the channel in my Swift app with:
let psmIdentifier: CBL2CAPPSM = 0x0193
peripheral.openL2CAPChannel(psmIdentifier)
This code:
func peripheral(_ peripheral: CBPeripheral, didOpen channel: CBL2CAPChannel?, error: Error?) {
if let error = error {
let nsError = error as NSError
print("Kon L2CAP-kanaal niet openen: (error.localizedDescription)")
print("Error code: (nsError.code)")
print("Error domain: (nsError.domain)")
print("User info: (nsError.userInfo)")
return
}
guard let channel = channel else {
print("L2CAP-kanaal is nil, maar geen error object ontvangen.")
return
}
print("L2CAP-kanaal geopend, PSM: (channel.psm)")
}
Prints this:
Kon L2CAP-kanaal niet openen: Unknown error.
Error code: 0Error domain: CBErrorDomain
User info: ["NSLocalizedDescription": Unknown error.]
What could be the problem? The L2CAP event handler on the ESP32 is never called. I register the event handler with this:
void ble_l2cap_register_custom_psm(void) {
int rc = ble_l2cap_create_server(CUSTOM_PSM, L2CAP_MTU, ble_l2cap_event_handler, NULL);
if (rc != 0) {
ESP_LOGE("L2CAP", "Failed to create L2CAP server: rc=%d", rc);
} else {
ESP_LOGI("L2CAP", "L2CAP server successfully created with PSM 0x%04X", CUSTOM_PSM);
}
}