android 9 监听蓝牙 ACTION_FOUND广播无响应解决办法

1、问题
高版本android 给定蓝牙权限,并注册监听事件后,ACTION_FOUND还是不能触发事件。程序如下图所示。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.king.Bluetooth.activity">

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED"
        tools:ignore="ProtectedPermissions" />
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
        intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

2、解决办法:
经过很多次测试发现,即使给了权限软件好使拿不到,在程序运行的时候判断一下,如果权限没获取到,在动态获取下即可解决问题。可以在onCreate函数中添加下面程序即可。

    /*
    this method is required for all devices running  API 23+ (Android 6.0 + )
    Android must programmatically check the permission for bluetooth
    only put permission in manifest is not enough
    Note: this will only execute on version > LOLLIPOP because it is not needed otherwise.
    */
    private void checkBTPermission() {
        Log.d(TAG, "checkBTPermission: Start");
        if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP){
            int permissionCheck = 0;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                permissionCheck = this.checkSelfPermission("Manifest.permission.ACCESS_FINE_LOCATION");
                permissionCheck += this.checkSelfPermission("Manifest.permission.ACCESS_COARSE_LOCATION");
                if(permissionCheck != 0){
                    this.requestPermissions(new String[]{
                            Manifest.permission.ACCESS_FINE_LOCATION,
                            Manifest.permission.ACCESS_COARSE_LOCATION
                    }, 1001); //any number
                }else{
                    Log.d(TAG,
                            "checkBTPermissions: No need to check permissions. SDK version < LOLLIPOP.");
                }
            }

        }
        Log.d(TAG, "checkBTPermission: Finish");
    }
```![在这里插入图片描述](https://img-blog.csdnimg.cn/20210113204944965.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3Nvbmdpc2dvb2Q=,size_16,color_FFFFFF,t_70)


   	

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Android 中,可以通过注册广播接收器来监听蓝牙设备的连接和断开事件。具体步骤如下: 1. 在 AndroidManifest.xml 文件中添加以下权限: ```xml <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> ``` 2. 创建一个广播接收器类,继承 BroadcastReceiver 并实现 onReceive 方法: ```java public class BluetoothReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) { // 蓝牙设备连接成功 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); Log.i("BluetoothReceiver", "Device connected: " + device.getName()); } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) { // 蓝牙设备连接断开 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); Log.i("BluetoothReceiver", "Device disconnected: " + device.getName()); } } } ``` 3. 在需要监听蓝牙设备连接和断开事件的地方,注册广播接收器: ```java BluetoothReceiver receiver = new BluetoothReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED); filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED); registerReceiver(receiver, filter); ``` 注意:在不需要监听蓝牙设备连接和断开事件时,需要调用 unregisterReceiver 方法来取消注册广播接收器,以避免内存泄漏。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值