Implicit Broadcast android.bluetooth.device.action.ACTION_ACL_CONNECTED NOT working

走远了吗. 提交于 2021-02-08 03:25:36

问题


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>

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.


回答1:


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.

Use this code:

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
    }

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.



来源:https://stackoverflow.com/questions/50284008/implicit-broadcast-android-bluetooth-device-action-action-acl-connected-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!