在部分Android手机上,当连接上GATTService后直接requestMtu有可能会造成蓝牙连接中断,随后继续重新连接会报错Need BLUETOOTH PRIVILEGED permission

1 //扫描成功后连接gatt
2 BluetoothDevice mRemoteDevice = mBluetoothAdapter.getRemoteDevice(result.getDevice().getAddress());
3 BluetoothGatt mBluetoothGatt = mRemoteDevice.connectGatt(getContext(), false, bluetoothGattCallback);

在 BluetoothGattCallback 监听的 onConnectionStateChange(BluetoothGatt gatt, int status, int newState)方法中直接requestMtu(512) 会直接导致蓝牙断开连接

 1 private int setMTUInt = 512;
2
3
4 @Override
5 public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
6 super.onConnectionStateChange(gatt, status, newState);
7
8 if (newState == BluetoothProfile.STATE_CONNECTED) { //// 连接成功
9 //虽然这里的boolean会为true但是实际上会失败,并且导致蓝牙断开链接
10 boolean requestMtu = gatt.requestMtu(setMTUInt);
11 Log.e(TAG,"设置 requestMtu:"+requestMtu);
12
13 } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { // 连接断开
14 //清除缓存,防止Need BLUETOOTH PRIVILEGED permission
15 refreshDeviceCache();
16 //将MTU的值减少,值可以随意设置
17 setMTUInt -= 100;
18 // 关闭GATT客户端
19 mBluetoothGatt.close();
20 }
21 }

在监听的onMtuChanged(BluetoothGatt gatt, int mtu, int status)方法中,确认mtu设置成功后,再去请求特征值

1 @Override
2 public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
3 super.onMtuChanged(gatt, mtu, status);
4
5 // 开始查找GATT服务器提供的服务
6 gatt.discoverServices();
7
8 }

随后在监听的onServicesDiscovered(BluetoothGatt gatt, int status)方法中设置特征

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
//设置特征值
requestCharacteristic();
}
}

清除缓存的方法,gatt本身有提供清除缓存方法,但是不给外部直接调用,所以使用反射的方式进行调用,建议在每次开启扫描设备时也清除一下缓存,防止链接过其他设备后直接链接新设备报错 java.lang.SecurityException: Need BLUETOOTH PRIVILEGED permission: Neither user 10208 nor current process has android.permission.BLUETOOTH_PRIVILEGED.

 1 public boolean refreshDeviceCache() {
2 if (mBluetoothGatt != null) {
3 Log.e(TAG,"清除缓存 refreshDeviceCache");
4 try {
5 BluetoothGatt localBluetoothGatt = mBluetoothGatt;
6 Method localMethod = localBluetoothGatt.getClass().getMethod(
7 "refresh", new Class[0]);
8 if (localMethod != null) {
9 boolean bool = ((Boolean) localMethod.invoke(
10 localBluetoothGatt, new Object[0])).booleanValue();
11 return bool;
12 }
13 } catch (Exception localException) {
14 Log.i(TAG, "An exception occured while refreshing device");
15 }
16 }
17 return false;
18 }

