Java 在阵列中存储arduino的串行打印数据
我正在使用蓝牙连接将串行数据从Arduino发送到我的Android应用程序。我用来连接Android设备的代码是Java 在阵列中存储arduino的串行打印数据,java,android,bluetooth,arduino,Java,Android,Bluetooth,Arduino,我正在使用蓝牙连接将串行数据从Arduino发送到我的Android应用程序。我用来连接Android设备的代码是BluetoothChat.java,这是谷歌搜索到的示例代码 示例代码在列表视图中显示数据,我想将其存储在数组中 这是从Arduino读取序列打印的代码 case MESSAGE_READ: byte[] readBuf = (byte[]) msg.obj; String readMessage = new String(read
BluetoothChat.java
,这是谷歌搜索到的示例代码
示例代码在列表视图中显示数据,我想将其存储在数组中
这是从Arduino读取序列打印的代码
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
String readMessage = new String(readBuf);
mConversationArrayAdapter.add(readMessage);
int a = readBuf.length;
//coordinates[] is the exact length necessary as given by double[a], and contains no more and
//no fewer positions than required.
double coordinates[] = new double[a];
//Check the values of the data in readBuf[] and make the necessary conversions to store in the
//variables.
for(int i=0; i<a; i++){
int val=readBuf[i];
/*
*(int) val/8 will divide val and round down to the nearest integer.
*val%8 divides by 8 but takes the remainder which is the value of
*all variables above x1, though the math works with x1.
*/
coordinates[(int) val/8] = val%8;
}
回归作为一个独立的Java应用程序独立工作
coordinates[(int) val/8] = val%8;
这句话让我觉得很奇怪。创建一个长度为readBuf的数组。那么为什么要使用(int)val/8
之类的东西来存储数组中的值呢
大概
coordinates[i] = val%8;
这就是你想要的。或者至少坐标[i]=something
是使用for
循环在数组中存储值的正确方法
coordinates[i] = val%8;