我在connectThread中有以下代码,我在youtube教程中使用它来了解如何建立蓝牙连接。套接字无法连接。任何建议,将不胜感激。我正在使用try catch
public void run(){
BluetoothSocket tmp = null;
Log.i(TAG, "RUN mConnectThread");
// get a blue tooth socket for a connection with the given bluetoothdevice
try{
Log.d(TAG, "ConnectThread: Trying to create InsecureRfcommSocket " + MY_UUID_INSECURE);
tmp = mmDevice.createRfcommSocketToServiceRecord(deviceUUID);
}catch (IOException e){
Log.e(TAG, "ConnectThread: Could not create InsecureRfcommSocket" + e.getMessage());
}
mmSocket = tmp;
// Always cancel discovery because it will slow down a connection
mBluetoothAdapter.cancelDiscovery();
// Make a connection to the BluetoothSocket
try {
// this is a blocking call and will only return on a succesful connection or an exception
System.out.println(mmSocket);
mmSocket.connect();
Log.d(TAG, "run: ConnectThread connected");
}catch (Exception e){
// close the socket
try{
mmSocket.close();
Log.d(TAG, " run: Closed socket");
}catch (IOException e1)
{
Log.e(TAG, "mConnectThread: run: Unable to close connection in socket" + e1.getMessage());
}
Log.d(TAG, "run: ConnectThread: Could not connect to UUID: " + MY_UUID_INSECURE);
}
connected(mmSocket, mmDevice);
}