Android 接受线程上的IOException

Android 接受线程上的IOException,android,bluetooth,ioexception,iobluetooth,Android,Bluetooth,Ioexception,Iobluetooth,我的应用程序的一部分通过蓝牙连接到设备,正常情况下工作正常,但偶尔它无法连接,我会出现以下错误 03-11 10:29:20.328: E/BluetoothComService(8059): accept() failed 03-11 10:29:20.328: E/BluetoothComService(8059): java.io.IOException: Operation Canceled 03-11 10:29:20.328: E/BluetoothComService(8059):

我的应用程序的一部分通过蓝牙连接到设备,正常情况下工作正常,但偶尔它无法连接,我会出现以下错误

03-11 10:29:20.328: E/BluetoothComService(8059): accept() failed
03-11 10:29:20.328: E/BluetoothComService(8059): java.io.IOException: Operation Canceled
03-11 10:29:20.328: E/BluetoothComService(8059):    at android.bluetooth.BluetoothSocket.acceptNative(Native Method)
03-11 10:29:20.328: E/BluetoothComService(8059):    at android.bluetooth.BluetoothSocket.accept(BluetoothSocket.java:316)
03-11 10:29:20.328: E/BluetoothComService(8059):    at android.bluetooth.BluetoothServerSocket.accept(BluetoothServerSocket.java:105)
03-11 10:29:20.328: E/BluetoothComService(8059):    at android.bluetooth.BluetoothServerSocket.accept(BluetoothServerSocket.java:91)
03-11 10:29:20.328: E/BluetoothComService(8059):    at com.mypackage.name.bluetooth.BluetoothService$AcceptThread.run(BluetoothService.java:298)
这是我得到异常的那一行

socket = mmServerSocket.accept();    
这是完整的接受线程

private class AcceptThread extends Thread {
    // The local server socket
    private BluetoothServerSocket mmServerSocket;
    public boolean successInit = false;

    public AcceptThread() {
        closeAllConnections();

        /*
         * if(mmServerSocket != null) { try { mmServerSocket.close(); } catch
         * (IOException e) { e.printStackTrace(); } }
         */
        BluetoothServerSocket tmp = null;

        // Create a new listening server socket
        while (!successInit) {
            try {
                tmp = mAdapter
                        .listenUsingRfcommWithServiceRecord(NAME, MY_UUID);

                successInit = true;
            } catch (Exception e) {

                successInit = false;
            }
        }

        /*
         * try { tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME,
         * MY_UUID); successInit= true; } catch (IOException e) { Log.e(TAG,
         * "listen() failed", e); tmp = null; successInit = false; }
         */
        mmServerSocket = tmp;
    }

    public void run() {
        if (D)
            Log.d(TAG, "BEGIN mAcceptThread" + this);
        setName("AcceptThread");
        BluetoothSocket socket = null;

        // Listen to the server socket if we're not connected
        while (mState != STATE_CONNECTED) {
            try {
                // This is a blocking call and will only return on a
                // successful connection or an exception
                mAdapter.cancelDiscovery();

                socket = mmServerSocket.accept();
            } catch (IOException e) {
                Log.e(TAG, "accept() failed", e);
                Log.e("Error", "This isn't connecting");
                break;
            }

            // If a connection was accepted
            if (socket != null) {
                synchronized (BluetoothService.this) {
                    switch (mState) {
                    case STATE_LISTEN:
                    case STATE_CONNECTING:
                        // Situation normal. Start the connected thread.
                        connected(socket, socket.getRemoteDevice());
                        break;
                    case STATE_NONE:
                    case STATE_CONNECTED:
                        // Either not ready or already connected. Terminate new
                        // socket.
                        try {
                            socket.close();
                        } catch (IOException e) {
                            Log.e(TAG, "Could not close unwanted socket", e);
                        }
                        break;
                    }
                }
            }
        }
        if (D)
            Log.i(TAG, "END mAcceptThread");
    }

    public void cancel() {
        if (D)
            Log.d(TAG, "cancel " + this);
        try {
            mmServerSocket.close();
        } catch (IOException e) {
            Log.e(TAG, "close() of server failed", e);
        }
    }
}     
下面是我在
AcceptThread
开头调用的函数,希望关闭所有内容以重新启动它

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();
            //mmSocket.connect();
        }
        catch (IOException e) {
            Log.e(TAG, "close() of connect socket failed", e);
        }
    }
}
我已经通读了这些问题,但我没有发现任何适合我的东西,这让我有点困惑,因为这是我第一次通过BT连接

