I have problem with connecting blutooth RfComm. I was able to connect to bluetooth and communicate but sometimes it doesn't connect. I am using standard uuid-00001101-0000-1000-8000-00805F9B34FB
please help me out :)
private class ConnectThread extends Thread {
private BluetoothSocket mmSocket;
private BluetoothDevice mmDevice;
private ConnectThread(BluetoothDevice device) {
Log.d(TAG, "in ConnectThread");
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
BluetoothSocket tmp = null;
mmDevice = device;
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
Log.d(TAG, "createRfcommSocketToServiceRecord for " + mmDevice.getName());
// MY_UUID is the app's UUID string, also used by the server code
tmp = mmDevice.createRfcommSocketToServiceRecord(MY_UUID);
//tmp = mmDevice.createInsecureRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
e.printStackTrace();
}
mmSocket = tmp;
}
public void run() {
Log.d(TAG, "in ConnectThread : run");
// Cancel discovery because it will slow down the connection
bluetoothAdapter.cancelDiscovery();
try {
Log.d(TAG, "Try to Connect Socket");
// Connect the device through the socket. This will block
mmSocket.connect();
} catch (IOException connectException) {
try {
Log.i(TAG, "Trying fallback...");
mmSocket = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}).invoke(mmDevice, 1);
mmSocket.connect();
} catch (Exception e2) {
Log.e(TAG, "Couldn't establish Bluetooth connection!");
try {
mmSocket.close();
} catch (IOException e3) {
Log.e(TAG, "unable to close() socket during connection failure", e3);
}
return;
}
// Do work to manage the connection (in a separate thread)
mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
}
}
// Will cancel an in-progress connection, and close the socket
private void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class ConnectedThread extends Thread implements Runnable {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
Log.d(TAG, "in ConnectedThread");
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
Log.d(TAG, "in ConnectedThread : run");
byte[] buffer; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
//Log.d(TAG,"in ConnectedThread : run -- while");
try {
try {
sleep(25);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Read from the InputStream
buffer = new byte[1024];
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI activity
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
} catch (IOException e) {
break;
}
}
}
01-18 09:19:09.661 15692-16231/iconcreator.pidroid I/Main Activity :: Trying fallback...
01-18 09:19:09.661 2809-2873/? E/bt_btif_sock_rfcomm: find_rfc_slot_by_id unable to find RFCOMM slot id: 14
01-18 09:19:09.662 15692-16231/iconcreator.pidroid W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
01-18 09:19:09.743 16236-16236/? D/NetworkSecurityConfig: No Network Security Config specified, using platform default
01-18 09:19:09.846 2809-2859/? W/bt_rfcomm: port_rfc_closed RFCOMM connection in state 1 closed: Closed (res: 19)
01-18 09:19:09.846 15692-16231/iconcreator.pidroid E/Main Activity :: Couldn't establish Bluetooth connection!
01-18 09:19:09.847 2809-2873/? E/bt_btif_sock_rfcomm: find_rfc_slot_by_id unable to find RFCOMM slot id: 15