安卓蓝牙设置接收订阅通知断开第二次连接不上或者自动断开status=8的解决办法。

26 篇文章 0 订阅
26 篇文章 0 订阅

最近在做蓝牙开发,发现订阅蓝牙通知后,第二次连接会出现连接不上的情况,

BluetoothGattCharacteristic notifycharacteristic = service.getCharacteristic(GetDataUtils.characterReadUUID);//GetDataUtils.characterReadUUID为我司的订阅通知的UUID,为了方便我起名为read,其实应该起名为notify
mBluetoothGatt.setCharacteristicNotification(notifycharacteristic, true);
BluetoothGattDescriptor descriptor descriptor = notifycharacteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);

以上是正常订阅通知的方式。但是会出现第二次连接不上的情况。这是为什么呢

我钻研了将近了一个月,其实需要在断开时把接受通知关闭,网上和安卓蓝牙官方DEMO都没有把关闭蓝牙通知写出来

descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);

把setValue的ENABLE改为DISABLE,

也就是

descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);

然后再writeDescriptor告诉底层我要关闭接受通知,第二次连接的时候就能完美连接上了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值