Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6543757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:23:03+00:00 2026-05-25T11:23:03+00:00

I try to set up a Bluetooth connection as follows: private class ConnectThread extends

  • 0

I try to set up a Bluetooth connection as follows:

private class ConnectThread extends Thread {

    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;
    BluetoothSocket connection = null;

    public ConnectThread(BluetoothDevice device) {
        mmDevice = device;
        // Get a BluetoothSocket for a connection with the
        // given BluetoothDevice
        try {
            if(D)
                Log.i(TAG, "createRfcommSocket");
            Method m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
            connection = (BluetoothSocket) m.invoke(mmDevice, 1);

            Utils.pause(100);
        } catch (Exception e) {
            Log.e(TAG, "create() failed", e);
        }
        if(D) 
            Log.i(TAG, "Bluetooth socket initialised");
        mmSocket = connection;
    }

    public void run() {
        if(D) 
            Log.i(TAG, "BEGIN mConnectThread on Device: " + mmDevice);
        setName("ConnectThread");

        if (mmSocket != null) {
            // Always cancel discovery because it will slow down a connection
            if(mAdapter.isDiscovering()) {
                if(D) 
                    Log.i(TAG, "stop discovering before trying to connect");
                mAdapter.cancelDiscovery();
            }

            // Make a connection to the BluetoothSocket
            try {
                // This is a blocking call and will only return on a
                // successful connection or an exception
                if(D) 
                    Log.i(TAG, "start Bluetooth socket connection");
                mmSocket.connect();
                if(D) 
                    Log.i(TAG, "end of Bluetooth socket connection");
            } catch (Exception e1) {
                Log.e(TAG, "connect failed", e1);
                connectionFailed();

                // Reset the socket
                resetConnection(mmSocket);

                // Start the service over to restart listening mode
                if(D) 
                    Log.i(TAG, "restart connection");
                BluetoothConnectionService.this.start();
                if(D) 
                    Log.i(TAG, "return");
                return;
            }

            // Start the connected thread
            if(D) 
                Log.i(TAG, "The device is considered as connected, call connected thread");
            connected(mmSocket, mmDevice);
        } else {
            if(D) 
                Log.i(TAG, "connection fail");
            connectionFailed();
            BluetoothConnectionService.this.start();
        }
    }

    public void cancel() {
        if(D) 
            Log.i(TAG, "connect thread cancel");
        resetConnection(mmSocket);
    }
}

The connection seems to be OK, and I get this log:

DEBUG/BluetoothConnectionService(3439): **connect to: 00:18:E4:21:8B:5E**
INFO/BluetoothConnectionService(3439): createRfcommSocket
INFO/BluetoothConnectionService(3439): Bluetooth socket initialised
INFO/BluetoothConnectionService(3439): BEGIN mConnectThread on Device : 00:18:E4:21:8B:5E
INFO/BluetoothConnectionService(3439): **start Bluetooth socket connection**
INFO/BluetoothConnectionService(3439): setState() 1 -> 2
INFO/ProtectionService(3439): MESSAGE_STATE_CHANGE: 1
DEBUG/ProtectionService(3439): STATE_CHANGED to STATE_LISTEN
INFO/ProtectionService(3439): MESSAGE_STATE_CHANGE: 2
DEBUG/abtfilt(2772): **Conn complete**
INFO/bluetoothd(2776): link_key_request (sba=24:21:AB:F4:69:25, dba=00:18:E4:21:8B:5E)
INFO/bluetoothd(2776): link_key_request (sba=24:21:AB:F4:69:25, dba=00:18:E4:21:8B:5E)
INFO/BluetoothConnectionService(3439): **end of Bluetooth socket connection**
INFO/BluetoothConnectionService(3439): **The device is considered as connected, call connected thread**

The BluetoothSocket.connect() returns without error.

Then I manage the connection with this:

private class ConnectedThread extends Thread {

    private final BluetoothSocket mmSocket;
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;

