繁体   English   中英

android蓝牙聊天应用程序(设备立即失去连接)

[英]android bluetooth chat app (device loses connection immediately)

我在这里经历了多个线程,却没有遇到我遇到的问题的答案。

我的设置:我有一台Mac PC,正在将其用作虚拟串行端口来与我的Android Nexus S手机通信。 在电话上运行蓝牙聊天应用程序,并将其作为客户端与我设置的虚拟通信进行交谈。

最初,我尝试使用2部android手机尝试蓝牙聊天应用程序,以确认其正常工作。 我可以来回发送短信。

我的用例:我有一个读取RFid标签并将数据发送到android手机以收集信息的设备。

我现在使用PC代表我的设备。

++++++++++++++++++可以解决这个问题,

我尝试通过手机连接到PC,最初我得到“正在连接...”状态栏的更新,大约15秒后,我收到一条吐司消息,说“我已连接到PC”,但是当我收到“设备断开连接”吐司。 然后状态栏转到“未连接”

当我逐步调试器时,它似乎在蓝牙聊天应用程序的以下部分失败。 特别是这一行( bytes = mmInStream.read(buffer);

    public void run() {
        Log.i(TAG, "BEGIN mConnectedThread");
        byte[] buffer = new byte[1024];
        int bytes;

        // Keep listening to the InputStream while connected
        while (true) {
            try {
                // Read from the InputStream
                bytes = mmInStream.read(buffer);

                // Send the obtained bytes to the UI Activity
                mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
                        .sendToTarget();
            } catch (IOException e) {
                Log.e(TAG, "disconnected", e);
                connectionLost();
                break;
            }
        }
    }

当我查看logcat时,输入输出上的read()的I / O异常是“ software caused connection abort ”。

问题:这与我的虚拟端口设置不正确有关吗? 我已将终端启动,并等待使用/ 9600 baud屏幕命令在/dev/tty.Nexus ....上接收输入

否则,我认为输入流连接到的套接字可能不可用。 我将其打印到日志中,看来它不是NULL。 每次我逐步通过时都会死于ConnectThread而不是ConnectedThread中

代码的以下部分:特别是这一行( mmSocket.connect();

        public void run() {
        Log.i(TAG, "BEGIN mConnectThread");
        setName("ConnectThread");

        // Always cancel discovery because it will slow down a connection
        mAdapter.cancelDiscovery();

        // Make a connection to the BluetoothSocket
        try {
            // This is a blocking call and will only return on a
            // successful connection or an exception
            mmSocket.connect();
        } catch (IOException e) {
            connectionFailed();
            // Close the socket
            try {
                mmSocket.close();
            } catch (IOException e2) {
                Log.e(TAG, "unable to close() socket during connection failure", e2);
            }
            // Start the service over to restart listening mode
            BluetoothChatService.this.start();
            return;
        }

        // Reset the ConnectThread because we're done
        synchronized (BluetoothChatService.this) {
            mConnectThread = null;
        }

        // Start the connected thread
        connected(mmSocket, mmDevice);
    }

我想知道套接字变量是否由于多线程而失去作用域,并且套接字正在传递?

谢谢

如何在Mac PC上设置虚拟串行帖子。 由于您尝试在2部手机上运行该应用程序且该应用程序正常运行,因此我认为问题出在PC上。

我已经发布了关于Android和Java蓝牙条目这里 希望它会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM