Thanks to Wookie on stackoverflow, I managed to solve some problems such as 
read socket failed which nearly never happens before Bluedroid Stack.


Link I found and tested works great for now. I hope it helps someone who 
has similar problems. The fix is for bluetoothsocket.close which doesnot 
work properly on 4.2 - 4.4.4(may be later I haven't tested).


http://stackoverflow.com/questions/22022001/android-bluetooth-connection-fails-after-489-successful-connections/27873675#27873675

In order to use code, you need to add those codes:

public static void cleanClose(BluetoothSocket btSocket){
    if(btSocket == null)
        return;

    if(Build.VERSION.SDK_INT >= 17 && Build.VERSION.SDK_INT <= 20)
    {
        try { cleanCloseFix(btSocket); }
        catch (Exception e)
        {
            Log.d(sLogName, "Exception during BluetoothSocket close bug fix: " 
+ e.toString());
        }

        //Go on to call BluetoothSocket.close() too, because our code didn't do 
quite everything
    }

    //Call BluetoothSocket.close()
    try { btSocket.close(); }
    catch (Exception e)
    {
        Log.d(sLogName, "Exception during BluetoothSocket close: " + 
e.toString());
    }
}
private static void cleanCloseFix(BluetoothSocket btSocket) throws IOException{
    synchronized(btSocket)
    {
        Field socketField = null;
        LocalSocket mSocket = null;
        try
        {
            socketField = btSocket.getClass().getDeclaredField("mSocket");
            socketField.setAccessible(true);

            mSocket = (LocalSocket)socketField.get(btSocket);
        }
        catch(Exception e)
        {
            Log.d(sLogName, "Exception getting mSocket in cleanCloseFix(): " + 
e.toString());
        }

        if(mSocket != null)
        {
            mSocket.shutdownInput();
            mSocket.shutdownOutput();
            mSocket.close();

            mSocket = null;

            try { socketField.set(btSocket, mSocket); }
            catch(Exception e)
            {
                Log.d(sLogName, "Exception setting mSocket = null in 
cleanCloseFix(): " + e.toString());
            }
        }


        Field pfdField = null;
        ParcelFileDescriptor mPfd = null;
        try
        {
            pfdField = btSocket.getClass().getDeclaredField("mPfd");
            pfdField.setAccessible(true);

            mPfd = (ParcelFileDescriptor)pfdField.get(btSocket);
        }
        catch(Exception e)
        {
            Log.d(sLogName, "Exception getting mPfd in cleanCloseFix(): " + 
e.toString());
        }

        if(mPfd != null)
        {
            mPfd.close();

            mPfd = null;

            try { pfdField.set(btSocket, mPfd); }
            catch(Exception e)
            {
                Log.d(sLogName, "Exception setting mPfd = null in 
cleanCloseFix(): " + e.toString());
            }
        }       

    } //synchronized}


to your BluetoothIOIOConnection class. Afterwards, you need to replace the line 
socket_.close() with  cleanClose(socket_);


I hope it helps.

-- 
You received this message because you are subscribed to the Google Groups 
"ioio-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/ioio-users.
For more options, visit https://groups.google.com/d/optout.

Reply via email to