Arduino未接收蓝牙gatt信号

发布于 2025-01-31 09:53:33 字数 1252 浏览 4 评论 0原文

在我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文