有没有办法知道connect()方法是否成功?
我在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;
}
}
并尝试将此主题连接为:
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();
}
}
}
我想知道这种连接是否成功。任何帮助将不胜感激!
答案 0 :(得分:0)
如果它没有成功,它将抛出异常。如果在connect()调用之后到达语句,则成功。