Android BLE中心设备的onCharacteristicChanged()方法没有回调

描述:当设备为 Indication 模式时,设备的值有变化时会主动返回给App,App在 onCharacteristicChanged() 方法中能收到返回的值。

Indication: 从机会先向主机发送一条通知,主机接收到通知后去读取从机数据
Notification:从机直接发送给主机数据

问题:在App中通过如下代码注册监听,注册成功后就能接收到设备主动反馈的值了。然而以下代码执行后依旧收不到反馈。但是对设备的读写都是可行的,并且iOS端可以接收到通知。

bluetoothGatt.setCharacteristicNotification(characteristic, true)

解决: 当上面的方法执行返回true后,还要执行如下的代码才能注册成功。

for(BluetoothGattDescriptor dp: characteristic.getDescriptors()){
    if (dp != null) {
        if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
            dp.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        } else if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) {
            dp.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
        }
        gatt.writeDescriptor(dp);
    }
}
完整的代码如下:
public boolean enableNotification(BluetoothGatt gatt, UUID serviceUUID, UUID characteristicUUID) {
    boolean success = false;
    BluetoothGattService service = gatt.getService(serviceUUID);
    if (service != null) {
        BluetoothGattCharacteristic characteristic = findNotifyCharacteristic(service, characteristicUUID);
        if (characteristic != null) {
            success = gatt.setCharacteristicNotification(characteristic, true);
            if (success) {
                // 来源:http://stackoverflow.com/questions/38045294/oncharacteristicchanged-not-called-with-ble
                for(BluetoothGattDescriptor dp: characteristic.getDescriptors()){
                    if (dp != null) {
                        if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
                            dp.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                        } else if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) {
                            dp.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
                        }
                        gatt.writeDescriptor(dp);
                    }
                }
            }
        }
    }
    return success;
}

private BluetoothGattCharacteristic findNotifyCharacteristic(BluetoothGattService service, UUID characteristicUUID) {
    BluetoothGattCharacteristic characteristic = null;
    List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
    for (BluetoothGattCharacteristic c : characteristics) {
        if ((c.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0
                && characteristicUUID.equals(c.getUuid())) {
            characteristic = c;
            break;
        }
    }
    if (characteristic != null)
        return characteristic;
    for (BluetoothGattCharacteristic c : characteristics) {
        if ((c.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0
                && characteristicUUID.equals(c.getUuid())) {
            characteristic = c;
            break;
        }
    }
    return characteristic;
}
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
Android BLE(低功耗蓝牙)通信中,接收分包数据主要依赖于特性操作和回调函数的处理。 首先,我们需要注册特性操作的回调函数,以便能够正确地接收分包数据。在回调函数中,你可以通过onCharacteristicRead()方法处理读取特性的分包数据,或通过onCharacteristicChanged()方法处理接收特性的分包通知数据。 接收分包数据的步骤如下: 1. 连接到BLE设备并发现服务和特性。 2. 找到需要读取或接收的特性对象。 3. 使用BluetoothGatt的readCharacteristic()方法读取特性的分包数据,或使用BluetoothGattsetCharacteristicNotification()方法启用该特性的通知功能。 4. 处理回调函数中的分包数据。 在处理回调函数时,可以通过分包数据的属性(Properties)来判断是否为分包数据。例如,如果一个特性的Properties包含BluetoothGattCharacteristic.PROPERTY_READ,那么可以使用readCharacteristic()方法逐个读取分包数据。如果一个特性的Properties包含BluetoothGattCharacteristic.PROPERTY_NOTIFY,那么可以使用onCharacteristicChanged()方法接收分包通知数据。 在处理分包数据时,可能需要将多个分包数据拼接在一起形成完整的信息。为了确保准确性,可以使用一个缓冲区来存储和管理拼接后的数据。 需要注意的是,对于较大的分包数据,一次性读取可能会导致BLE通信失败。因此,在读取分包数据时,可能需要将数据分成多个片段来处理,直到接收到所有分包数据为止。 总之,在Android BLE接收分包数据的过程中,我们需要正确处理回调函数,使用特性操作来读取或接收分包数据,并在处理数据时进行数据拼接和分片操作,以确保数据的完整性和准确性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值