我使用蓝牙连接将Arduino的串行数据发送到我的Android应用程序。我用来连接Android设备的代码是BluetoothChat.java
,这是用Google搜索时可以找到的示例代码。
示例代码显示ListView
中的数据,我想将其存储在数组中。
以下是从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;
}
我认为我正确存储了数据。但是当我使用coordinates[]
时,它并没有更改TextView
我设置的文字。以下是设置文本的代码。
if(readBuf.length==4){
double x1 = coordinates[0];
double x2 = coordinates[1];
double y1 = coordinates[2];
double y2 = coordinates[3];
r = new Regression(x1, x2, y1, y2);
r.computation();
leftLung.setText("Left Lung "+ r.leftLungDamage() +"% hit");
rightLung.setText("Right Lung "+ r.rightLungDamage() +"% hit");
heart.setText("Heart "+ r.heartDamage() +"% hit");
liver.setText("Liver "+ r.liverDamage() +"% hit");
smallIntestine.setText("Small Intestine "+ r.smIntestineDamage() +"% hit");
largeIntestine.setText("Large Intestine "+ r.lgIntestineDamage() +"% hit");
stomach.setText("Stomach "+ r.stomachDamage() +"% hit");
spleen.setText("Spleen "+ r.spleenDamage() +"% hit");
gallbladder.setText("Gallbladder "+ r.gallbladderDamage() +"% hit");
lKidney.setText("Left Kidney "+ r.lKidneyDamage() +"% hit");
rKidney.setText("Right Kidney "+ r.rKidneyDamage() +"% hit");
pancreas.setText("Pancreas "+ r.pancreasDamage() +"% hit");
venaCava.setText("Vena Cava "+ r.venaCavaDamage() +"% hit");
dAorta.setText("Descending Aorta "+ r.dAortaDamage() +"% hit");
aAorta.setText("Ascending Aorta "+ r.aAortaDamage() +"% hit");
}
回归本身就是一个独立的Java应用程序。
答案 0 :(得分:0)
coordinates[(int) val/8] = val%8;
这条线让我觉得奇怪的阵列对齐。您创建一个长度为readBuf的数组。那么为什么要使用(int) val/8
之类的东西来存储数组中的值。
可能
coordinates[i] = val%8;
是你想要的。或者至少coordinates[i] = something
是使用for
循环在数组中存储值的正确方法。