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

  • Home
  • SEARCH
  • 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 9204287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:40:36+00:00 2026-06-17T23:40:36+00:00

I am working on a application where I am connecting to a device with

  • 0

I am working on a application where I am connecting to a device with Bluetooth which works good with all the android version but had some issues with Samsung galaxy tab 2 10.1.
Problem:–
If device turn offs after some time my application detects and then start searching for remote device. But application not able to find device (When turned on). I also have a normal device list activity when I again start normal searching here it founds device and also connects to it properly.
Code:–
My connection code is as follow and I am using low level connection not serial porting.
Steps I follows:
1) Find bluetooth device (get mac)
2) Start HHDService.start();
3) HHDService.connect(Bluetooth device);

Main issue is Application not able to find device once device is disconnected…

public class HHDService {
    // Debugging
    private static final String TAG = "HHDService";
    private static final boolean D = true;

    boolean isRequestSend = true;

    // Unique UUID for this application
    //private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

    private BluetoothSocket mmSocket = null;
    private InputStream mmInStream = null;
    private OutputStream mmOutStream = null;

    // Member fields
    private final BluetoothAdapter mAdapter;
    private Handler mHandler;
    //private AcceptThread mAcceptThread;
    private ConnectThread mConnectThread;
    private ConnectedThread mConnectedThread;
    private int mState;

    private long waitTime = 15000;

    // Constants that indicate the current connection state
    public static final int STATE_NONE = 0; // we're doing nothing
    public static final int STATE_LISTEN = 1; // now listening for incoming
                                                // connections
    public static final int STATE_CONNECTING = 2; // now initiating an outgoing
                                                    // connection
    public static final int STATE_CONNECTED = 3; // now connected to a remote
                                                    // device

    public static final int STATE_SLEEP = 4; // now connected to a remote
    // device

    private Context mContext;

    /**
     * Constructor. Prepares a new BluetoothChat session.
     * 
     * @param context
     *            The UI Activity Context
     * @param handler
     *            A Handler to send messages back to the UI Activity
     */
    public HHDBapiBTService(Context context, Handler handler) {
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        mState = STATE_NONE;
        mHandler = handler;
        mContext = context;
    }

    /**
     * Set the current state of the chat connection
     * 
     * @param state
     *            An integer defining the current connection state
     */
    public synchronized void setState(int state) {
        if (D)
            Log.d(TAG, "setState() " + mState + " -> " + state);
        mState = state;

        // Give the new state to the Handler so the UI Activity can update
        mHandler.obtainMessage(GlobalValues.MESSAGE_STATE_CHANGE, state, -1)
                .sendToTarget();
    }

    /**
     * Return the current connection state.
     */
    public synchronized int getState() {
        return mState;
    }

    public void setHandler(Handler handler) {
        mHandler = handler;
    }

    /**
     * Start the chat service. Specifically start AcceptThread to begin a
     * session in listening (server) mode. Called by the Activity onResume()
     */
    public synchronized void start() {
        if (D)
            Log.d(TAG, "start");

        // Cancel any thread attempting to make a connection
        if (mConnectThread != null) {
            mConnectThread.cancel(true);
            mConnectThread = null;
        }

        // Cancel any thread currently running a connection
        if (mConnectedThread != null) {
            mConnectedThread.cancel();
            mConnectedThread = null;
        }

        setState(STATE_LISTEN);
    }

    /**
     * Start the ConnectThread to initiate a connection to a remote device.
     * 
     * @param device
     *            The BluetoothDevice to connect
     */
    public synchronized void connect(BluetoothDevice device) {
        if (D)
            Log.d(TAG, "connect to: " + device);

        // Cancel any thread attempting to make a connection
        if (mState == STATE_CONNECTING) {
            if (mConnectThread != null) {
                mConnectThread.cancel();
                mConnectThread = null;
            }
        }

        // Cancel any thread currently running a connection
        if (mConnectedThread != null) {
            mConnectedThread.cancel();
            mConnectedThread = null;
        }

        // Start the thread to connect with the given device
        mConnectThread = new ConnectThread(device);
        mConnectThread.start();
        setState(STATE_CONNECTING);
    }

