【问题标题】:Programmatically turning on all notifications以编程方式打开所有通知
【发布时间】:2019-03-02 09:23:03
【问题描述】:

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

我尝试遍历所有服务

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(descriptor);

不知道怎么办……

【问题讨论】:

  • 在您的代码中启用了指示,但您说您想要通知?此外,您一次只能有一个未完成的 GATT 操作(请参阅您的 writeDescriptor 调用)。您需要等待 onDescriptorWrite 才能继续。
  • 您一次只能有一个未完成的 GATT 操作(请参阅您的 writeDescriptor 调用)。您需要等待 onDescriptorWrite 才能继续。如果你不这样做,writeDescriptor 将返回 false。
  • Emil 感谢您提供钥匙!

标签: java android bluetooth notifications bluetooth-lowenergy


【解决方案1】:

通过重写 onDescriptorWrite 方法解决了我的问题。我在里面做了一个flag,如果是真的我会继续通知所有剩下的特征。

@覆盖 公共无效 onDescriptorWrite(BluetoothGatt gatt,BluetoothGattDescriptor 描述符,int 状态){ String serviceUUID = 描述符.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--;
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    • 2013-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多