我试图通过蓝牙从arduino发送一个int到android但是如果我发送让我们说56,我在android方面收到8 ...无论如何,我可以收到56,因为它是最好以字符串形式包括字符
Arduino代码:
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 ;
}});
将值定义为静态字符串作为类变量
答案 0 :(得分:1)
您将字节转换为字符串,因此您获得的'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
。显然,您可以以任何方式更改此行为。