2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.bluetooth;
15 import java.util.HashMap;
17 import java.util.UUID;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
23 * The {@link BluetoothDescriptor} class defines the Bluetooth descriptor.
25 * Descriptors are defined attributes that describe a characteristic value.
27 * https://www.bluetooth.com/specifications/gatt/descriptors
29 * @author Chris Jackson - Initial contribution
30 * @author Kai Kreuzer - added constructor and fixed setValue method
33 public class BluetoothDescriptor {
35 protected final BluetoothCharacteristic characteristic;
36 protected final UUID uuid;
38 protected final int handle;
41 * The main constructor
43 * @param characteristic the characteristic that this class describes
44 * @param uuid the uuid of the descriptor
46 public BluetoothDescriptor(BluetoothCharacteristic characteristic, UUID uuid, int handle) {
47 this.characteristic = characteristic;
53 * Returns the characteristic this descriptor belongs to.
57 BluetoothCharacteristic getCharacteristic() {
58 return characteristic;
62 * Returns the permissions for this descriptor.
64 * @return the permissions
66 public int getPermissions() {
71 * Returns the UUID of this descriptor.
75 public UUID getUuid() {
80 * Returns the handle for this descriptor
82 * @return the handle for the descriptor
84 public int getHandle() {
88 public @Nullable GattDescriptor getDescriptor() {
89 return GattDescriptor.getDescriptor(uuid);
92 public enum GattDescriptor {
94 CHARACTERISTIC_EXTENDED_PROPERTIES(0x2900),
95 CHARACTERISTIC_USER_DESCRIPTION(0x2901),
96 CLIENT_CHARACTERISTIC_CONFIGURATION(0x2902),
97 SERVER_CHARACTERISTIC_CONFIGURATION(0x2903),
98 CHARACTERISTIC_PRESENTATION_FORMAT(0x2904),
99 CHARACTERISTIC_AGGREGATE_FORMAT(0x2905),
101 EXTERNAL_REPORT_REFERENCE(0x2907),
102 REPORT_REFERENCE(0x2908),
103 NUMBER_OF_DIGITALS(0x2909),
104 TRIGGER_SETTING(0x290A);
106 private static @Nullable Map<UUID, GattDescriptor> uuidToServiceMapping;
108 private final UUID uuid;
110 private GattDescriptor(long key) {
111 this.uuid = BluetoothBindingConstants.createBluetoothUUID(key);
114 public static @Nullable GattDescriptor getDescriptor(UUID uuid) {
115 Map<UUID, GattDescriptor> localServiceMapping = uuidToServiceMapping;
116 if (localServiceMapping == null) {
117 localServiceMapping = new HashMap<>();
118 for (GattDescriptor s : values()) {
119 localServiceMapping.put(s.uuid, s);
121 uuidToServiceMapping = localServiceMapping;
123 return localServiceMapping.get(uuid);
129 public UUID getUUID() {