Bluetooth on 2.0+

I am developing bluetooth for connecting to a PC. I mainly used BTChatExample and changed the UUID to a standard PC SPP profile.

  • Attempting to close the bluetooth application while reading a lock by closing the BluetoothSocket will leave the Bluetooth stack in an unusable state. This can be fixed by turning bluetooth off and on and restarting the application. When checking logcat, you can see that some internal methods do not work, leaving an open port. Any info on this?

  • First of all, there are seams that may differ from how bluetooth is implemented on the N1 and HTC Legend / Desire with both 2.1, do you know anything about this?

  • The connection is not 100% reliable, sometimes I get a warning ~PortSystemContext init: FAILED. This leaves the Bluetooth device unusable and a restart is required.

  • Am I right in believing that SPP is the only profile supported for use with the API? This is evidenced by the documents in the BluetoothAdapter.

I would like to discuss the bluetooth issues with the developer and fix these errors so that Android can have the good proper Bluetooth support that it deserves.

+1
source share
3 answers

Closing a socket in one thread while reading a lock must necessarily lead to the fact that the read will be returned (by throwing an IOException) and should not leave the stack in a "bad state". This behavior is on Droid and Nexus.

, HTC Legend HTC Desire. , API. .

, SPP/RFCOMM , API. SPP/RFCOMM , .

BT Nexus One/Motorola Droid, "" API Bluetooth.

+2

, read(), , , inputstream.available(), , , .

    long timeouttime = gettimeinseconds() + 2;
    String response = "";

    while (gettimeinseconds() < timeouttime) {
      if (inputstream.available() > 0)
          response = response + inputstream.read();
      } else {
          Thread.sleep(100); // sleep to slow down the while() loop. 
      }
    }
return response;

. , (read()), , .

, BufferedInputStream InputStream.

+1

Can anyone solve this problem?

I am trying the following code:

// Keep listening to the InputStream while connected
while (!isInterrupted)
{
    try
    {
        //Clear buffer
        buffer = new byte[1024];

        // Read from the InputStream
        if (mmInStream != null && mmInStream.available() > 0)
        {
            if (isInterrupted)
                break;

            bytes = mmInStream.read(buffer);

            // Send the obtained bytes to the UI Activity
            mHandler.obtainMessage(Act_Main.MESSAGE_READ, bytes, -1, buffer).sendToTarget();
        }
        else
        {
            try
            {
                synchronized (this)
                {
                    this.wait(100);
                }

                if (isInterrupted)
                    break;
            }
            catch(InterruptedException ex)
            {
                Log.e(TAG, "WAIT_EXCEPTION:"+ ex.getMessage());
            }
        }
    }
    catch(Exception ex)
    {
        Log.e(TAG, "disconnected", ex);
        connectionLost();
        break;
    }
}

And I changed the isInterruptedboolean value in the method cancel(). Here is my method stop():

/**
 * Stop all threads
 */
public synchronized void stop()
{
    isStop = true ;

    if (D)
        Log.d(TAG, "stop");

    if(mConnectThread != null)
    {
        mConnectThread.cancel();
        mConnectThread = null;
    }

    if(mConnectedThread != null)
    {
        mConnectedThread.cancel();
        mConnectedThread = null;
    }

    setState(STATE_NONE);
}
0
source

Source: https://habr.com/ru/post/1753519/


All Articles