【问题标题】:android bluetooth chat app (device loses connection immediately)android蓝牙聊天应用程序(设备立即失去连接)
【发布时间】:2011-06-20 02:57:43
【问题描述】:

我在这里浏览了几个线程,但没有找到我遇到的问题的答案。

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

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

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

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

++++++++++++++++++++ 好的,问题,

我尝试从我的手机连接到电脑,最初我收到“正在连接....”状态栏更新,大约 15 秒后,我收到一条吐司消息,说“我已连接到电脑”,但之后立即我得到“设备丢失连接”吐司。然后状态栏转到“未连接”

当我使用调试器单步执行时,蓝牙聊天应用程序的以下部分似乎失败了。特别是这一行(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 时,i/o 异常是“software caused connection abort” 对于输入流上的 read()。

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

否则,我认为输入流连接的套接字可能不可用。我将其打印到日志中,似乎它不是 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);
    }

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

谢谢

【问题讨论】:

    标签: java android bluetooth


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-13
      • 1970-01-01
      • 2014-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多