无法编辑从蓝牙接收的数据

时间:2019-07-16 13:32:48

标签: java android

我通过蓝牙接收从arduino(hc05)发送的数字数据。当我使用setText显示它们时,第一个字符被截断。在转换为float时,应用程序停止工作。

public SendReceive(BluetoothSocket socket)
{
    bluetoothSocket = socket;
    InputStream tempIn = null;

    try {
        tempIn = bluetoothSocket.getInputStream();
    } catch (IOException e) {
        e.printStackTrace();
    }

    inputStream = tempIn;

}

public void run() {
    byte[] buffer = new byte[16];
    int bytes;




    while (true) {

        try {

                bytes = inputStream.read(buffer, off, len);
                handler.obtainMessage(STATE_MESSAGE_RECEIVED, bytes, -1, buffer).sendToTarget();

            }


        } catch (IOException e) {
            e.printStackTrace();

        }

    }
}

Handler handler=new Handler(new Handler.Callback(){
        @Override
        public boolean handleMessage(Message msg) {

            switch (msg.what) {

                case STATE_MESSAGE_RECEIVED:
                    byte[] readBuff = (byte[]) msg.obj;
                    String tempMsg = new String(readBuff, 0, msg.arg1);
                    msg_box.setText(tempMsg);
                    break;
            }
            return true;
        }
});

在发送数字233和Arduino在电话显示屏233上而不是现在的33后,我想这样做。我还希望能够编辑和投影接收到的数据。

0 个答案:

没有答案