Question

is there any way to know whether if connect() method succeeded or not?

I have the code in class ConnectThread:

public void run() {
    // Cancel discovery because it will slow down the connection
    mBluetoothAdapter.cancelDiscovery();

    try {
        // Connect the device through the socket. This will block
        // until it succeeds or throws an exception
        mmSocket.connect();
    } catch (IOException connectException) {
        // Unable to connect; close the socket and get out
        try {
            mmSocket.close();
        } catch (IOException closeException) { }
        return;
    }
}

and try connecting this thread as:

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) 
{
    // Loop through paired devices
    for (BluetoothDevice device : pairedDevices) 
    {
        if (device.getName().equals("HC-06"))
        {
            Thread initialThread = new ConnectThread(device);
            initialThread.run();
        }
    }
}

I want to find out if this connection was successful or not. Any help would be appreciated thanks!

Was it helpful?

Solution

If it didn't succeed it will have thrown an exception. If you get to the statement after the connect() call, it succeeded.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top