IOException异常的接收线程 [英] IOException on accept thread

查看:450
本文介绍了IOException异常的接收线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序的一个组成部分连接到通过蓝牙设备并正常工作正常,但偶尔会无法连接,我得到了以下错误

One part of my application connects to a device through Bluetooth and normally works fine but occasionally it won't connect and I get the following error

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();    

这是完整的AcceptThread

And this is the complete AcceptThread

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 的希望开始调用函数关闭一切重新启动

Here is the function I call at the beginning of AcceptThread in hopes to close everything to restart it

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连接。

I've read through the Bluetooth Docs and SO questions but I haven't found anything that works for me and it gets a bit confusing for me as this is my first time connecting through BT.

注意

唯一的修复我发现,当出现这种情况是关闭的BT适配器,强制关闭程序,重新启动BT适配器,然后重新启动应用程序,这是不好的原因很明显。我试图以编程方式重新启动适配器,但我仍然无法连接。

The only "fix" I have found when this happens is to turn off the BT adapter, force close the program, restart BT adapter and restart app, which is not good for obvious reasons. I tried restarting the adapter programmatically but I still can't connect.

有人能看到什么可能是错误的,我Blut​​oothService类,这是哪里 AcceptThread 位于?或者,我怎么会去解决这个问题?谢谢!

Can anyone see what might be wrong in my BlutoothService class, which is where AcceptThread is located? Or how I would go about resolving this issue? Thanks!

它呢,其实,看似连接被关闭,有时在一个并尝试重新连接上的另一个。问题是,我想不出什么会导致它尝试连接在一个独立的或如何解决它,当发生这种情况。

It does, in fact, seem like the connection is sometimes closed on one Thread and trying to reconnect on another. The problem is that I can't figure out what would cause it to try and connect on a separate Thread or how to fix it when this happens.

我可以成功复制的唯一方法是,如果我的蓝牙设备处于关闭状态,然后我打开BT适配器关闭。当我把一切都回到然后我得到了异常,无法连接。我的客户认为它发生随机和定期所以我希望这些问题都是相关的。

The only way I can successfully reproduce this is if my BT device is turned off then I turn the BT adapter off. When I turn everything back on then I get the exception and cannot connect. I have customers that it happens to randomly and periodically so I'm hoping the issues are related.

推荐答案

嗯,我的问题的一部分是,被发现了要对第三方制造商最终的问题是硬件问题。他们的固件是不完全正确,当它在读的BT地址,它偶尔会被损坏。

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.

在软件方面,它运行的 AcceptThread 在两个独立的主题周期性。我所做的修复,这是创建一个函数来关闭套接字和输入/输出流...

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);}
        }

然后在 BluetoothCom 类,我发现它并不总是检查是BT对象前试图重新启动该服务。

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();
}

这似乎起了作用,我不再有这些问题。然而,现在正在测​​试上三星Galaxy Tab 4,我再次有连接问题,但只能在此设备。也许这些信息可以帮助别人,我会更新我的答案,如果我想别的了新的问题。

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.

注:从Android的BluetoothChat应用如上所述,这种蓝牙应用程序使用修改code

另外,我已阅读(并注意到),不同厂商实现了BT协议栈不同,可导致头痛(至少如果哟没有足够了解的话)。

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).

这篇关于IOException异常的接收线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