动态注册广播接收器不起作用-BluetoothDevice.ACTION_FOUND

最近公司需要开发一款蓝牙app,从来没搞过蓝牙所以想写个demo试试手,于是按照文档就开始了,一口气把蓝牙的状态、扫描、扫描完成等广播一口气全都注册了

        //蓝牙状态监听
        IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
        //扫描蓝牙设备
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        //搜索完成
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        BluetoothReceiver  bluetoothReceiver = new BluetoothReceiver() ;
        registerReceiver(bluetoothReceiver,filter);
        //蓝牙适配器
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (bluetoothAdapter == null) {
            showToast("设备不支持蓝牙");
        }
        if (!bluetoothAdapter.isEnabled()) {
            //未打开蓝牙
            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(intent, REQUEST_ENBLE_BT);
        }
        //开启蓝牙扫描
        bluetoothAdapter.startDiscovery();

自定义的广播接收器BluetoothReceiver如下:

package com.test.bluetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.blankj.utilcode.util.LogUtils;

/**
 * @CreateDate: 2022/5/5
 * @Author:lp
 * @Description:
 */
public class BluetoothReceiver extends BroadcastReceiver {
    public int DEFAULT_VALUE_BLUETOOTH = 1000;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        //蓝牙状态改变
        if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
            int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, DEFAULT_VALUE_BLUETOOTH);
            switch (state) {
                case BluetoothAdapter.STATE_ON:
                    LogUtils.i("onReceive: open");
                    break;
                case BluetoothAdapter.STATE_OFF:
                    LogUtils.i("onReceive: off");
                    break;
                case BluetoothAdapter.STATE_TURNING_ON:
                    LogUtils.i("onReceive: 正在打开");
                    break;
                case BluetoothAdapter.STATE_TURNING_OFF:
                    LogUtils.i("onReceive: 正在关闭");
                    break;
            }
        }
        //扫描其他设备
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // 从intent对象中获取蓝牙设备的信息
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            Log.d("设备",device.getName() + "\t" + device.getAddress());

        }
        //蓝牙配对
        if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(intent.getAction())) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            switch (device.getBondState()) {
                case BluetoothDevice.BOND_BONDED:
                    LogUtils.i("onReceive: 配对完成");
                    break;
                case BluetoothDevice.BOND_BONDING:
                    LogUtils.i("onReceive: 正在配对");
                    break;
                case BluetoothDevice.BOND_NONE:
                    LogUtils.i("onReceive: 取消配对");
                    break;
            }
        }
        if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(intent.getAction())) {
            LogUtils.i("扫描完成");
        }
    }
}

然后发现我注册的BluetoothDevice.ACTION_FOUND在广播接收器没有被触发,onReceive()方法没有被调用,查阅文档蓝牙6.0之后开启蓝牙搜索需要增加动态权限:android.permission.ACCESS_FINE_LOCATION

于是在AndroidManifest中添加权限

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

之前写的 bluetoothAdapter.startDiscovery();放到权限允许的方法中去做:(这里偷个懒,因为项目依赖了工具包com.blankj:utilcodex,这里就直接调用工具类的权限请求方法)

    @SuppressLint("WrongConstant")
    private void reqPermission(){
        PermissionUtils.permissionGroup(Manifest.permission.ACCESS_FINE_LOCATION).callback(new PermissionUtils.FullCallback() {
            @Override
            public void onGranted(@NonNull @NotNull List<String> granted) {
                //权限允许
                bluetoothAdapter.startDiscovery();
            }

            @Override
            public void onDenied(@NonNull @NotNull List<String> deniedForever, @NonNull @NotNull List<String> denied) {
                //权限拒绝
                showToast("权限拒绝");

            }
        }).request();
    }

重新运行会弹出权限授权提示:

允许之后运行发现可以拿到周边蓝牙设备:

 

 

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值