Need BLUETOOTH PRIVILEGED permission以及requestMtu导致蓝牙断连问题的更多相关文章

  1. 【转】Android bluetooth介绍(三): 蓝牙扫描(scan)设备分析

    原文网址:http://blog.csdn.net/xubin341719/article/details/38584469 关键词:蓝牙blueZ  A2DP.SINK.sink_connect.s ...

  2. Android bluetooth介绍(三): 蓝牙扫描(scan)设备分析

    关键词:蓝牙blueZ  A2DP.SINK.sink_connect.sink_disconnect.sink_suspend.sink_resume.sink_is_connected.sink_ ...

  3. cordova之File Transfer (Permission denied) 权限导致下载失败 - 简书

    原文:cordova之File Transfer (Permission denied) 权限导致下载失败 - 简书 在文件上传时,由于权限问题,会报错(Permission denied),安卓6. ...

  4. Privileged Permission开机授权时序图 SourceCode android-10.0.0_r36

    Privileged Permission开机授权时序图 | SourceCode:android-10.0.0_r36 | Author:秋城 | v1.1SystemServerSystemSer ...

  5. 浅谈Bluetooth蓝牙开发

    前言:项目用到蓝牙开发,具体蓝牙获取硬件传感器中的数据. 因为没有蓝牙开发的相关经验,决定先了解一些蓝牙开发的知识,再去看之前同事写的蓝牙相关代码. ------------------------- ...

  6. android -- 蓝牙 bluetooth (三)搜索蓝牙

    接上篇打开蓝牙继续,来一起看下蓝牙搜索的流程,触发蓝牙搜索的条件形式上有两种,一是在蓝牙设置界面开启蓝牙会直接开始搜索,另一个是先打开蓝牙开关在进入蓝牙设置界面也会触发搜索,也可能还有其它触发方式,但 ...

  7. android -- 蓝牙 bluetooth (二) 打开蓝牙

    4.2的蓝牙打开流程这一部分还是有些变化的,从界面上看蓝牙开关就是设置settings里那个switch开关,widget开关当然也可以,起点不同而已,后续的流程是一样的.先来看systemServe ...

  8. iOS - Bluetooth 蓝牙

    1.蓝牙介绍 具体讲解见 蓝牙 技术信息 蓝牙协议栈 2.iBeacon 具体讲解见 Beacon iBeacon 是苹果公司 2013 年 9 月发布的移动设备用 OS(iOS7)上配备的新功能.其 ...

  9. ZT android -- 蓝牙 bluetooth (三)搜索蓝牙

    android -- 蓝牙 bluetooth (三)搜索蓝牙 分类: Android的原生应用分析 2013-05-31 22:03 2192人阅读 评论(8) 收藏 举报 bluetooth蓝牙s ...

  10. ZT android -- 蓝牙 bluetooth (二) 打开蓝牙

    android -- 蓝牙 bluetooth (二) 打开蓝牙 分类: Android的原生应用分析 2013-05-23 23:57 4773人阅读 评论(20) 收藏 举报 androidblu ...

随机推荐

  1. 【CentOS】rpm包安装Jdk

    1.系统环境检查 前提情要:[如果是使用虚拟机的Linux系统,强烈建议先打个快照备份一下,以免操作失误无法重来] 首先查看系统是否存在java环境 java -version 因为点选了环境工具,这 ...

  2. 内存交换空间--Swap Space

    转载请注明出处: 一.概述 内存交换空间(Swap Space)是计算机内存的一种补充,位于硬盘驱动器上.当物理内存(RAM)不足时,系统会将不活跃的页面(或称为内存页)移到交换空间中,以释放物理内存 ...

  3. Pintia 天梯地图 dijkstra进阶

    7-14 天梯地图 - SMU 2024 spring 天梯赛3(补题) (pintia.cn) dijkstra进阶做法,包含路径记录,以及按权重统计路径条件等; 不过最开始我一直将优先队列开的最大 ...

  4. apisix-dashboard上添加自定义插件

    参考:https://overstarry.vip/posts/apisix如何添加自定义插件/ 首先,我们需要向自定义的插件user-remote-auth添加到apisix中,对这块不清楚的同学, ...

  5. NCP1207A笔记

    uc3844d8 NCP1207A实现一个标准的电流模式结构,关断时间由峰值电流设置决定:铁芯复位检测则触发开启事件. 变压器铁芯检测:无论什么操作都会保证临界操作.因此,几乎没有一次开关接通损耗和二 ...

  6. WPF自定义控件之ItemsControl鱼眼效果

    原理 先获取鼠标在控件中的坐标,在获取其每一项相对于ItemsControl的坐标,然后计算每一项离当前鼠标的距离,在根据这个距离,对其每一项进行适当的缩放 实现 创建一个类,命名为FishEyeIt ...

  7. 能否自定义一个String类使用

    先说下结论,可以自定义包名不为java.lang的String类,区别包名是可以正常使用的. 包名不为java.lang package com.seven.jvm; public final cla ...

  8. springjdbc处理nvarchar

    当我们使用spring-jdbc来做持久化时(注意不是spring-data-jbc),有时候一些特殊字符存入数据库时会用到nvarchar.nvarchar2这种类型(比如存放化学式,如CO₂等), ...

  9. CMake构建学习笔记11-minizip库的构建

    准确来说,minizip其实是zlib提供的辅助工具,位于zlib库的contrib文件夹内.minizip提供了更为高级一点的接口,能直接操作文件进行压缩.不过,有点麻烦的是这个工具并没有提供CMa ...

  10. 对GEE下载时分块影像进行镶嵌(GDAL)

    前言 GDAL自带的镶嵌功能比较单一,只能将图像简单拼接到一起,不能实现直方图匀色以及羽化等功能,但是对GEE导出的分块影像进行镶嵌较为适合. 优点: 使用代码,镶嵌多个影像较为简便 GDAL较为稳定 ...