以编程方式打开所有通知 [英] Programmatically turning on all notifications

查看:106
本文介绍了以编程方式打开所有通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android开发的新手,我不明白如何正确打开所有ble通知并获取所有通知.

I am new to Android development and I can't understand how to properly turn all ble notifications and get all of them.

我尝试遍历所有服务

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);
            }
        }

但是我没有收到任何通知. 拜托,帮帮我,我不明白我在做什么错..

but I don't get any notifications back. Please, help I do not understand what I am doing wrong..

编辑

现在,我使用此方法在发现服务后启用通知:

Right now I use this method to enable notifications after services are discovered:

public void setCharacteristicNotification(BluetoothGattCharacteristic特性,已启用布尔值){

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {

        // 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;

    }

问题在于,第一个特征通知得很好,但是我尝试启用的所有其他特征都在线上失败了

The problem is that first characteristic notifies well, but all others I am trying to enable fails on the line

mBluetoothGatt.writeDescriptor(descriptor);

mBluetoothGatt.writeDescriptor(descriptor);

不知道该怎么办...

Don't know what to do...

推荐答案

我的问题通过覆盖onDescriptorWrite方法得以解决.我在里面做了一个标记,如果是真的,我会继续通知所有剩余的特征.

My problem was resolved by overiding onDescriptorWrite method. I made a flag inside, and if it is true I continue to notify all left characteristics.

@Override public void onDescriptorWrite(BluetoothGatt gatt,BluetoothGattDescriptor描述符,int状态){ 字符串serviceUUID = descriptor.getCharacteristic().getService().getUuid().toString(); 字符串serviceName = GattAttributes.lookupUUID(descriptor.getCharacteristic().getService().getUuid(),serviceUUID);

@Override public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { String serviceUUID = descriptor.getCharacteristic().getService().getUuid().toString(); String 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--;
        }
    }

这篇关于以编程方式打开所有通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