【问题标题】:Android Bluetooth Serial Communication reads symbol "�"Android 蓝牙串行通信读取符号“�”
【发布时间】:2016-01-17 09:57:27
【问题描述】:

我编写了 Android 代码来连接蓝牙 HC-05,向 HC-05 发送命令并接收与发送的命令相关的不同数据。 Android 应用程序连接蓝牙并在发送第一个命令时收到我想要的确切数据,但在下一个命令中它会收到符号“�”以及相关数据。

我在安卓上用其他蓝牙终端测试过硬件电路,效果很好。

以下是我的串行通信代码:

void beginListenForData() {
        final Handler handler = new Handler();
        final byte delimiter = 10; 
        stopWorker = false;
        readBufferPosition = 0;
        readBuffer = new byte[1024];
        workerThread = new Thread(new Runnable() {
            public void run() {
                while (!Thread.currentThread().isInterrupted() && !stopWorker) {
                    try {
                        int bytesAvailable = mmInputStream.available();

                        if (bytesAvailable > 0) {
                            byte[] packetBytes = new byte[bytesAvailable];
                            mmInputStream.read(packetBytes);

                            for (int i = 0; i < bytesAvailable; i++) {
                                byte b = packetBytes[i];
                                if (b == delimiter) {
                                    byte[] encodedBytes = new        byte[readBufferPosition];
                                    System.arraycopy(readBuffer, 0,
                                            encodedBytes, 0,
                                            encodedBytes.length);
                                    final String data = new String(
                                            encodedBytes,"US-ASCII" );
                                    readBufferPosition = 0;

                                    handler.post(new Runnable() {
                                        public void run() {
                                            sampleView.setText(data);
                                            str = sampleView.getText()
                                                    .toString();
                                            Log.i("Data", data);                                        
 }

                                    });
                                } else {
                                    readBuffer[readBufferPosition++] = b;
                                }
                            }
                        }

                    } catch (IOException ex) {
                        stopWorker = true;
                    }
                }

            }


        });

        workerThread.start();
        try {


            String command1=command.getText().toString();
            mmOutputStream.write(command1.getBytes());



        } catch (IOException e) {

            e.printStackTrace();
        }
    }

请帮忙!!

【问题讨论】:

    标签: android bluetooth


    【解决方案1】:

    有时会出现这种乱码,你应该注意你的字符集格式。你可以将你的命令都转换为“UTF-8”。

    另一种情况可能是:你从错误的开始或结束位置读取数据。

    【讨论】:

    • 抱歉回复晚了...我试过“UTF-8”但情况没有变化。
    • 这个符号的十六进制表示是什么?它不一定是“�”,但可能是“无法识别”字符的表示。我想问题是你的分隔符检测。如果 HC-05 发送的 UTF 字符的低字节为 0x10,则可能会被您的代码误解。
    • 如果问题是分隔符检测,那么它总是会发送这个符号。但是在第一个命令的情况下,它会发送正确的数据,而第二个命令会在代码中读取符号和数据。您认为我应该对上述代码进行哪些更改?
    猜你喜欢
    • 1970-01-01
    • 2016-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多