Программно включить все уведомления - PullRequest
0 голосов
/ 02 марта 2019

Я новичок в разработке для Android и не могу понять, как правильно включить все уведомления и получить их все.

Я пытался перебрать все службы

for(BluetoothGattService service : gatt.getServices()){
            if( service.getUuid().equals(Step_UUID)) {
                BluetoothGattCharacteristic characteristicData = service.getCharacteristic(Step_UUID);
                for (BluetoothGattDescriptor descriptor : characteristicData.getDescriptors()) {
                    descriptor.setValue( BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
                    gatt.writeDescriptor(descriptor);
                }
                gatt.setCharacteristicNotification(characteristicData, true);
            }
        }

но я не получаю никаких уведомлений.Пожалуйста, помогите, я не понимаю, что я делаю неправильно ..

РЕДАКТИРОВАТЬ

Сейчас я использую этот метод для включения уведомлений после обнаружения служб:

public void setCharacteristicNotification (характеристика BluetoothGattCharacteristic, логическое значение включено) {

        // Setting default write type according to CDT 222486
        characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);

        String serviceUUID = characteristic.getService().getUuid().toString();
        String serviceName = GattAttributes.lookupUUID(characteristic.getService().getUuid(), serviceUUID);

        String characteristicUUID = characteristic.getUuid().toString();
        String characteristicName = GattAttributes.lookupUUID(characteristic.getUuid(), characteristicUUID);

        String descriptorUUID = GattAttributes.CLIENT_CHARACTERISTIC_CONFIG;
        String descriptorName = GattAttributes.lookupUUID(UUIDDatabase.UUID_CLIENT_CHARACTERISTIC_CONFIG, descriptorUUID);

        if (characteristic.getDescriptor(UUID.fromString(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG)) != null) {
            if (enabled == true) {
                BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
                descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                boolean aaa = mBluetoothGatt.writeDescriptor(descriptor);

                aaa = mBluetoothGatt.writeDescriptor(descriptor);
                aaa = false;
            } else {
                BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
                descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
                mBluetoothGatt.writeDescriptor(descriptor);

            }
        }

        boolean aaaa = mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
        aaaa = false;

    }

Проблема заключается в том, что первая характеристика хорошо уведомляет, но все остальные, которые я пытаюсь включить, завершаются неудачно на линии

mBluetoothGatt.writeDescriptor (дескриптор);

Не знаю, что делать ...

1 Ответ

0 голосов
/ 19 марта 2019

Моя проблема была решена путем переопределения метода onDescriptorWrite.Я сделал флаг внутри, и если это правда, я продолжаю уведомлять все оставленные характеристики.

@ Переопределить public void onDescriptorWrite (BluetoothGatt gatt, дескриптор BluetoothGattDescriptor, int status) {String serviceUUID = descriptor.getCharacteristic ().. GetService () getUuid () ToString ().Строка serviceName = GattAttributes.lookupUUID (descriptor.getCharacteristic (). GetService (). GetUuid (), serviceUUID);

        String characteristicUUID = descriptor.getCharacteristic().getUuid().toString();
        String characteristicName = GattAttributes.lookupUUID(descriptor.getCharacteristic().getUuid(), characteristicUUID);

        String descriptorUUID = descriptor.getUuid().toString();
        String descriptorName = GattAttributes.lookupUUID(descriptor.getUuid(), descriptorUUID);

        if (status == BluetoothGatt.GATT_SUCCESS) {
            written = true;
            numberLeft--;
        } else {
            written = true;
            numberLeft--;
        }
    }
...