1

我正在开发使用 adk 模式通信的 android 和 arduino 应用程序我已经在我的主要活动和运行方法中实现了可运行我正在读取值,当读取完成时我正在将值写入 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();         
    }
};
4

0 回答 0