    public ConnectedThread(BluetoothSocket socket) {
        if(D) Log.d(TAG, "create ConnectedThread");
        mmSocket = socket;

        // Get the BluetoothSocket input and output streams
        try {
            if(D) 
                Log.i(TAG, "Get input stream");
            tmpIn = socket.getInputStream();
            if(D) 
                Log.i(TAG, "Get output stream");
            tmpOut = socket.getOutputStream();
            if(D) 
                Log.i(TAG, "isConnected = true");
            isConnected = true;
        } catch (IOException e) {
            Log.e(TAG, "Input and Output sockets not created", e);
            isConnected = false;
        }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

    public void run() {
        if(D) 
            Log.i(TAG, "BEGIN mConnectedThread");
        byte[] buffer = new byte[1024]; //buffer store for the stream
        int bytes; //bytes returned from read()

        // Keep listening to the InputStream until an exception occurs
        while(true) {
            try {
                // Read from the InputStream
                if(D) 
                    Log.i(TAG, "start read mmInStream");
                bytes = mmInStream.read(buffer);
                if(D) 
                    Log.i(TAG, "Bytes read: " + bytes);

                // Send the obtained bytes to the UI Activity
                mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
            } catch (IOException e) {
                Log.e(TAG, "ConnectedThread: disconnected", e);
                break;
            }
        }
    }

    /**
     * Write to the connected OutStream.
     *
     * @param buffer
     *            The bytes to write
     */
    public void write(byte[] buffer) {
        try {
            mmOutStream.write(buffer);

            // Share the sent message back to the UI Activity
            mHandler.obtainMessage(MESSAGE_WRITE, -1, -1, buffer).sendToTarget();
            // mmOutStream.flush();
        } catch (IOException e) {
            Log.e(TAG, "Exception during write", e);
            connectionFailed();
        }
    }

    public void cancel() {
        if(D) 
            Log.i(TAG, "connected Thread cancel");
        resetConnection(mmSocket);
    }

}

And I receive this error:

DEBUG/BluetoothConnectionService(3439): create ConnectedThread
INFO/BluetoothConnectionService(3439): Get input stream
INFO/BluetoothConnectionService(3439): Get output stream
INFO/BluetoothConnectionService(3439): isConnected = true
INFO/BluetoothConnectionService(3439): BEGIN mConnectedThread
INFO/BluetoothConnectionService(3439): start read mmInStream
INFO/BluetoothConnectionService(3439): setState() 2 -> 3
INFO/ProtectionService(3439): MESSAGE_STATE_CHANGE: 3
DEBUG/ProtectionService(3439): connectionEstablished
**ERROR/BluetoothConnectionService(3439): ConnectedThread : disconnected
ERROR/BluetoothConnectionService(3439): java.io.IOException: Software caused connection abort
ERROR/BluetoothConnectionService(3439):     at android.bluetooth.BluetoothSocket.readNative(Native Method)
ERROR/BluetoothConnectionService(3439):     at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:307)
ERROR/BluetoothConnectionService(3439):     at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
ERROR/BluetoothConnectionService(3439):     at java.io.InputStream.read(InputStream.java:133)**
INFO/ProtectionService(3439): Send Bluetooth message
**ERROR/BluetoothConnectionService(3439): Exception during write
ERROR/BluetoothConnectionService(3439): java.io.IOException: Transport endpoint is not connected
ERROR/BluetoothConnectionService(3439):     at android.bluetooth.BluetoothSocket.writeNative(Native Method)
ERROR/BluetoothConnectionService(3439):     at android.bluetooth.BluetoothSocket.write(BluetoothSocket.java:317)**

The error clearly come when I am doing this:

bytes = mmInStream.read(buffer);

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:23:03+00:00Added an answer on May 25, 2026 at 11:23 am

    I had a very similar problem some days ago…

    Try adding this check to your code:

    if(mmInStream.available() > 0){
       bytes = mmInStream.read(buffer);
       mHandler.obtainMessage(MESSAGE_WRITE, -1, -1, buffer).sendToTarget();
    }
    

    This fixed my problem…

    Good Luck 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to set the anchorPoint property, in order to rotate a view by
I try to set an attribute in a XML node using MSXML. IXMLDOMElement alone
While we try to set up as many unit tests as time allows for
When I try to set the z variable in the code below, I get
Every time try to set the value of any variable in my model object,
I have try to set icon for my App in the xCode. In the
I have a WebBrowser control and try to set onclick and href attributes on
Usually, I try to set my folders to have permission 775, but some web-hosting
Greetings, what is the problem that when I try to set credentials for my
I'm trying to automate some stuff in MS Excel. When I try to set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.