我想知道如何在Android中将5个字符串发送到Arduino以存储在变量中?

时间:2014-08-15 08:50:22

标签: android arduino

我在5个edittexts上使用Amarino在Android上接收5个字符串,然后发送给Arduino。

//Code to send a String.  One of five Strings.
Amarino.sendDataToArduino(MainActivity.this,"HC-05",'a',name);

在Arduino中,我为每个String创建了函数以保存变量。但我不能通过编译。有一个错误消息“空值不被忽略,因为它应该是”。

//Arduino code.  I take part of custom function code that relate Meetandroid.
void getUsername(byte flag, byte numOfValues)
{
  // first we need to know how long the string was in order to prepare an array 
  // big enough to hold it.
  // you should know that: (length == 'length of string sent from Android' + 1)
  // due to the '\0' null char added in Arduino
  int length = meetAndroid.stringLength();

  // define an array with the appropriate size which will store the string
  char data[length];

  // tell MeetAndroid to put the string into your prepared array
  //Can't compile Here
  username = meetAndroid.getString(data);
}

我也尝试过发送String数组。 Amarino中有一个函数但我找不到接收函数。我不知道我可能会错过什么。如何解决这个问题?

//data is array of String in Andriod
Amarino.sendDataToArduino(MainActivity.this,"HC-05",'a',data);

2 个答案:

答案 0 :(得分:0)

meetAndroid.getString(data)没有返回值。因此,您无法将其分配给username

答案 1 :(得分:0)

char data[length];

username = meetAndroid.getString(data);

将其更改为:

char data [] = new char[length];

meetAndroid.getString(data);

String username = data.toString();

return username; // and change function return type to String

您确定byte data [] = new byte[length];不合适吗?