简体   繁体   中英

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.

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.

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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