android ble 扫描慢,Android BLE重新连接非常慢

背景:

我有一个BLE外设有两种模式:“应用程序”和“引导程序”.在这两种模式下,设备都使用相同的MAC地址进行通告.

要从一种模式切换到另一种模式,BLE外设必须自行重启.在这样做时,它必须断开任何活动的BLE连接.

BLE外设仅在Bootloader模式下保持约5秒钟.如果没有人在该窗口内连接它,它将切换到应用程序模式.

问题:

Android需要很长时间才能重新连接到BLE设备,足够长,以至于我错过了5秒窗口.原始代码有几层到BluetoothGATT和BluetoothAdapter层,但调用顺序归结为:

BluetoothGattCharacteristic c = mCharacteristics.get(POWER_STATE_UUID);

c.setValue(SHUTDOWN_VALUE);

mBluetoothGatt.writeCharacteristic(c);

// Signalled by BluetoothGattCallback.onCharacteristicWrite

bleWriteCondition.await();

mBluetoothGatt.disconnect();

// Wait for the underlying layer to confirm we're disconnected

while( mConnectionState != BluetoothProfile.STATE_DISCONNECTED ) {

// Signalled by BluetoothGattCallback.onConnectionStateChange

bleStateCondition.await();

}

mBluetoothGatt.connect();

while (mConnectionState != BluetoothProfile.STATE_CONNECTED) {

// Signalled by BluetoothGattCallback.onConnectionStateChange

bleStateCondition.await();

if (bleStateCondition.stat != 0) {

break;

}

}

我完全是错误的做法吗?我试过在BluetoothGatt实例上调用close(),然后使用BluetoothDevice.connectGatt生成一个新的实例,但是我得到了同样非常慢的行为.

我正在测试三星Galaxy S4,API级别21.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值