I want to open RFCOMM socket connection between two devices, but nothing that I tried worked. This is what I have:
- Python script uses PyBluez to advertise SPP service and waits for connections.
- Android application tries to find that service and connect to the device.
Server side
Bluetooth server script advertises serial port profile service. The service UUID used is 00001101-0000-1000-8000-00805F9B34FB
.
bluetooth.advertise_service(
sock=self.server_socket,
name="My Service",
service_id=self.service_uuid,
service_classes=[self.service_uuid, bluetooth.SERIAL_PORT_CLASS],
profiles=[bluetooth.SERIAL_PORT_PROFILE],
description="My service description.",
)
Client
I can find the device to which I want to connect. To make a connection, I followed official Android documentation Connect Bluetooth devices / Connect as a client and used the same UUID as used on the server side. However, I get an exception java.io.IOException: read failed, socket might closed or timeout, read ret: -1
.
Also, when scanning SDP server of the device that I want to connect to, I do find 00001101-0000-1000-8000-00805F9B34FB
service UUID, but it seems to be a default value, as it does not matter whether I advertise the service or not. Also, if I change the service UUID to something else, it does not show up. I tried clearing the cache both on both devices, but ti doesn't not change anything.
Moreover, I tried using BluetoothProfile.ServiceListener
to get the information about the advertised services, but it does not find the serial port profile service that I advertise through the Python script that I mentioned earlier.
public class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
private final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
public ConnectThread(BluetoothDevice device, String serviceUuid) {
BluetoothSocket tempSocket = null;
mmDevice = device;
try {
UUID uuid = UUID.fromString(serviceUuid);
tempSocket = device.createRfcommSocketToServiceRecord(serviceUuid);
} catch (IOException e) {
Log.e("BluetoothClassicModule", "Socket's create() method failed", e);
}
mmSocket = tempSocket;
}
public void run() {
bluetoothAdapter.cancelDiscovery();
try {
Log.d("BluetoothClassicModule", "[Thread] Began connecting to device " + mmDevice.getName());
mmSocket.connect();
} catch (IOException connectException) {
Log.e("BluetoothClassicModule", "[THREAD] Could not connect to device.", connectException);
try {
mmSocket.close();
} catch (IOException closeException) {
Log.e("BluetoothClassicModule", "[THREAD] Could not close the client socket.", closeException);
}
return;
}
Log.d("BluetoothClassicModule", "[THREAD] Connected successfully.");
}
}
How could I make it work or at least debug this? I want to:
- Find the device that advertises service with serial port profile and specific service UUID.
- Establish RFCOMM connection to that device from Android application.