隐式广播android.bluetooth.device.action.ACTION_ACL_CONNECTED无法正常工作

隐式广播android.bluetooth.device.action.ACTION_ACL_CONNECTED无法正常工作

问题描述:

当我在AndroidManifest中声明一个隐式广播接收器时,我的接收器没有被调用.

My receiver is not getting called when I declare an implicit broadcast receiver in AndroidManifest.

<receiver
    android:name=".BluetoothConnectionReceiver_"
    android:enabled="true"
    android:exported="true"

    android:permission="android.permission.BLUETOOTH,
    android.permission.BLUETOOTH_ADMIN">
    <intent-filter>
            <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
            <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
    </intent-filter>
</receiver>

我了解在奥利奥(Oreo)中,某些隐式广播受到限制.但是在文档中, https://developer.android.com/guide/components/broadcast-例外,ACL_CONNECTED和ACL_DISCONNECTED不在其中.

I understand that in Oreo, there is a restriction on some implicit broadcasts. But in the doc, https://developer.android.com/guide/components/broadcast-exceptions, ACL_CONNECTED and ACL_DISCONNECTED are not among them.

声明< uses-permission android:name ="android.permission.ACCESS_COARSE_LOCATION"/> 在您的AndroidManifest中,并且您必须签入运行时以验证是否已授予> = Lollipop版本的权限.

Declare <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> in your AndroidManifest and you must check in runtime to verify if the permission is granted for the versions >= Lollipop.

使用此代码:

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
    } else {
        //Permission is already granted
    }

使用 ContextCompat ActivityCompat 类,您无需检查正在运行的SDK/OS版本是否为Lollipop.如果版本<棒棒糖方法 ContextCompat.checkSelfPermission()将返回true.

Using ContextCompat and ActivityCompat classes you don't check if the running SDK/OS version is >= Lollipop. In case of the version < Lollipop the method ContextCompat.checkSelfPermission() will return true.