注意

发生这种情况时,我发现唯一的“修复”方法是关闭BT适配器,强制关闭程序,重新启动BT适配器并重新启动应用程序,这显然是不好的。我尝试以编程方式重新启动适配器,但仍然无法连接

有人能看到我的BluthouthService类中可能出现的错误吗?
AcceptThread
位于该类中?或者我将如何着手解决这个问题?谢谢

更新 事实上,看起来连接有时在一个
线程上关闭,并试图在另一个线程上重新连接。问题是,我不知道是什么导致它尝试在一个单独的
线程上连接,或者在发生这种情况时如何修复它


我能成功复制的唯一方法是,如果我的BT设备关闭,然后我关闭BT适配器。当我把所有的东西都打开的时候,我会出现异常,无法连接。我有一些客户是随机和定期出现的,所以我希望这些问题是相关的。

当BluetoothServerSocket关闭或垃圾收集时,应该会出现这种异常。我怀疑异常发生在线程的旧副本上。例如:当您创建新线程时,旧线程被取消,因此BluetoothServerSocket被关闭,因此正确地接受失败并出现该错误。在调试器和/或记录发生各种事件的线程中检查这一点;e、 g.在accept之后的行上设置断点,或者在cancel函数上设置断点,然后检查那里的线程ID——在以前的线程上是否发生异常?

虽然这是一篇旧文章,但我最近联系了同一个问题,所以我想写下解决它的方法

看起来你的代码来自谷歌聊天室(看起来是一样的,如果我误解了,很抱歉)。我还基于此示例创建了自己的应用程序(API级别10)。如果我尝试将一个设备连接到另一个设备,我会遇到accept()失败问题,但最后我只需删除MainActivty中的一些代码即可解决此问题

在Google示例的主要活动中,当活动发生变化(开始、暂停等)时,它包含许多方法

原始代码有这个

@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();
        }
    }
}
此代码启动聊天服务并运行AcceptThread来侦听传入连接

当应用程序启动时,此方法将被调用“一次”,并创建AcceptThread。如果您执行任何其他操作使主活动onPause()暂停(在Google示例中,如果您单击菜单启动设备列表活动,则主活动将暂停),当应用程序返回主活动时,它将调用create AcceptThread方法“再一次”,这会导致问题,因为一个线程已经在运行,但您试图中断它。最后,发生accept()失败错误并抛出java.io.IOException:操作取消错误

为了避免这种情况,只需删除OnResume()中的代码

或者,如果因为担心会引起问题而不想删除任何代码,请将此代码(mChatService!=null)放入mChatService.stop();在

在我的项目中,这两种方法都非常有效。如果活动继续,方法1将不创建新线程;如果您离开当前活动,方法2将杀死所有线程;如果您已经有一个活动,则断开所有当前连接(一旦您返回,它将再次启动线程)。方法1不会返回任何错误,但如果您离开当前活动,方法2将再次抛出accept fail,因此我建议使用方法1

需要注意的是,此错误通常发生在您修改Google samples BlutothChat后,它将永远不会出现在原始应用程序上


我看过很多关于这个问题的帖子,但没有看到任何人给出这个答案,所以我想分享一下。希望这会有帮助。

我的部分问题是硬件问题,被发现是第三方制造商端的问题。他们的固件不太正确,当它读取BT地址时,偶尔会被损坏

在软件方面,它定期在两个独立的
线程中运行
AcceptThread
。我所做的是创建一个函数来关闭套接字和输入/输出流

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);}
        }
然后在
BluetoothCom
类中,我注意到它在尝试重新启动服务之前并不总是检查BT对象是否为
null

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();
}
这似乎有帮助,我不再有这些问题。然而,现在在三星Galaxy Tab 4上进行测试时,我再次遇到连接问题,但仅在该设备上。也许这些信息可以帮助一些人,如果我发现新问题的其他方面,我会更新我的答案

注:如上所述,此蓝牙应用程序使用Android BluetoothChat应用程序的修改代码


此外,我还读到(并注意到)不同的制造商采用不同的BT堆栈,这可能会导致头痛(至少如果你对它了解不够)。

看起来确实可能会发生这种情况,但当我试图弄清楚哪个线程用于什么目的时,调试时应用程序会冻结。我确实看到正在创建两个
AcceptThread
s。您能否建议如何确保它使用正确的
线程
?感谢您的回复,是的,它基于Google聊天示例。已经放好了
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);}
        }
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();
}