2 * Copyright (c) 2010-2020 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;
20 * The {@link BluetoothDescriptor} class defines the Bluetooth descriptor.
22 * Descriptors are defined attributes that describe a characteristic value.
24 * https://www.bluetooth.com/specifications/gatt/descriptors
26 * @author Chris Jackson - Initial contribution
27 * @author Kai Kreuzer - added constructor and fixed setValue method
29 public class BluetoothDescriptor {
31 protected final BluetoothCharacteristic characteristic;
32 protected final UUID uuid;
33 protected byte[] value;
36 * The main constructor
38 * @param characteristic the characteristic that this class describes
39 * @param uuid the uuid of the descriptor
41 public BluetoothDescriptor(BluetoothCharacteristic characteristic, UUID uuid) {
42 this.characteristic = characteristic;
47 * Returns the characteristic this descriptor belongs to.
51 BluetoothCharacteristic getCharacteristic() {
52 return characteristic;
56 * Returns the permissions for this descriptor.
58 * @return the permissions
60 public int getPermissions() {
65 * Returns the UUID of this descriptor.
69 public UUID getUuid() {
74 * Returns the stored value for this descriptor. It doesn't read remote data.
76 * @return the value of the descriptor
78 public byte[] getValue() {
83 * Sets the stored value for this descriptor. It doesn't update remote data.
85 * @param value the value for this descriptor instance
87 public void setValue(byte[] value) {
91 public GattDescriptor getDescriptor() {
92 return GattDescriptor.getDescriptor(uuid);
95 public enum GattDescriptor {
97 CHARACTERISTIC_EXTENDED_PROPERTIES(0x2900),
98 CHARACTERISTIC_USER_DESCRIPTION(0x2901),
99 CLIENT_CHARACTERISTIC_CONFIGURATION(0x2902),
100 SERVER_CHARACTERISTIC_CONFIGURATION(0x2903),
101 CHARACTERISTIC_PRESENTATION_FORMAT(0x2904),
102 CHARACTERISTIC_AGGREGATE_FORMAT(0x2905),
104 EXTERNAL_REPORT_REFERENCE(0x2907),
105 REPORT_REFERENCE(0x2908),
106 NUMBER_OF_DIGITALS(0x2909),
107 TRIGGER_SETTING(0x290A);
109 private static Map<UUID, GattDescriptor> uuidToServiceMapping;
111 private final UUID uuid;
113 private GattDescriptor(long key) {
114 this.uuid = new UUID((key << 32) | 0x1000, BluetoothBindingConstants.BLUETOOTH_BASE_UUID);
117 private static void initMapping() {
118 uuidToServiceMapping = new HashMap<>();
119 for (GattDescriptor s : values()) {
120 uuidToServiceMapping.put(s.uuid, s);
124 public static GattDescriptor getDescriptor(UUID uuid) {
125 if (uuidToServiceMapping == null) {
128 return uuidToServiceMapping.get(uuid);
134 public UUID getUUID() {