简体   繁体   English

订阅通知后未调用BLE GATT onCharacteristicChanged

[英]BLE GATT onCharacteristicChanged not called after subscribe to notification

After subscribe notification immediate write command doesn't work. 订阅通知后,立即写命令不起作用。 I have to restart server device and auto connect to existing ble instance to get result. 我必须重新启动服务器设备并自动连接到现有的ble实例才能获得结果。

Notification enable code: 通知启用代码:

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

    System.out.println("descriptor length----" + characteristic.getDescriptors().size());

    // This is specific to Heart Rate Measurement.
    if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
                UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));

        System.out.println("descriptor--" + Arrays.toString(descriptor.getValue()));
        System.out.println("characteristic value--" + Arrays.toString(descriptor.getCharacteristic().getValue()));
             descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
        return mBluetoothGatt.writeDescriptor(descriptor);
    }
    return false;
}

and here is my write command code: 这是我的写命令代码:

 public void writeCustomCharacteristic(String value) {


    this.value = "";
    this.command = value;
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    /*check if the service is available on the device*/
    BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString("65333333-a115-11e2-9e9a-0800200ca100"));
    if (mCustomService == null) {
        Log.w(TAG, "Custom BLE Service not found");
        return;
    }
    /*get the read characteristic from the service*/
    BluetoothGattCharacteristic mWriteCharacteristic = mCustomService.getCharacteristic(UUID.fromString("65333333-a115-11e2-9e9a-0800200ca101"));
    mWriteCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);

        mWriteCharacteristic.setValue(value.getBytes());
        if (!mBluetoothGatt.writeCharacteristic(mWriteCharacteristic)) {
            Log.w(TAG, "Failed to write characteristic");
        }

    System.out.println("BluetoothLeService.writeCustomCharacteristic------->" + value);

}

Before write any characteristic , delay for few sec . 在写入任何特性之前,请延迟几秒钟。 Bcz it takes time to execute. Bcz需要时间才能执行。

 new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                mBluetoothGatt.writeDescriptor(RTS_CCCD); //for descriptor
  //  or
    mBluetoothGatt.readCharacteristic(brspInfo); //for read
//or
    mBluetoothGatt.writeCharacteristic(brspInfo); //for write
                            }
                        }, 500);

Here you get more explaination 在这里你得到更多的解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM