gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 05:50:19 25 4
gpt4 key购买 nike

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

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

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

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

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

++++++++++++++++++++好的解决问题,

我尝试从手机连接到电脑,最初我收到“正在连接...”状态栏更新,大约 15 秒后,我收到一条 Toast 消息,显示“我已连接到电脑”,但紧接着我收到“设备失去连接” toast 。然后状态栏变为“未连接”

当我使用调试器进行单步调试时,蓝牙聊天应用程序的以下部分似乎失败了。具体来说,这一行 (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 异常是“软件导致连接中止”用于输入流上的 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);
}

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

谢谢

最佳答案

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

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

关于java - Android 蓝牙聊天应用程序(设备立即失去连接),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4877305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com