有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Bluetooth每次读取一个传入字符

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

共 (0) 个答案