Android error RFCOMM_CreateConnection - already opened state:2, RFC state:4, MCB state:5 when attempting to Bluetooth Socket

Jump to: navigation, search

Multiple open and close of a Bluetooth socket may result in the following error when you attempt to reconnect : RFCOMM_CreateConnection - already opened state. There is an isConnected() method added to the Bluetooth Device class for API level 14. But if you are programming for older API levels one way to get around this problem is to just put your thread to sleep after closing your socket. 1000ms sleep time works but it probably doesn't need to be that long. Example: grd = mBluetoothAdapter.getDefaultAdapter().getRemoteDevice(btDevID); Method m = grd.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class}); gbts = (BluetoothSocket) m.invoke(grd, 1); gbts.connect(); //do stuff gbts.close(); try { Thread.sleep(1000);} catch(Exception e){}

See also

Use your Android as a remote for garage door

Personal tools