Secure BLE L2CAP Channel between iOS and Android

Hi,

I'm trying to establish a secure L2CAP channel between and iPhone and Android device. I'm able to do it for an insecure L2CAP channel but not a secure L2CAP channel, and the error that I'm getting back on the iOS side is not useful (Error code: 104, description: Unknown ATT error).

Here's what I'm doing on the Android side:

Code Block
serverSocket = adapter.listenUsingL2capChannel()
Log.d("DemoService", "<startL2CapService> socket psm=" + serverSocket.psm)
serverSocketExecutor.submit {
Log.d("DemoService", "<startL2CapService> Waiting for a connection...")
val socket = serverSocket.accept()
Log.d("DemoService", "<startL2CapService> accepted socket...")
}


I'm also posting the PSM from the server socket as a GATT characteristic. On the iOS side, I read that characteristic to get the PSM and attempt to open the L2CAP channel:

Code Block
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
print("Did update value for characteristic: \(characteristic)")
guard let rawValue = characteristic.value else { return }
print("raw value: \(rawValue)")
let psmInt: Int32 = rawValue.withUnsafeBytes {
$0.load(as: Int32.self)
}
let psm = CBL2CAPPSM(psmInt)
print("psm: \(psm)")
peripheral.openL2CAPChannel(psm)
}


If the two devices are not paired, then the pairing workflow gets triggered on the Android side and I pair the two devices. So, I know that my attempt at creating a secure L2CAP channel is at least triggering the secure flow. The problem is that the socket never gets created on the Android side (unless I instead use the insecure version) and on the iOS side, I get an error (Error code: 104, description: Unknown ATT error) in the delegate callback:

Code Block
func peripheral(_ peripheral: CBPeripheral, didOpen channel: CBL2CAPChannel?, error: Error?) {
print("Did open channel: \(String(describing: channel?.psm))")
if let error = error as NSError? {
print("Error code: \(error.code), description: \(error.localizedDescription)")
}
}


I've confirmed that I'm using the correct PSM, and this code will work if I instead listenUsingInsecureL2capChannel on the Android side.

Has anyone successfully opened a secure L2CAP channel between iOS and Android? If so, can you point me to the code and/or provide guidance on what I'm doing wrong.

Thanks,
Tom
Secure BLE L2CAP Channel between iOS and Android
 
 
Q