android gatt阻塞,android – 更改蓝牙低能量gatt超时或刷新读取流以更快地检测断开连接事件...

这里的其他答案可能比这个更好,但这是我解决问题的方法.在使用之前一定要尝试

Emil’s的答案.

我所做的事情因为时间太慢而无法等待它,因为它总是在变化,检查rssi.如果有一段时间,让我们说3秒,其中值保持不变,它会断开与设备的连接.这绕过了15秒的超时并添加了我们自己的超时.

这将是检查信号强度所需的.这是几年前写的,所以有些事情可能需要改变.

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {

@Override

public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status){

//check for signal strength changes. It will stay the same if we

//are getting no updates

if(mLastRssi == rssi){

disconnectCounter++;

if(disconnectCounter> 140) {

//disconnect logic as if we are done with connection

//maybe start listening for device again to reconnect

disconnectCounter = 0;

}

}

else{

//we are connected. reset counter

disconnectCounter = 0;

}

//store last value as global for comparison

mLastRssi= rssi;

}

}

在循环的某个地方,打电话

mBluetoothGatt.readRemoteRssi()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值