我有一个问题,试图从我的Android Xamarin.Forms应用程序发送命令到我的Arduino Mega。当我使用Play商店的蓝牙终端应用时,它可以工作,但我的代码却不行。在Arduino终端中,我只是得到了方块,好像是波特率的问题,但从我看到的情况来看,波特率总是由设备决定的,HM10的默认值是9600。
我使用的是这个蓝牙库 https:/github.comxabrexamarin-bluetooth-le。
Arduino Sketch (与bt终端应用配合使用)
void loop()
{
// Read from the Bluetooth module and send to the Arduino Serial Monitor
if (Serial3.available())
{
byte y = Serial3.read();
//int x = (int)y;
Serial.write(y);
}
// Read from the Serial Monitor and send to the Bluetooth module
if (Serial.available())
{
c = Serial.read();
// do not send line end characters to the HM-10
if (c != 10 & c != 13)
{
Serial3.write(c);
}
// Echo the user input to the main window.
// If there is a new line print the ">" character.
if (NL) { Serial.print("\r\n>"); NL = false; }
Serial.write(c);
if (c == 10) { NL = true; }
}
}
然后,我在Xamarin.Forms应用中的发送代码。
private async Task Button_ClickedAsync(object sender, EventArgs e)
{
if (_charataristice != null && _charataristice.CanWrite)
{
var data = Convert.ToByte(16);
_charataristice.WriteType = CharacteristicWriteType.WithoutResponse;
await _charataristice.WriteAsync(new[] { data });
}
}
感谢任何帮助,我试图恢复的值总是一个数字,应该落在一个字节之内。