通过蓝牙连接从Arduino清除收到的串行数据

时间:2019-06-29 03:40:18

标签: android bluetooth arduino

我有一个通过蓝牙连接到我的Android手机的Arduino UNO板。一切都好。来自android手机的任何命令都在Arduino中接收并执行。但是arduino的反馈是错误的。

这是Arduino的第一个数据

device information :
pin 4 set to 0
pin 5 set to 0
pin 6 set to 0

但是第二个数据是这样的

change pin 4 to 1n :
pin 4 set to 0
pin 5 set to 0
pin 6 set to 0

我期望的是

change pin 4 to 1

这是我从互联网上获得的android代码

mHandler = new Handler(){
  public void handleMessage(android.os.Message msg){
  if(msg.what == MESSAGE_READ){
    String readMessage = null;
    try {
      readMessage = new String((byte[]) msg.obj, "UTF-8");
      //readMessage[bytes] = '\0';
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
    mReadBuffer.append(readMessage+"\n");
  }

  if(msg.what == CONNECTING_STATUS){
    if(msg.arg1 == 1)
      mBluetoothStatus.setText("Connected to Device: " + (String)(msg.obj));
    else
      mBluetoothStatus.setText("Connection Failed");
  }
  }
};

private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;

public ConnectedThread(BluetoothSocket socket) {
  mmSocket = socket;
  InputStream tmpIn = null;
  OutputStream tmpOut = null;

  try {
    tmpIn = socket.getInputStream();
    tmpOut = socket.getOutputStream();
  } catch (IOException e) { }

  mmInStream = tmpIn;
  mmOutStream = tmpOut;
}

public void run() {
  byte[] buffer = new byte[1024];  
  int bytes; 

  while (true) {
    try {

      bytes = 0;
      bytes = mmInStream.available();
      if(bytes != 0) {
        SystemClock.sleep(100); 
        bytes = mmInStream.available(); 
        bytes = mmInStream.read(buffer, 0, bytes); 

        mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget(); 
      }
    } catch (IOException e) {
      e.printStackTrace();
      break;
    }
  }
}


public void write(String input) {
  byte[] bytes = input.getBytes();           
  try {
    mmOutStream.write(bytes);
  } catch (IOException e) {
    Toast.makeText(getApplicationContext(), "Show Paired Devices", Toast.LENGTH_SHORT).show();
    mReadBuffer.append("Error Sending Data\n");
  }
}

public void cancel() {
  try {
    mmSocket.close();
  } catch (IOException e) {

  }
}
}

请帮助。

1 个答案:

答案 0 :(得分:0)

我不知道这是怎么发生的,但是反应很明显……有人可以向我解释,或者可能有更好的答案?

mHandler = new Handler(){
  public void handleMessage(android.os.Message msg){

    if(msg.what == MESSAGE_READ){
      String readMessage = "";
      /*
      try {
        byte[] readBuf = (byte[]) msg.obj;
        readMessage = new String(readBuf, "UTF-8");
        readMessage = new String(readBuf, 0,13);
        readMessage[bytes] = '\0';
      } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
      }
      */
      readMessage = new String((byte[]) msg.obj, 0, msg.arg1);
      mReadBuffer.append(readMessage+"\n");
    }

    if(msg.what == CONNECTING_STATUS){
      if(msg.arg1 == 1)
        mBluetoothStatus.setText("Connected to Device: " + (String)(msg.obj));
      else
        mBluetoothStatus.setText("Connection Failed");
    }
  }
};