Android 蓝牙 ACTION_DISCOVERY_FINISHED 不起作用

发布于 2024-12-11 16:16:29 字数 822 浏览 0 评论 0原文

我已经编写了我的第一个 Android 应用程序,一切都运行良好,除了......在下面的例程中,ACTION_DISCOVERY_FINISHED 似乎从未被调用(或者广播或接收或其他)。不管怎样,“else if”中的代码块都不起作用。

我只在我的 摩托罗拉 Atrix 上进行了测试,所以我想知道这是否是问题所在。由于我正在测试蓝牙功能,因此我认为我无法使用Android模拟器进行有效测试。

想法?

private BluetoothAdapter mBtAdapter;
mBtAdapter.startDiscovery();

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        // When discovery finds a device
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
        //do something
        }

        else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            //do something else
        }
    }
}

I have written my first Android app and everything is working really well, except...in the routine, below, the ACTION_DISCOVERY_FINISHED never seems to get called (or broadcast or received or whatever). No matter what the block of code in that "else if" is not working.

I have only tested on my Motorola Atrix, so I am wondering if that is the issue. Since I am testing bluetooth functionality, I don't think I can use the Android emulator for effective testing.

Thoughts?

private BluetoothAdapter mBtAdapter;
mBtAdapter.startDiscovery();

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        // When discovery finds a device
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
        //do something
        }

        else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            //do something else
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北凤男飞 2024-12-18 16:16:29

2 种可能的解决方案:

  1. BroadcastReceiver 进行子类化,然后在项目清单中声明它(请记住声明您的接收器接收您想要的这些操作)。

  2. 从您的活动/服务中动态注册它,如下所示:

    IntentFilter filter = new IntentFilter();
    filter.addAction(BluetoothDevice.ACTION_FOUND);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    this.registerReceiver(mReceiver, 过滤器);
    

我不确定从活动/服务注册它时是否必须取消注册它(我知道从应用程序的上下文注册时必须取消注册) )所以检查一下。

2 possibles solutions:

  1. Instead of creating an annonymous receiver, subclass BroadcastReceiver with just the same implementation, then declare it in your project manifest (Remember to declare that your receiver receives these actions you want).

  2. Dynamically register it from your activity/service, this way:

    IntentFilter filter = new IntentFilter();
    filter.addAction(BluetoothDevice.ACTION_FOUND);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    this.registerReceiver(mReceiver, filter);
    

I'm not sure if you have to unregister it when registering it from an activity/service (I know you have to when registering from app's context) so check it out.

爱你不解释 2024-12-18 16:16:29

您需要将该行添加

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

到您的清单中

You need to add the line

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

to your manifest

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文