我使用现在的经典Google蓝牙聊天代码实现了蓝牙连接.但是,我有一个问题,我似乎无法将我的大脑包裹起来. 读取输入流的方式如下: public void run() { byte[] buffer = new byte[1024]; // buffer stor
读取输入流的方式如下:
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信号将有所帮助.