这是我的Arduino代码:
#include <SoftwareSerial.h>
int bluetoothTx = 11;
int bluetoothRx = 12;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup() {
Serial.begin(9600);
bluetooth.begin(9600);
}
void loop() {
//Read from bluetooth and write to usb serial
if(bluetooth.available()) {
char tosend = (char)bluetooth.read();
Serial.print(tosend);
if (tosend == '1') {
Serial.println("Hello");
bluetooth.println("Hellllo");
}
}
}
在Android上我有这个功能:
public void run() {
byte[] buffer = new byte[1024]; // buffer store for the stream
int bytes = 0; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
bytes = mInStream.read(buffer);
// Send the obtained bytes to the UI activity
mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer).sendToTarget();
} catch (IOException e) {
Log.i(TAG, e.toString());
break;
}
}
}
第二件事是即使我没有写出延迟,我在Android上的输出也会分成一个帖子。请帮忙。
答案 0 :(得分:0)
我得到解决方案的事情是我发送数据的时间只有我正在接收数据,因为数据只是来自蓝牙慢慢地出现了问题。 \ n所以我所做的解决问题的方法是让线程睡眠几秒钟然后我得到了完整的输出.. Finnallly