IOException on accept thread

半城伤御伤魂 提交于 2019-11-30 07:21:57

Well, part of my problem was a hardware issue that was found out to be a problem on the third-party manufacturers end. They're firmware wasn't quite right and when it was reading the BT address, it was occasionally being corrupted.

On the software side, it was running the AcceptThread in two separate Threads periodically. What I did to fix that was to create a function to close the socket and input/output streams...

public void closeAllConnections()
    {
        if (mmInStream != null)
        {
            try {mmInStream.close();}
            catch  (Exception e){Log.e(TAG, "close() of connect socket failed", e);}
        }
        if (mmOutStream != null)
        {
            try {mmOutStream.close();}
            catch (Exception e){Log.e(TAG, "close() of connect socket failed", e);}
        }
        if (mmSocket != null)
        {
          try {
                mmSocket.close();
                Log.e("TAG", "close() of connect socket successfu;");
              } catch (IOException e) {
                   Log.e("TAG", "close() of connect socket failed", e);}
        }

Then in the BluetoothCom class, I noticed it wasn't always checking for the BT object to be null before trying to restart the service.

private void setupService() {

    // Initialize the BluetoothChatService to perform bluetooth connections
    if((mChatService != null) && (mChatService.getState() != 0)) {
        mChatService.stop();        
    }

    // I didn't have this so it would always start a new instance of the Service
    if (mChatService == null)
        mChatService = new BluetoothService(mHandler);

    mChatService.start();
}

This seems to have helped and I no longer have those problems. However, now testing on the Samsung Galaxy Tab 4 and I am once again having connection issues but only on this device. Maybe this information can help someone and I will update my answer if I figure anything else out with the new problem.

Note: As stated above, this Bluetooth app uses modified code from Android's BluetoothChat app.

Also, I have read (and noticed) that different manufacturers implement the BT stack differently which can lead to headaches (at least if yo don't know enough about it).

Although this is old post but I recently contact same issue so I want to write down the way I solve it.

It seems your code is from Google samples BlutoothChat (as it looks same, sorry if I misunderstand). I also create my own application that base on this sample (on API level 10). I meet the accept() fail issues if I try to connect one device to other device but at the end I solve this question by simply remove some code in MainActivty

On the Google samples main activity it contain many methods when activity change (Start, Pause, etc).

Original code have this

@Override
public synchronized void onResume() {
    super.onResume();
    if(D) Log.e(TAG, "+ ON RESUME +");
    if (mChatService != null) {
        if (mChatService.getState() == ChatService.STATE_NONE) {
          mChatService.start();
        }
    }
}

This code start the Chat Service and running the AcceptThread to listening incoming connections.

When application start, this method will be call "once" and create the AcceptThread. If you do any other things that make the main activity onPause() pause (In Google samples case, if you click menu to start device_list activity the main activity will pause), when the application back to main activity it will call create AcceptThread method "one more time", this cause the problem because one thread already running but you try to interrupt it. And at the end happen accept() fail error and throw java.io.IOException: Operation Canceled error.

So to avoid this is simply remove the codes in OnResume()

@Override
public synchronized void onResume() {
    super.onResume();
    if(D) Log.e(TAG, "+ ON RESUME +");
}

or if you don't want delete any codes because you afraid cause some problem, put this code (mChatService != null) mChatService.stop(); in...

@Override
public synchronized void onPause() {
    super.onPause();
    if (mChatService != null) mChatService.stop();
    if(D) Log.e(TAG, "- ON PAUSE -");
}

Both works perfect in my project. Method 1 not create the new thread if activity resume, and Method 2 kill all the thread if you leave the current activity and disconnect all current connections if you already have one (it will start the thread again once you turn back). Method 1 wouldn't return any error but method 2 will throw accept fail again if you leave the current activity, so I suggest to use method 1.

Need to notice that this error usually happen after you modify Google samples BlutoothChat, it will never appear on the original app.

I have seen many post talk about this issue, but not see any one come out with this answer, so just want to share this. Hope this is helpful.

That exception should occur when the BluetoothServerSocket is closed or garbage collected. I suspect that the exception is happening on an OLD copy of the thread. So something like: When you create the new thread the old thread gets cancelled and thus the BluetoothServerSocket is closed and thus accept correctly fails with that error. Check this in the debugger and/or logging on which thread the various events occur; e.g. set breakpoints on the line after accept and perhaps on the cancel function, then inspect the thread IDs there -- is the exception occurring on a previous thread?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!