How to detect Bluetooth state change using a broadcast receiver?

前端 未结 3 1537
逝去的感伤
逝去的感伤 2020-12-02 18:59

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,

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-02 19:15

    Unfortunately, for apps targeting api 26 or 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_CHANGED is not in that list.

    For bluetooth, you can only listen for changes on:

    ACTION_CONNECTION_STATE_CHANGED, ACTION_ACL_CONNECTED, ACTION_ACL_DISCONNECTED

提交回复
热议问题