我正在开发使用adk模式通信的android和arduino应用程序我已经在我的主要活动中实现了runnable,并且在运行方法中我正在读取值并且当读取完成时我正在向arduino板写入值并且通信运行无限我正在使用处理程序来更新GUI,但是应用程序在某段时间后停止了它的通信,我无法弄清楚是什么问题?处理程序中的GUI更新是否需要更多时间才能发生或其他任何事情?
以下代码用于接收和发送数据:
while (true) { // read data
try {
int ret_read = 0;
byte[] buffer_read = new byte[128];
ret_read = mInputStream.read(buffer_read);
for (int p = 0; p < 2 * (noOfFSensors); p = p + 2) {
intListValues.add((int) composeInt(buffer_read[p],
buffer_read[p + 1]));
Log.d("Jankari", "Pu Value : "+String.valueOf((int) composeInt(buffer_read[p],
buffer_read[p + 1])));
}
Message mPu = Message.obtain(mHandlerPuSensors);
mPu.obj = new ValueMsgPuSensors("f", ret_read,intListFValues);
mHandlerPuSensors.sendMessage(mPu);
for (int a = 0; a < 2 * (noOfAnSensors); a = a + 2) {
intListAnValues.add((int) (composeInt(buffer_read[2* (noOfFSensors) + a],
buffer_read[2* (noOfFSensors) + a + 1])));
Log.d("Jankari", "An Value : "+String.valueOf(
(int) (composeInt(buffer_read[2* (noOfFSensors) + a],
buffer_read[2* (noOfFSensors) + a + 1]))));
}
Message mAn = Message.obtain(mHandlerAnSensors);
mAn.obj = new ValueMsgAnSensors("f", ret_read,
intListAnValues);
mHandlerAnSensors.sendMessage(mAn);
for (int l = 0; l < noOfSw; l++) {
intListSwValues.add((int) buffer_read[2* (noOfFSensors) +
2 * (noOfAnSensors) + l]);
Log.d("Jankari", "Sw : "+String.valueOf((int)
buffer_read[2* (noOfFSensors) + 2 * (noOfAnSensors) + l]) );
}
Message mSw = Message.obtain(mHandlerSwSensors);
mSw.obj = new ValueMsgSwSensors("f", ret_read,intListSwValues);
mHandlerSwSensors.sendMessage(mSw);
i += 1;
mInputStream.close();
// Send Output to arduino
sendoutput();
} catch (IOException e) {
break;
}
}
我正在使用的处理程序是
Handler mHandlerPuSensors = new Handler() {
@Override
public void handleMessage(Message msg) {
ValueMsgPuSensors msgPu = (ValueMsgPuSensors) msg.obj;
txtReceivedBytes.setText("Received Bytes: " + msgPu.getRet());
for (int i = 0; i < txtListFSensors.size(); i++) {
txtListFSensors.get(i).setText(String.valueOf(msgPu.getFReadings().get(i)));
}
intListFValues.clear();
}
};