我打算在arduino和移动设备之间传输数据。现在,我可以从移动设备中的arduino读取数据,但无法将数据发送到arduino板。这是我用于数据传输的代码:
Android代码:
void sendData() throws IOException { String msg = myTextbox.getText().toString(); msg += "\n"; mmOutputStream.write(msg.getBytes()); //mmOutputStream.write('A'); myLabel.setText("Data Sent"+msg.getBytes()); }
Arduino代码:
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx); void loop() { char aChar = bluetooth.read(); Serial.print(aChar); }
如果有人能帮助我解决这个问题,我将不胜感激。
答案 0 :(得分:0)
我遇到了同样的问题。从Arduino发送数据时我添加了一个小延迟。这是一个例子。
void loop()
{
if (Serial.available() > 0)
{
char data = Serial.read();
Serial.print(data);
}
delay(5);
}
答案 1 :(得分:0)
以前我的Tx和Rx设置为:
int bluetoothTx = 2; // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 3; // RX-I pin of bluetooth mate, Arduino D3
我将引脚更改为:
int bluetoothTx = 8; // TX-O pin of bluetooth mate, Arduino D8
int bluetoothRx = 10; // RX-I pin of bluetooth mate, Arduino D10
现在它工作正常。