Android-BluetoothKit icon indicating copy to clipboard operation
Android-BluetoothKit copied to clipboard

打开notify成功回调和失败回调都不走

Open guozhiqiang123 opened this issue 7 years ago • 2 comments

查了一下原因,大概如下: 通过serviceId获取到对应的service,并从service中获取到对应的BluetoothGattCharacteristic信息。根据BluetoothGattCharacteristic获取到对应的BluetoothGattDescriptor,但是这里每次获取都是为空的,所以无法进行设置,这样的话Android是无法收到蓝牙发送的消息的。

希望博主能修复这个问题: https://www.2cto.com/kf/201802/721207.html

guozhiqiang123 avatar Jun 12 '18 05:06 guozhiqiang123

源码是有使用CLIENT_CHARACTERISTIC_CONFIG的,在BleConnectWorker类中setCharacteristicNotification方法,你可以看看

@Override
public boolean setCharacteristicNotification(UUID service, UUID character, boolean enable) {
    checkRuntime();

    BluetoothLog.v(String.format("setCharacteristicNotification for %s, service = %s, character = %s, enable = %b",
            getAddress(), service, character, enable));

    BluetoothGattCharacteristic characteristic = getCharacter(service, character);

    if (characteristic == null) {
        BluetoothLog.e(String.format("characteristic not exist!"));
        return false;
    }

    if (mBluetoothGatt == null) {
        BluetoothLog.e(String.format("ble gatt null"));
        return false;
    }

    if (!mBluetoothGatt.setCharacteristicNotification(characteristic, enable)) {
        BluetoothLog.e(String.format("setCharacteristicNotification failed"));
        return false;
    }

    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(Constants.CLIENT_CHARACTERISTIC_CONFIG);

    if (descriptor == null) {
        BluetoothLog.e(String.format("getDescriptor for notify null!"));
        return false;
    }

    byte[] value = (enable ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);

    if (!descriptor.setValue(value)) {
        BluetoothLog.e(String.format("setValue for notify descriptor failed!"));
        return false;
    }

    if (!mBluetoothGatt.writeDescriptor(descriptor)) {
        BluetoothLog.e(String.format("writeDescriptor for notify failed"));
        return false;
    }

    return true;
}

fuhaodev avatar Jun 12 '18 07:06 fuhaodev

有可能是设备硬件问题,硬件中没有默认的 CLIENT_CHARACTERISTIC_CONFIG,需要你自己去遍历查找,确定哪一个特征是你需要的

bbccoolly avatar Oct 19 '18 02:10 bbccoolly