Android 通过蓝牙接收接收文本

Android ReceiveReceive Text via Bluetooth

我尝试使用 Android 通过蓝牙 (BT) 接收文本。我可以使用 Google Play 商店中已有的蓝牙记录器查看文本,但使用我自己的代码无法正常工作。

通常我使用 Android 文档和 samples 中的默认 BT 代码。我记录收到的文本。我希望我的文字是这样的: -0.01, -0.03, 1.04,但我只收到了其中的一部分。这是日志的一部分:

06-14 17:25:01.925 28217-28412/[...]: I read the following: 0.01, -0.
06-14 17:25:01.977 28217-28412/[...]: I read the following: 
06-14 17:25:02.039 28217-28412/[...]: I read the following: -
06-14 17:25:02.051 28217-28412/[...]: I read the following: 0.01, -0.
06-14 17:25:02.101 28217-28412/[...]: I read the following: 
06-14 17:25:02.174 28217-28412/[...]: I read the following: -

这是我用来读取BT的代码:

    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);
                String readMessage = new String(buffer, 0, bytes);
                Log.e(Constants.TAG, "I read the following: "+readMessage);
            } catch (IOException e) {
                break;
            }
        }
    }

不确定是否相关,但我正在使用连接到 Arduino 的 HC-06 BT 模块以 9600 波特率发送数据。我也可以给你代码。

我尝试了两种不同的 Android 手机,结果相同。 Wiko Fever 和 Moto G2。

好的,我找到问题了。实际传输没问题。这是两件事的结合。首先,我使用 Serial.println 从 Arduino 传输数据,我将其更改为 Serial.print。我也扔掉了日志并将结果发送到 UI.

因此,Android 日志记录框架似乎对 Arduino 发送的新行字符很敏感,并且(部分)跳过日志消息,如您在上面所见。