문제

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!

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top