0

我是android开发应用程序的新手。想从我的蓝牙套接字中读取缓冲区中的数据并显示它。数据来自温度传感器(LM35 > 微控制器 > 蓝牙模块 > 安卓设备)。下面是读取数据并将数据保存到缓冲区的编码。我想知道如何显示它。希望你们能帮助我。非常感谢您..

         byte[] buffer = new byte[1024];
         int bytes;
         try {
              InputStream instream = Bee_btSocket.getInputStream();
              bytes = instream.read(buffer);
             }

         catch (IOException e) {
         break;
4

1 回答 1

0

我通过以下代码来做这件事......

public ConnectedThread(BluetoothSocket socket) {
            Log.d(TAG, "create ConnectedThread");
            mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            // Get the BluetoothSocket input and output streams
            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) {
                Log.e(TAG, "temp sockets not created", e);
            }

            mmInStream = tmpIn;
            mmOutStream = tmpOut;
        }

        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;
                }
            }
        }

于 2014-10-09T10:38:51.280 回答