【发布时间】:2017-03-14 05:55:09
【问题描述】:
我正在尝试通过蓝牙低功耗 bt-410 在 Android 设备和 Arduino Mega 2560 之间发送和接收数据, 当我收到数据类型是字节 [] 并且我解码为字符串时,它是一个问题符号,所以我尝试将日志字节 [] 转换为字节,然后值为 -32,当我将一些字符串从我的应用程序发送到 Arduino 中的串行监视器时没什么。 如何发送和接收数据?
//Receive data:
@Override
public void onCharacteristicChanged(BluetoothGatt mGatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(mGatt, characteristic);
String textRX;
try {
textRX = new String(characteristic.getValue(), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
writeLine("Received: " + textRX);
hideKeyboard();
scrollDown();
}
//Send data:
public void sendClick(View view) {
String message = editInput.getText().toString();
if (tx == null || message.isEmpty()) {
return;
}
try {
value = message.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
tx.setValue(value);
if (mGatt.writeCharacteristic(tx)) {
writeLine("Sent: " + message);
editInput.setText("");
hideKeyboard();
scrollDown();
Log.e("textTX", tx + "");
} else {
writeLine("Couldn't write TX characteristic!");
}
}
//Arduino Code:
String inputString="";
void setup() {
Serial.begin(9600);
}
void loop() {
if(Serial.available() > 0) {
inputString = Serial.readStringUntil('\n');
Serial.println(inputString);
if (inputString == "R1") {
delay(100);
Serial.println("R1 on");
}
}
delay(10);
}
【问题讨论】:
标签: android bluetooth arduino bluetooth-lowenergy