Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android蓝牙LE-BluetoothGatt-onNotify停止接收数据_Android_Bluetooth_Bluetooth Lowenergy_Android Bluetooth - Fatal编程技术网

Android蓝牙LE-BluetoothGatt-onNotify停止接收数据

Android蓝牙LE-BluetoothGatt-onNotify停止接收数据,android,bluetooth,bluetooth-lowenergy,android-bluetooth,Android,Bluetooth,Bluetooth Lowenergy,Android Bluetooth,我正在连接蓝牙LE外设作为中心设备。我正在向特征写入数据,并通过20字节的通知接收数据 订阅通知: private void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) { if (mBluetoothAdapter == null || mBluetoothGatt == null) { Log.w("BluetoothAdapter

我正在连接蓝牙LE外设作为中心设备。我正在向特征写入数据,并通过20字节的通知接收数据

订阅通知:

private void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w("BluetoothAdapter not initialized");
        return;
    }

    Log.d("Enabling Notifications");

    mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

    BluetoothGattDescriptor descriptor =
            characteristic.getDescriptor(UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG));

    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

    mBluetoothGatt.writeDescriptor(descriptor);
}
如果在写操作之间只需要接收少量的块,那么这种方法就可以很好地工作。蓝牙堆栈为每个区块发送通知:

D/BluetoothGatt﹕ onNotify() - Device=B0:EC:8F:00:07:AA UUID=06d1e5e7-79ad-4a71-8faa-373789f7d93c
但是,如果写入之间的块数大于10,堆栈将停止通知,其余数据将丢失!该设备肯定会发送更多数据,因为我们可以在iOS设备上接收数据

收到的通知数量因android设备而异。Galaxy S3(4.3)接收5个,nexus 5(4.4.4)和htc one(4.4.2)接收12个

BT堆栈在上次通知30秒后关闭连接

D/BluetoothGatt﹕ onClientConnectionState() - status=0 clientIf=5 device=B0:EC:8F:00:00:88
有人能重现这个问题吗

由于蓝牙LE堆栈是基于轮询的,我猜该堆栈出于某种原因停止轮询来自外围设备的数据。目标设备不支持指示

更新:Android L Bluetooth堆栈提供的其他信息:

06-27 12:20:02.982 18909-18946/? D/BtGatt.GattService﹕ onNotify() - address=B0:EC:8F:00:01:09, charUuid=06d1e5e7-79ad-4a71-8faa-373789f7d93c, length=20
06-27 12:20:07.666 18909-18984/?E/BTLD﹕ ###################################################################### 06-27 12:20:07.666 18909-18984/? E/BTLD﹕ # 06-27 12:20:07.666 18909-18984/? E/BTLD﹕ # 警告:BTU HCI(id=0)命令超时。操作码=0xfd55 06-27 12:20:07.666 18909-18984/? E/BTLD﹕ # 06-27 12:20:07.666 18909-18984/? E/BTLD﹕ ###################################################################### 06-27 12:20:07.670 18909-18984/? E/bt btm﹕ 无法解释IRK VSC cmpl回调 06-27 12:20:07.670 18909-18984/? W/bt hci﹕ HCI Cmd超时计数器1
06-27 12:20:34.315 18909-18984/? E/bt btm﹕ btm_sec_disconnected-清除挂起标志可能不是一个理想的解决方案,但我已经开始接受通知根本不起作用或停止工作,这是在Android上执行BLE的不幸现实。考虑到这一点,我认为最好是在正常通知被破坏时通过执行读取轮询来设置自己的“伪通知”备份机制。例如,在
处理程序中#postDelayed()
每秒循环一次。您甚至可以将其抽象为一个单独的类,该类存储上次读取的值,并且仅在上次读取的值不等于新值时通知您的UI。

最终对我们有效的是外围设备上的两个更改:

-为外围设备上的每个响应添加一个小延迟


-将从属延迟降低到0

这正是我们所看到的。经过多次尝试后,通知失败,然后设备在Nexus 5上大约15秒后自动断开连接。我们的BLE设备在iOS上工作得非常出色。值得注意的是,有了特定的代码,Galaxy S4将在出现这种情况时自动重新连接,这让我们相信BLE堆栈中存在一个问题,三星已经确定并开发了定制处理。同样值得注意的是,Android L预览版并没有解决这个问题。添加了来自Android L的附加日志。我们目前通过将外围设备的通知延迟60毫秒来解决这个问题。但这不是我所说的修复,因为它会大大降低速度。你有没有尝试过做一个简单的任务?我不知道你的意思,你能更具体一点吗?如果你是这个意思的话,我会在不同的线程中打每个BT电话。但问题发生在Android BT堆栈的某个地方。即使我从onCharacteristicChanged()中删除所有代码,行为也是一样的。Philipp,另一个需要考虑的问题是,在异步BtGattCallback接口下,BT堆栈是同步的。通过确保BT堆栈一次只处理一个操作(即任何将回调到BtGattCallback的操作),我提高了应用程序的整体稳定性。