Java 蓝牙每次读取一个输入字符

Java 蓝牙每次读取一个输入字符,java,android,android-studio,bluetooth,android-bluetooth,Java,Android,Android Studio,Bluetooth,Android Bluetooth,我有一个关于蓝牙通信的小问题,我一直在YouTube上学习如何创建一个与我的arduino通信的应用程序。一切都很好;然而,我唯一的问题是,当我在手机中测试来自arduino的传入数据时,字符串数据似乎是使用Stringbuilder随机测试的,因此测试有时可能有效,也可能无效。接收到的数据如下“B1234”。Lette B表示接收到的数据,以下数字表示数据本身,点表示流结束。我是android新手,所以如果我的问题不太清楚,我很抱歉 以下是BluetoothConnectionService的

我有一个关于蓝牙通信的小问题,我一直在YouTube上学习如何创建一个与我的arduino通信的应用程序。一切都很好;然而,我唯一的问题是,当我在手机中测试来自arduino的传入数据时,字符串数据似乎是使用Stringbuilder随机测试的,因此测试有时可能有效,也可能无效。接收到的数据如下“B1234”。Lette B表示接收到的数据,以下数字表示数据本身,点表示流结束。我是android新手,所以如果我的问题不太清楚,我很抱歉 以下是BluetoothConnectionService的onReceive java代码:

byte [] buffer = new byte [1024];
int bytes;
//Keep listening to the InputStream until an exception occurs
while(true) {
    try { 
        bytes mainstream.read(buffer);
        String incomingMessage = new String (buffer, 0, bytes);
        Intent incomingMessageIntent = new Intent ("incomingMessage");
        incomingMessageIntent.putExtra ("theMessage", incomingMessage);
        LocalBroadcastManager.getInstance (mContext).sendBroadcast(incomingMessageIntent);
    }
    catch (Exception e) {
        break;
    }
}
以下是在MainActivity中收到的java代码:

publix final BroadcastReceiver mBroadcastReceiver5 = new BroadcastReceiver () {
    public void onReceive(Context context, Intent intent) {
        final String text = intent.getStringExtra ("theMessage");
        messages.append(text); //Append StringBuilder
        FragmentData.RXData.setText (messages);
        messages.setLength(0); 
    }
}

您是否以循环方式发送数据?或者只有一次?我怀疑您的广播接收器与UI线程不同步。因此,大多数情况下,它会忽略append()。解决方案可能是使用一个处理器。数据从arduino连续发送到手机。我将尝试使用您提到的处理程序,看看是否有效