Arduino未接收蓝牙gatt信号
在我的Android Studio应用程序上,我有一个按钮,当按下时,发送一个“ GO”信号并发行时发送“停止”信号。
当您按下按钮然后释放时,Arduino接收这两个信号都可以正常工作。但是,当它是快速点击时,Arduino不会收到停止信号。
我已经检查了实际上是发送信号的,因为每当发生这种情况时,我都会输入日志消息,因此Arduino不得该应用程序不检测到按钮,这一定是一个问题。
这是代码:
ImageButton fwdBtn = (ImageButton) findViewById(R.id.fwd);
fwdBtn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
view.performClick();
sendSignal("fwd");
return true;
case MotionEvent.ACTION_UP:
sendSignal("stop");
return true;
}
return false;
}
});
private void sendSignal(String signal) {
if (checkPermission(Manifest.permission.BLUETOOTH, 101)) {
// Commands is a dictionary for converting string commands to integers
msg.setValue(commands.get(signal));
gatt.beginReliableWrite();
gatt.writeCharacteristic(msg);
}
}
编辑:
我已经意识到gatt.writecharacteristic(msg)正在返回false。我试图通过进行一段时间循环持续运行写入细菌来解决它,直到它返回真实,并且仅部分修复了它。一旦不再有问题,快速敲击它。但是,当您垃圾邮件垃圾邮件时,问题又出现了5到6倍。任何帮助将不胜感激!
On my android studio app, I have a button that when pressed, sends a "go" signal and on release sends a "stop" signal.
When you hold the button down and then release it works fine with the Arduino receiving both signals. However, when it is a quick tap, the Arduino does not receive the stop signal.
I have checked that the signal is in fact being sent as I put a log message whenever this happens so it must be a problem with the Arduino not the app not detecting button presses.
Here is the code:
ImageButton fwdBtn = (ImageButton) findViewById(R.id.fwd);
fwdBtn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
view.performClick();
sendSignal("fwd");
return true;
case MotionEvent.ACTION_UP:
sendSignal("stop");
return true;
}
return false;
}
});
private void sendSignal(String signal) {
if (checkPermission(Manifest.permission.BLUETOOTH, 101)) {
// Commands is a dictionary for converting string commands to integers
msg.setValue(commands.get(signal));
gatt.beginReliableWrite();
gatt.writeCharacteristic(msg);
}
}
EDIT:
I have since realised that the gatt.writeCharacteristic(msg) is returning false. I tried to fix it by doing a while loop constantly running writeCharacteristic until it returned true and it only partially fixed it. Tapping it quickly once is no longer problematic. However when you spam it 5 or 6 times the problem comes back. Any help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论