    /**
     * Start the ConnectedThread to begin managing a Bluetooth connection
     * 
     * @param socket
     *            The BluetoothSocket on which the connection was made
     * @param device
     *            The BluetoothDevice that has been connected
     */
    public synchronized void connected(BluetoothSocket socket,
            BluetoothDevice device) {
        if (D)
            Log.d(TAG, "connected");

        // Cancel the thread that completed the connection
        if (mConnectThread != null) {
            mConnectThread.cancel();
            mConnectThread = null;
        }

        // Cancel any thread currently running a connection
        if (mConnectedThread != null) {
            mConnectedThread.cancel();
            mConnectedThread = null;
        }

        // Start the thread to manage the connection and perform transmissions
        mConnectedThread = new ConnectedThread(socket);
        mConnectedThread.start();

        setState(STATE_CONNECTED);
    }

    /**
     * Stop all threads
     */
    public synchronized void stop() {
        if (D)
            Log.d(TAG, "stop");
        if (mConnectThread != null) {
            mConnectThread.cancel();
            mConnectThread = null;
        }
        if (mConnectedThread != null) {
            mConnectedThread.cancel();
            mConnectedThread = null;
        }

        setState(STATE_NONE);
    }

    /**
     * Write to the ConnectedThread in an unsynchronized manner
     * 
     * @param out
     *            The bytes to write
     * @see ConnectedThread#write(byte[])
     */
    public void write(byte[] out) {
        if(getState() == STATE_SLEEP){
            connectionLost();
            return;
        }
        // Create temporary object
        ConnectedThread r;
        // Synchronize a copy of the ConnectedThread
        synchronized (this) {
            if (mState != STATE_CONNECTED)
                return;
            r = mConnectedThread;

        }
        // Perform the write unsynchronized
        r.write(out);
    }

    public void closeConnection(){
        ConnectedThread r;
        // Synchronize a copy of the ConnectedThread
        synchronized (this) {
            if (mState != STATE_CONNECTED)
                return;
            r = mConnectedThread;
        }
        r.cancel();
    }

    /**
     * Indicate that the connection attempt failed and notify the UI Activity.
     */
    private void connectionFailed() {
        if(getState() != STATE_LISTEN){
            setState(STATE_LISTEN);

        }

    }

    /**
     * Indicate that the connection was lost and notify the UI Activity.
     */
    private void connectionLost() {
        //setState(STATE_LISTEN);

        // Send a failure message back to the Activity
        setState(STATE_SLEEP);

    }


    /**
     * This thread runs while attempting to make an outgoing connection with a
     * device. It runs straight through; the connection either succeeds or
     * fails.
     */

    /* private class ConnectThread extends Thread {
            private final BluetoothSocket mmSocket;
            private final BluetoothDevice mmDevice;

            public ConnectThread(BluetoothDevice device) {
                mmDevice = device;
                BluetoothSocket tmp = null;

                // Get a BluetoothSocket for a connection with the
                // given BluetoothDevice
                try {
                    tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
                } catch (IOException e) {
                    Log.e(TAG, "create() failed", e);
                }
                mmSocket = tmp;
            }

            public void run() {
                Log.i(TAG, "BEGIN mConnectThread");
                setName("ConnectThread");

                // Always cancel discovery because it will slow down a connection
                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
                    mmSocket.connect();
                } catch (IOException e) {
                    connectionFailed();
                    // Close the socket
                    try {
                        mmSocket.close();
                    } catch (IOException e2) {
                        Log.e(TAG, "unable to close() socket during connection failure", e2);
                    }
                    // Start the service over to restart listening mode
                    HHDService.this.start();
                    return;
                }

                // Reset the ConnectThread because we're done
                synchronized (HHDService.this) {
                    mConnectThread = null;
                }

                // Start the connected thread
                connected(mmSocket, mmDevice);
            }

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

    }*/

    private class ConnectThread extends Thread {
        private BluetoothSocket mmSocket;
        private final BluetoothDevice mmDevice;

