如何将 android 中的 bluetoothSPP 的两个值发送到 arduino?

How can I send two values from bluetoothSPP in android to arduino?

这是我的代码。

我想在 Arduino 中从 android 应用程序接收两个值。

我可以收到一个值,但我不能收到两个值。

这是我的代码。

我想用 Bluetooth.send() 发送两个值。

我该怎么做?

我在 android 工作室中使用蓝牙 SPP。

我想在 arduino 中接收 Go、Back、Right、Left 和 seconds。

所以如果我收到 Go 和 2sec,我的 arduino rc 汽车会走 2 秒。

//Arduino code
#include<SoftwareSerial.h>
#include <Servo.h>

SoftwareSerial BT(blueTx,blueRx);
Servo servo;
char input1;
char input2;

void loop() {
  if(BT.available()){
    if(Serial.available()){
      Serial.println("------");
      Serial.println(BT.read());
    }   
    
    input1 = BT.read();
    input2 = BT.read();   
  }
 }

// Android code , i use bluetoothSPP and send method

blockStartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!list.isEmpty()) {
                        delayHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                while(!list.isEmpty()) {
                                    String blockName = list.get(0);
                                    num1 = inputNum.getText().toString();
                                    list.remove(0);
                                    adapter.notifyDataSetChanged();
                                    if (blockName == "GO") {
                                        Bluetooth.send("G", true);
                                        Bluetooth.send(num1, true);
                                    } else if (blockName == "BACK") {
                                        Bluetooth.send("K", true);
                                        Bluetooth.send(num1, true);
                                    } else if (blockName == "LEFT") {
                                        Bluetooth.send("L", true);
                                        Bluetooth.send(num1, true);
                                    } else if (blockName == "RIGHT") {
                                        Bluetooth.send("R", true);
                                        Bluetooth.send(num1, true);
                                    }
//                                    inputNum.setText(null);
                                }
                            }
                        }, 10);

                }

我在问题中遇到了类似的问题,虽然我没有使用蓝牙 spp,但试一试,尝试在 send 中发送字节数组 我将我的输入设计为 a,b 并将其转换为字节数组,在您的情况下将其发送为 G/K/L/R,num1 看到添加的逗号,将其转换为字节数组使用 getBytes() ,在 aurdino 中执行以下操作:

void loop() {

if(Serial.available()){
  int available = Serial.available();
  for(int i=0; i< available; i++){
  char a = Serial.read();
 
  if(i < 1){ 
  input1 = a;
   }
  if(i > 1){
  int c =(int)a - 48;
    input2 *= 10;
    input2  +=  c;
  }
   
  }
} 

解释跳过逗号是第二个字符(1),第一个字符是正常的所以将它添加到 input1 ,第二个是数字所以提取 ascii 值从 48 开始并从中获取数字。Input2 是类型 int 初始值为 0 , Enjoy!