我正试图通过蓝牙从arduino发送一个int到android,但如果我发送让我们说56,我在android端收到8…无论如何,我可以收到56,因为它是,最好是字符串形式,包括字符
Arduino code:int level = 56;
Serial.write(level);
Android代码:
public void run() {
byte[] buffer = new byte[128];
int bytes;
while (true) {
try {
bytes = connectedInputStream.read(buffer);
String strReceived = new String(buffer, 0,bytes);
final String msgReceived =/* String.valueOf(bytes) +
" bytes received: "
+ */strReceived;
runOnUiThread(new Runnable(){
@Override
public void run() {
textStatus.setText(msgReceived);
value = msgReceived ;
}});
Value定义为静态字符串,作为类变量
您正在将字节转换为字符串,因此您将获得'8'
,即(char)56;
。如果您不希望这样做,只需按如下操作即可。
bytes = connectedInputStream.read(buffer);
String tmp = "";
for(int i=0;i<bytes;i++)
tmp += Byte.toString(buffer[i]);
final String msgReceived = tmp;
编辑
如果按如下方式发送,例如:
Serial.write(56);
Serial.write(76);
如果这两个字节都被读取,那么您将在msgReceived
中收到的将是5676
。显然,您可以以任何方式更改此行为。