        public ConnectThread(BluetoothDevice device) {
            mmDevice = device;
        }

        @SuppressLint("NewApi")
        protected boolean simpleComm(Integer port) {
            // byte [] inputBytes = null;

            // The documents tell us to cancel the discovery process.
            mAdapter.cancelDiscovery();

            Log.d(this.toString(), "Port = " + port);
            try {
                Method m = mmDevice.getClass().getMethod("createRfcommSocket",
                        new Class[] { int.class });
                mmSocket = (BluetoothSocket) m.invoke(mmDevice, port);

                // debug check to ensure socket was set.
                assert (mmSocket != null) : "Socket is Null";

                mAdapter.cancelDiscovery();
                // attempt to connect to device
                mmSocket.connect();
                try {
                    Log.d(this.toString(),
                            "************ CONNECTION SUCCEES! *************");

                    // Reset the ConnectThread because we're done
                    synchronized (HHDService.this) {
                        mConnectThread = null;
                    }

                    connected(mmSocket, mmDevice);
                    return true;
                } finally {
                    // close the socket and we are done.
                    // //mmSocket.close();
                }
                // IOExcecption is thrown if connect fails.
            } catch (IOException ex) {
                Log.e(this.toString(), "IOException " + ex.getMessage());
                if (port == 256) {
                    connectionFailed();
                    try {
                        mmSocket.close();
                        mmSocket = null;
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
            } catch (NoSuchMethodException ex) {
                Log.e(this.toString(),
                        "NoSuchMethodException " + ex.getMessage());
            } catch (IllegalAccessException ex) {
                Log.e(this.toString(),
                        "IllegalAccessException " + ex.getMessage());
            } catch (InvocationTargetException ex) {
                Log.e(this.toString(),
                        "InvocationTargetException " + ex.getMessage());
            }catch (NullPointerException ex) {
                Log.e(this.toString(),
                        "NullPointerException " + ex.getMessage());
            }
            return false;
        }

        public void run() {
            for (Integer port = 1; port <= 256; port++) {
                if (simpleComm(Integer.valueOf(port)))
                    break;
            }

        }

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

        public void cancel(boolean val) {
             if (mmSocket != null) { 
                 try { 
                     mmSocket.close(); mmSocket = null;
                 } catch (IOException e) { 
                     Log.e(TAG, "close() of connect secure socket failed", e); 
                 } 
             }
        }

    }

    /**
     * This thread runs during a connection with a remote device. It handles all
     * incoming and outgoing transmissions.
     */
    private class ConnectedThread extends Thread {

        private Logger logger;

        public ConnectedThread(BluetoothSocket socket) {
            Log.d(TAG, "create ConnectedThread");
            mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            // Get the BluetoothSocket input and output streams
            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) {
                Log.e(TAG, "temp sockets not created", e);
            }

            logger = Logger.getInstance();

            mmInStream = tmpIn;
            mmOutStream = tmpOut;

        }

        @Override
        public void run() {
            Log.i(TAG, "BEGIN mConnectedThread");
            byte[] buffer = new byte[1024];
            int bytes;

            byte commandByte = 00;

            //Logic to parse incoming data

        }; 

    }

}
  • 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-06-17T23:40:38+00:00Added an answer on June 17, 2026 at 11:40 pm

    After lot of research found solution on it?? just add delay of (500 ms in my case) before requesting to open socket.

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

Sidebar

Related Questions

I'm trying to connecting SQL Azure from my android application. The code is working
In android application we are using Bluetooth connectivity with some of medical devices. To
I am working on an iOS Bluetooth LE application. The functionality which I am
I am working on an application which has Bluetooth connection as core part. Currently
Currently a team of developers is working on Android application and during the development
For the current Android application I'm working on, I'm looking to create a tcp
So, I am working on the database architecture of a web application which would
We are developing some device and we need to find good solution for one
I am working on a Bluetooth LE application for iOS. I am using the
I made an application that is using Android SDK 2.1. This application uses bluetooth

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.