Android 遇坑【8】- Android10 蓝牙扫描失败

前言

最近遇到一个吊诡的问题,开发的APP在 华为荣耀V20 的测试中发现无法扫描到蓝牙设备。而手机系统蓝牙可以扫描得到。打断点调试发现其扫描回调未执行…多次尝试未果。
开始以为是该机型特有的问题,随后偶然看到其为Android10,这也是目前可供测试的唯一一部手机,考虑到安卓日益细分、严格的权限控制。我打开了 Android 10 隐私变更 ,终于解脱了。

在这里插入图片描述

原因

在这里插入图片描述
涉及到蓝牙功能如下,可以发现常用的扫描都需要…
在这里插入图片描述

解决方法

修改目标版本

解决不了问题,就解决提出问题的人。

既然是版本升级带来的权限细分,那么我们直接修改build.gradle,将targetSdkVersion改为27,问题解决。

android {
   
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
   
        applicationId "com.example.felicitysolar_debug"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 2
        versionName "3.6"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
   
        release {
   
            minifyEnabled false
            proguardFi
  • 13
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
以下是 Android 测试手机与小米手环 BLE 蓝牙扫描、连接、断开的代码: 1. 首先,在 AndroidManifest.xml 中添加以下权限: ```xml <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> ``` 2. 在代码中声明蓝牙适配器和扫描回调: ```java private BluetoothAdapter mBluetoothAdapter; private BluetoothLeScanner mBluetoothLeScanner; private ScanCallback mScanCallback; ``` 3. 初始化蓝牙适配器和扫描回调: ```java // 初始化蓝牙适配器 final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = bluetoothManager.getAdapter(); // 初始化扫描回调 mScanCallback = new ScanCallback() { @Override public void onScanResult(int callbackType, ScanResult result) { super.onScanResult(callbackType, result); BluetoothDevice device = result.getDevice(); // 连接设备 connect(device); } @Override public void onScanFailed(int errorCode) { super.onScanFailed(errorCode); // 扫描失败 } }; ``` 4. 开始扫描: ```java if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()) { mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner(); mBluetoothLeScanner.startScan(mScanCallback); } ``` 5. 连接设备: ```java private BluetoothGatt mBluetoothGatt; private void connect(BluetoothDevice device) { if (mBluetoothGatt != null) { mBluetoothGatt.close(); } mBluetoothGatt = device.connectGatt(this, false, mGattCallback); } private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { super.onConnectionStateChange(gatt, status, newState); if (newState == BluetoothProfile.STATE_CONNECTED) { // 连接成功 } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { // 断开连接 } } }; ``` 6. 断开连接: ```java if (mBluetoothGatt != null) { mBluetoothGatt.disconnect(); mBluetoothGatt.close(); mBluetoothGatt = null; } ``` 以上是 Android 测试手机与小米手环 BLE 蓝牙扫描、连接、断开的代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值