首页 > 其他分享 >Need BLUETOOTH PRIVILEGED permission以及requestMtu导致蓝牙断连问题

Need BLUETOOTH PRIVILEGED permission以及requestMtu导致蓝牙断连问题

时间:2024-10-12 10:11:42浏览次数:1  
标签:status permission gatt requestMtu int BluetoothGatt Need

在部分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 }

 

标签:status,permission,gatt,requestMtu,int,BluetoothGatt,Need
From: https://www.cnblogs.com/developer-wang/p/18459969

相关文章

  • HarmonyOS开发——编译报错“The reason and usedScene attributes are mandatory for
    问题现象:DevEcoStudio编译失败,提示“ThereasonandusedSceneattributesaremandatoryforuser_grantpermissions”。问题原因:从DevEcoStudioNEXTDeveloperPreview2版本开始新增规则:APP包中,所有entry和featurehap的module下的requestPermissions权限清单必须指定(......
  • why do we need 'select…for share' instead of a simple 'select'
    (Fromchatgpt)AsimpleSELECTqueryinPostgreSQLoperatesundertheMVCC(Multi-VersionConcurrencyControl)model,whichallowsittoreaddatawithoutlockingtherows.Thismeansitcanseeasnapshotofthedataatthestartofthetransaction,rega......
  • Pyside6 键盘输入控件---单行文本框QLineEdit
    1.QLineEdit的介绍(官翻)单行文本编辑器允许用户输入和编辑单行纯文本,具有一些实用的编辑功能,包括撤销和重做,剪切和粘贴,以及拖放。通过修改行编辑器的echomde(),它还可以用作只写字段,用于输入密码等信息。QTextEdit是一个与之相关的类,它允许多行、富文本编辑。使用maxLength......
  • Long-Sequence Recommendation Models Need Decoupled Embeddings
    目录概DecoupledAttentionandRepresentationEmbeddings(DARE)modelFengN.,PangJ.,WuJ.,ChenB.,WangX.,LiQ.,HuX.,JiangJ.andLongM.Long-sequencerecommendationmodelsneeddecoupledembeddings.2024.概通过embedding选择短序列,最好从一个......
  • PbootCMS附件上传失败报错UNKNOW: Code: 8192; Desc: stripos(): Non-string needles
    当遇到PBootCMS附件上传失败,并报错 UNKNOW:Code:8192;Desc:stripos():Non-stringneedleswillbeinterpretedasstringsinthefuture. 时,这通常是因为PHP的版本更新导致某些函数的行为有所改变。在这个情况下,stripos() 函数在处理非字符串参数时会发出警告,因为它......
  • PermissionHandler包的用法
    文章目录概念介绍使用方法示例代码经验分享我们在上一章回中介绍了局部动态列表相关的内容,本章回中将介绍权限管理包permission_hanadler.闲话休提,让我们一起TalkFlutter吧。概念介绍权限是使用某种功能的授权,比如使用手机上的相机就是获取相机相关的权限......
  • 易优CMS后台上传图片报错mkdir(): Permission denied 报错-eyoucms
    当你在易优CMS后台上传图片时遇到“mkdir():Permissiondenied”的错误提示时,通常是因为文件系统的权限设置不当。以下是一些具体的解决步骤:步骤1:检查目录权限确定问题目录确认出现问题的目录是 /core/library/think/cache/driver/File.php。实际上,问题可能出现在其他目......
  • 易优CMS网站SQLSTATE[HY000]: General error: 1615 Prepared statement needs to be r
    当你遇到“SQLSTATE[HY000]:Generalerror:1615Preparedstatementneedstobere-prepared”的错误时,这通常是因为数据库连接配置中的某些参数不正确。具体来说,这可能是因为数据库驱动程序在某些环境下需要特定的配置才能正常工作。解决方法找到数据库配置文件打开数据......
  • 出现这种报错怎么办?SQLSTATE[HY000]: General error: 1615 Prepared statement needs
    如果你遇到由于数据库配置问题导致前后台无法打开的情况,可以通过修改数据库配置文件来解决。具体步骤如下:解决步骤第一步:打开数据库配置文件使用Notepad++打开配置文件:使用Notepad++或其他专业文本编辑器打开数据库配置文件 application/database.php。例如,假设你的......
  • SQLSTATE[HY000]: General error: 1615 Prepared statement needs to be re-prepared
    当遇到由于数据库配置问题导致前后台无法打开的情况时,可以通过修改数据库配置文件来解决问题。具体步骤如下:1.准备工作备份数据库配置文件:在修改前,建议先备份 application/database.php 文件。sh cpapplication/database.phpapplication/database.php.bak准......