androidbluetoothbluecove

Bluetooth socket connection with Bluetooth PC server fails "ava.io.IOException: read failed, socket might closed or timeout, read ret: -1"


I am trying to create a connection between an Android smartphone (Client) with a Bluetooth app (server) running on a PC.

Below is the code snippet for Client

    private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

    private class ConnectThread extends Thread {
    private BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;

    public ConnectThread(BluetoothDevice device) {
        mmDevice = device;
        BluetoothSocket tmp = null;

        try {
            //tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
            tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);

        } catch (IOException e) {
            Log.d(TAG, "create() failed", e);
            e.printStackTrace();
        }
        mmSocket = tmp;
    }


    public void run() {
        mAdapter.cancelDiscovery();

        try {

            mmSocket.connect();
        }

        catch (IOException e) {  
        //Exception caught 
        //java.io.IOException: read failed, socket might closed or timeout,      read ret: -1
        ...
        }
        ...
    }

Below is the server code (Uses BlueCover jar)

private void waitForConnection() { LocalDevice local = null;

    StreamConnectionNotifier notifier;
    StreamConnection connection = null;

    try {
        local = LocalDevice.getLocalDevice();
        local.setDiscoverable(DiscoveryAgent.GIAC);
        String uuidstr = "00001101-0000-1000-8000-00805F9B34FB";
        String uuid_wo_space = uuidstr.replaceAll("-", "");      

        String url = "btspp://localhost:" + uuid_wo_space  + ";authenticate=false;encrypt=false;name=RemoteBluetooth";
        notifier = (StreamConnectionNotifier) Connector.open(url);
    }
    catch (Exception e) {
        e.printStackTrace();
        return;
    }

    // waiting for connection
    while(true) {
        try {
            connection = notifier.acceptAndOpen();
            Thread processThread = new Thread(new ProcessConnectionThread(connection));
            processThread.start();
        }
        catch(Exception e) {
            e.printStackTrace();
            return;
        }
    }

I have read several links suggesting to change the UUID to "00001101-0000-1000-8000-00805F9B34FB", which i have already tried. I have also tried creating a not secure socket using createInsecureRfcommSocketToServiceRecord, which still fails with the same result.

Below is the IOException stack trace

java.io.IOException: read failed, socket might closed or timeout, read ret: -1
at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:900)
at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:912)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:531)

Solution

  • Sorry for writing a new answer but this way the information is more visible than the reply. Here's an example list of UUIDs for my device:

    UUID: 0000xxxx-0000-1000-8000-00805f9b34fb          
    
    i   xxxx    Status  Mode
    0   110a    N.A.    
    1   1105    Connected   Serial Port Protocol (SPP)
    2   1106    Connected   File transfer (FTP)
    3   1116    N.A.    
    4   112d    Connected   Remote SIM mode
    5   112f    Connected   Phone book request
    6   1112    Connected   
    7   111f    Connected   
    8   1132    Connected   Message access request