c ++ / Arduino会处理'?'与其他字符

时间:2016-03-14 23:52:03

标签: android c++ bluetooth arduino

我有一个Android应用程序通过蓝牙与arduino板进行通信

所有命令都向前和向前移动,直到我想发送一个类型为

的命令
"aT?bb"
从Android应用程序

,但是当我在ardunio中打印它时,我正在

"aT%3F"

我在android中记录命令并且它正确形成我的问题是c ++ / Arduino处理'?'不同于正常的字符?

这是我的arduino代码 - >

while(bluetooth.available())
{
char toSend = (char)bluetooth.read();
if(toSend != '\0'){
    if (toSend == 'a'){ i=0 ;}
    inMsg[i] = toSend;
    i++;
  } 
}
if(i == 5 )
{
// mock sending queries
  if(inMsg[2] == '?'){
   if(inMsg[1] == 'T'){
      bluetooth.write("ty1");Serial.println("");
    }else if(inMsg[1] == 'x'){  //normal cycle
       bluetooth.write("xx1");
    }else if(inMsg[1] == 'X'){ Serial.println(""); //booter
       bluetooth.write("XX0");
    }else if(inMsg[1] == 'N'){Serial.println("");  //On time 
       bluetooth.write("on1");
    }else if(inMsg[1] == 'F'){ Serial.println(""); //Off time
       bluetooth.write("of30");
    }else if(inMsg[1] == 'S'){ Serial.println(""); //Speed percent
       bluetooth.write("sp30");
    }
}
// write to console 
  for(int j = 0; j < 5; j++){
    Serial.write(inMsg[j]);
}
// new line
  if(i == 5){Serial.println("");}
  i = 0; // reset buffer
}


aT%3F <- this is mal formed
aS133 <- all the other are as I sent them from android 
aN169
aF192
aS200
aXXXX
aYYYY
ayYYY
axXXX

我的Android代码      ...      command =“aT?bb”;      writeCommand(命令);      ...

private void writeCommand(String command)
{
    for (BluetoothGattCharacteristic characteristic : characteristics)
    {
        if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) >0)
        {
            try {
                characteristic.setValue(URLEncoder.encode(command, "utf-8"));
                gatt.writeCharacteristic(characteristic);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

正如上面的评论所指出的那样,URLEncoder正在改变String。我现在已将此方法更改为

private void writeCommand(String command)
{
    for (BluetoothGattCharacteristic characteristic : characteristics)
    {
        if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) >0)
        {
            characteristic.setValue(command);
            gatt.writeCharacteristic(characteristic);
        }else{
            Log.i(TAG,"non write able");
        }
    }
}