ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android BluetoothGatt无法接收特征通知BluetoothGatt#writeDescriptor(desc)返回false

2019-10-28 06:25:02  阅读:863  来源: 互联网

标签:bluetooth bluetooth-lowenergy lg android-ble android


我正在开发需要与Bluetooth LE设备通信的应用程序.

这是我用来设置CharacteristicNotification的代码

public boolean setCharacteristicNotification(
    BluetoothGattCharacteristic characteristic, boolean enable) {

if(mBluetoothAdapter == null || mBluetoothGatt == null) {
    Log.w(TAG, "BluetoothAdapter not initialized");
    return false;
}

Log.v(TAG, "setCharacteristicNotification(): uuid=" + characteristic.getUuid() + " enabled=" + enable);

boolean notifications = mBluetoothGatt.setCharacteristicNotification(characteristic, enable);

if(notifications) {
    Log.v(TAG, "setCharacteristicNotification(): Notifications are enabled");
}
else {
    Log.w(TAG, "setCharacteristicNotification(): Notifications are not enabled for characteristic " + characteristic);
}

BluetoothGattDescriptor desc = characteristic.getDescriptor(
        UUID.fromString(FBGattAttributes.CHARACTERISTIC_CLIENT_CONFIG));

desc.setValue(enable ?
                BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE :
                new byte[]{0x00, 0x00}
);

boolean ok = mBluetoothGatt.writeDescriptor(desc);

if(ok){
    Log.v(TAG, "wrote descriptor value for notification: ok=" + ok);
}else{
    Log.w(TAG, "writeDescriptor failed: we will not get notifications=" + ok);
}


return ok;
}

在此代码中,“ mBluetoothGatt.writeDescriptor(desc);”有时返回false,这就是我无法从BluetoothGatt收到任何通知的原因.我不确定如何解决此问题.

此问题仅发生在具有OS 5.02的LG G2之前,该操作系统版本为4.4.问题不是那么频繁,但是在更新之后,除第一次以外,我每次都得到“假”消息.
如果我们尝试在连接后第一次设置通知,那么它将起作用,并且一旦我断开连接并尝试连接,它将始终返回False.
我需要杀死并重新启动该应用程序才能使其再次运行.
有谁知道为什么这不起作用?提前致谢

解决方法:

在Lollypop中,对BluetoothGatt.java进行了更新,以包含“设备繁忙”块,这些块可防止应用程序一次请求多个异步操作.请参阅BluetoothGatt.java:1029以了解从writeDescriptor返回的内容.

我不得不设置一个命令队列来解决这个问题.基本上,我的逻辑是将所有失败的异步调用加入队列并在我的BluetoothGattCallback中执行重试.

标签:bluetooth,bluetooth-lowenergy,lg,android-ble,android
来源: https://codeday.me/bug/20191028/1950369.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有