Android BluetoothServerSocket的accept()mathos抛出IOException

时间:2014-08-28 13:43:39

标签: android bluetooth

我正在使用Google的示例蓝牙聊天代码进行修改,以开发蓝牙聊天应用程序。我从BluetoothServerSocket的IOException方法获得accept()。我不知道为什么会这样。谁能帮我吗?。我发布了一些我的代码..每次流程都会进入catch部分。我读了一些答案,但无法得到任何帮助。

private class AcceptThread extends Thread {
        // The local server socket
        private final BluetoothServerSocket mmServerSocket;

        public AcceptThread() {
            BluetoothServerSocket tmp = null;

            // Create a new listening server socket
            try {
                tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
                if(tmp != null)
                    Log.d("listening", this.getId()+" listening");

            } catch (IOException e) {
                Log.e(TAG, "listen() failed", e);
            }
            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

                    socket = mmServerSocket.accept();

                } catch (IOException e) {

                        Log.d("errormsg", e.getMessage());
                        // Send a failure message back to the Activity
                        Message msg = mHandler.obtainMessage(HomepageActivity.MESSAGE_TOAST);
                        Bundle bundle = new Bundle();
                        bundle.putString(HomepageActivity.TOAST, "Remote device didn't accept the connection request");
                        msg.setData(bundle);
                        mHandler.sendMessage(msg);
                        break;

                }

                // If a connection was accepted
                if (socket != null) {
                    synchronized (BluetoothChatService.this) {
                        switch (mState) {
                        case STATE_LISTEN:
                        case STATE_CONNECTING:
                            Log.d("msg", "socket not null");
                            // 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, this.getId()+"END mAcceptThread");
        }

logcat的

08-28 19:34:04.161: E/BluetoothChatService(6449): accept() failed 08-28 19:34:04.161:
 E/BluetoothChatService(6449): java.io.IOException: read failed, 
    socket might closed or timeout, read ret: -1 08-28 19:34:04.161: 
E/BluetoothChatService(6449): at android.bluetooth.BluetoothSocket.
    readAll(BluetoothSocket.java:553) 08-28 19:34:04.161: 
E/BluetoothChatService(6449): at android.bluetooth.BluetoothSocket.
    waitSocketSignal(BluetoothSocket.java:530) 08-28 19:34:04.161: 
E/BluetoothChatService(6449): at 
    android.bluetooth.BluetoothSocket.accept(BluetoothSocket.java:440)

0 个答案:

没有答案