【问题标题】:Read Blutooth Device data and print it读取蓝牙设备数据并打印
【发布时间】:2010-11-15 07:23:11
【问题描述】:

任何人都可以告诉我如何在 LogCat 中显示蓝牙设备(MyGlucoHealth 仪表)数据。以下代码:

InputStream is = btSocket.getInputStream();

如何从“is”中读取数据并打印出来。 ?

谢谢, 陈娜

【问题讨论】:

    标签: android bluetooth device inputstream


    【解决方案1】:
    【解决方案2】:

    查看BluetoothChat 示例。 ConnectedThread.run()InputStream 读取数据。

        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 上打印数据。其说明:软件导致 IOException。 java.io.IOException: Connection reset by peer.. 谢谢,Chenna
    • 连接丢失时会出现 IOException。您确定要连接设备吗?
    • 是的,我确定设备已连接,并且我的葡萄糖设备连接丢失后出现的错误..
    • 如果连接丢失,您将收到 IOException。如果设备已连接,那么您的设备发送的任何字节都应该能够由 android 设备从流中检索。我对你提到的设备不熟悉,所以我可能误解了你的问题。
    • 但是没有太多的读数要发送(历史上可能有 2 个读数..)设备保持连接很长一段时间(大约 2 或 3 分钟)..设备可以发送那个时候的数据很容易......我猜......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多