bluetooth BluetoothSocket.connect() thread. how to close this thread

Posted by Hia on Stack Overflow See other posts from Stack Overflow or by Hia
Published on 2012-03-29T23:45:44Z Indexed on 2012/03/30 5:29 UTC
Read the original article Hit count: 113

Filed under:
|

I am trying to make an android app that makes connection with bluetooth device. It works fine but when I call BluetoothSocket.connect() and it is not able to connect to devise its blocking. The thread and does not throw any exception. So when I try to close application while connect() is running its not responding. How can I cancel it? Used BluetoothSocket.close() in ... but still its not working for me.

         protected void simpleComm(Integer port) {
    // The documents tell us to cancel the discovery process.
        try {
            Method m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
            mmSocket = (BluetoothSocket) m.invoke(mmDevice, port);
            mmSocket.connect();   // <== blocks untill is not connected 
            Log.d(TAG, "   connection success===");
        }catch(Exception e){
            if (!abort) {
                connectionFailed();
                // Close the socket
                try {
                    mmSocket.close();
                    // Start the service over to restart listening mode
                    BluetoothService.this.start();
                } catch (IOException e2) {
                    Log.e(TAG,"unable to close() socket during connection failure", e2);
                }
            }
            return;
       }
    }

   public void cancel() {
        try {
            abort = true;
            mmSocket.close();
        } catch (IOException e) {
            Log.e(TAG, "close() of connect socket failed", e);
        }
    }

© Stack Overflow or respective owner

Related posts about java

Related posts about android