我已经使用现在经典的 Google 蓝牙聊天代码实现了蓝牙连接。但是,我有一个问题,我似乎无法全神贯注。
输入流的读取是这样的:
public void run() {
byte[] buffer = new byte[1024]; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
break;
}
}
}
现在,如果我只是打印出我在原始示例中收到的字符,那很好。但是,假设我想传输一个图像文件。我不知道文件的大小,所以我无法计算接收到的字节数或类似的东西。在我的测试中,我似乎从来没有从输入流中收到“-1”,这似乎是从输入流中读取的“标准”。那么我怎么知道我已经到达了正在发送的文件的末尾呢?
感谢您的帮助和时间。
最佳答案
Android 蓝牙输入流似乎永远不会返回 -1。
我想通过首先发送文件大小和最后发送 EOF 信号来设置一个简单的协议(protocol)会有所帮助。
关于android - 如何在android蓝牙文件传输上检测EOF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5768294/