4

Android 10 released support for BLE CoC connection so I wanted to try this out by making two simple android 10 apps, which would connect to each other with l2Cap and exchange "Hello World".

I wrote two apps, Server app and Client app, both having all permissions they need in their manifest files, and I run that apps on two Android 10 phones, and the connection was not established.

Here is the relevant part of my server app code:

try {
    mServerSocket = bluetoothAdapter.listenUsingInsecureL2capChannel();
    int psm = mServerSocket.getPsm();
} catch (IOException e) {
    e.printStackTrace();
}

BluetoothSocket socket = mmServerSocket.accept();

Variable int psm is the PSM value I am using in client app.

Here is the relevant part of my client app code:

for (BluetoothDevice bluetoothDevice : deviceSet) {
    if (!bluetoothDevice.getName().equals(PAIRED_DEVICE_NAME)) continue;
    BluetoothDevice bd = adapter.getRemoteDevice(bluetoothDevice.getAddress());
    bluetoothSocket = bd.createInsecureL2capChannel(psm_value);
    break;
}

bluetoothSocket.connect();

where the string PAIRED_DEVICE_NAME is name of expected device, which is successfully found because devices are Bluetooth paired. Int psm_value is the PSM value from server app. I suspect this might be a problem because I hard-coded this value from Server every time I tried to test this (every time was different value because this value is dynamically assigned and it lasts until you close server socket).

So my question is how to get remote PSM value? And how to connect these devices, because if I am using RFCOMM connection, this code works perfectly. With this code I am getting error in bluetoothSocket.connect() line from client app saying:

java.io.IOException: read failed, socket might closed or timeout, read ret: -1

Thanks!

2
  • You can store the psm in some GATT characteristic that the client reads.
    – Emil
    Jan 16, 2020 at 18:05
  • Have you ever been able to get this done? I have pretty much the same problem. RFCOMM works but whenever I use L2CAP sockets it fails with the IOException you stated. Dec 22, 2020 at 18:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.