Android 如何使用广播接收器检测蓝牙状态变化?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24888023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:54:04  来源:igfitidea点击:

How to detect Bluetooth state change using a broadcast receiver?

androidbluetoothbroadcastreceiverandroid-permissions

提问by Mohammad H

I am trying to make an app that shows a toast when the device's Bluetooth turned on. I wanna do that even when my app is not running. So I should use a broadcast receiver, add some permissions, an intent-filter to android manifest and make a java class but I don't know the details.

我正在尝试制作一个在设备的蓝牙打开时显示吐司的应用程序。即使我的应用程序没有运行,我也想这样做。所以我应该使用一个广播接收器,添加一些权限,一个意图过滤器到 android manifest 并创建一个 java 类,但我不知道细节。

What should I do? Which permissions should I use?

我该怎么办?我应该使用哪些权限?

回答by paNji

AS far as permissions go, to detect the state change of bluetooth you need to add this to your AndroidManifest.xml.

就权限而言,要检测蓝牙的状态变化,您需要将其添加到您的 AndroidManifest.xml 中。

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

An example receiver would look like this, you add this code to where you want to handle the broadcast, for example an activity:

示例接收器如下所示,您将此代码添加到要处理广播的位置,例如活动:

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

                if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
                    if(intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) 
    == BluetoothAdapter.STATE_OFF)
    // Bluetooth is disconnected, do handling here
}

}

        };

To use the receiver, you need to register it. Which you can do as follows. I register the receiver in my main activity.

要使用接收器,您需要注册它。您可以执行以下操作。我在我的主要活动中注册接收器。

registerReceiver(this, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));

You could also decide to add all of it to your AndroidManifest.xml. This way you can make a special class for the receiver and handle it there. No need to register the receiver, just make the class and add the below code to the AndroidManifest

您也可以决定将所有这些添加到您的 AndroidManifest.xml 中。这样你就可以为接收器创建一个特殊的类并在那里处理它。无需注册接收器,只需创建类并将以下代码添加到AndroidManifest

<receiver
        android:name=".packagename.NameOfBroadcastReceiverClass"
        android:enabled="true">
    <intent-filter>
        <action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
    </intent-filter>
</receiver>

回答by Manikanta Ottiprolu

You have to take following permission.

您必须获得以下许可。

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

and you have to write this as your intent filter in receiver tag.

并且您必须将其写为接收者标签中的意图过滤器。

<action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />

回答by Nicola Gallazzi

Unfortunately, for apps targeting api 26or higher, manifest declared broadcast receivers don't work anymore (reference here: https://developer.android.com/guide/components/broadcast-exceptions), with some exceptions. android.bluetooth.adapter.action.STATE_CHANGEDis not in that list.

不幸的是,对于面向api 26或更高版本的应用程序,清单声明的广播接收器不再工作(参考此处:https: //developer.android.com/guide/components/broadcast-exceptions),但有一些例外。 android.bluetooth.adapter.action.STATE_CHANGED不在那个列表中。

For bluetooth, you can only listen for changes on:

对于蓝牙,您只能监听以下更改:

ACTION_CONNECTION_STATE_CHANGED, ACTION_ACL_CONNECTED, ACTION_ACL_DISCONNECTED

ACTION_CONNECTION_STATE_CHANGED, ACTION_ACL_CONNECTED,ACTION_ACL_DISCONNECTED