blob: 1cd68787ce0467e788b638e9b99c361855c94c8d [file] [log] [blame]
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.bluetooth;
18
19import java.util.UUID;
20
21/**
Matthew Xieddf7e472013-03-01 18:41:02 -080022 * Represents a Bluetooth GATT Descriptor
Matthew Xie33ec9842013-04-03 00:29:27 -070023 *
24 * <p> GATT Descriptors contain additional information and attributes of a GATT
25 * characteristic, {@link BluetoothGattCharacteristic}. They can be used to describe
26 * the characteristic's features or to control certain behaviours of the characteristic.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080027 */
28public class BluetoothGattDescriptor {
29
30 /**
31 * Value used to enable notification for a client configuration descriptor
32 */
33 public static final byte[] ENABLE_NOTIFICATION_VALUE = {0x01, 0x00};
34
35 /**
36 * Value used to enable indication for a client configuration descriptor
37 */
38 public static final byte[] ENABLE_INDICATION_VALUE = {0x02, 0x00};
39
40 /**
41 * Value used to disable notifications or indicatinos
42 */
43 public static final byte[] DISABLE_NOTIFICATION_VALUE = {0x00, 0x00};
44
45 /**
46 * Descriptor read permission
47 */
48 public static final int PERMISSION_READ = 0x01;
49
50 /**
51 * Descriptor permission: Allow encrypted read operations
52 */
53 public static final int PERMISSION_READ_ENCRYPTED = 0x02;
54
55 /**
56 * Descriptor permission: Allow reading with man-in-the-middle protection
57 */
58 public static final int PERMISSION_READ_ENCRYPTED_MITM = 0x04;
59
60 /**
61 * Descriptor write permission
62 */
63 public static final int PERMISSION_WRITE = 0x10;
64
65 /**
66 * Descriptor permission: Allow encrypted writes
67 */
68 public static final int PERMISSION_WRITE_ENCRYPTED = 0x20;
69
70 /**
71 * Descriptor permission: Allow encrypted writes with man-in-the-middle
72 * protection
73 */
74 public static final int PERMISSION_WRITE_ENCRYPTED_MITM = 0x40;
75
76 /**
77 * Descriptor permission: Allow signed write operations
78 */
79 public static final int PERMISSION_WRITE_SIGNED = 0x80;
80
81 /**
82 * Descriptor permission: Allow signed write operations with
83 * man-in-the-middle protection
84 */
85 public static final int PERMISSION_WRITE_SIGNED_MITM = 0x100;
86
87 /**
88 * The UUID of this descriptor.
89 * @hide
90 */
91 protected UUID mUuid;
92
93 /**
94 * Permissions for this descriptor
95 * @hide
96 */
97 protected int mPermissions;
98
99 /**
100 * Back-reference to the characteristic this descriptor belongs to.
101 * @hide
102 */
103 protected BluetoothGattCharacteristic mCharacteristic;
104
105 /**
106 * The value for this descriptor.
107 * @hide
108 */
109 protected byte[] mValue;
110
111 /**
112 * Create a new BluetoothGattDescriptor.
113 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
114 *
Matthew Xieddf7e472013-03-01 18:41:02 -0800115 * @param uuid The UUID for this descriptor
116 * @param permissions Permissions for this descriptor
117 */
118 public BluetoothGattDescriptor(UUID uuid, int permissions) {
119 initDescriptor(null, uuid, permissions);
120 }
121
122 /**
123 * Create a new BluetoothGattDescriptor.
124 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
125 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800126 * @param characteristic The characteristic this descriptor belongs to
127 * @param uuid The UUID for this descriptor
128 * @param permissions Permissions for this descriptor
129 */
130 /*package*/ BluetoothGattDescriptor(BluetoothGattCharacteristic characteristic, UUID uuid,
131 int permissions) {
Matthew Xieddf7e472013-03-01 18:41:02 -0800132 initDescriptor(characteristic, uuid, permissions);
133 }
134
135 private void initDescriptor(BluetoothGattCharacteristic characteristic, UUID uuid,
136 int permissions) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800137 mCharacteristic = characteristic;
138 mUuid = uuid;
139 mPermissions = permissions;
140 }
141
142 /**
143 * Returns the characteristic this descriptor belongs to.
144 * @return The characteristic.
145 */
146 public BluetoothGattCharacteristic getCharacteristic() {
147 return mCharacteristic;
148 }
149
150 /**
Matthew Xieddf7e472013-03-01 18:41:02 -0800151 * Set the back-reference to the associated characteristic
152 * @hide
153 */
154 /*package*/ void setCharacteristic(BluetoothGattCharacteristic characteristic) {
155 mCharacteristic = characteristic;
156 }
157
158 /**
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800159 * Returns the UUID of this descriptor.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800160 *
161 * @return UUID of this descriptor
162 */
163 public UUID getUuid() {
164 return mUuid;
165 }
166
167 /**
168 * Returns the permissions for this descriptor.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800169 *
170 * @return Permissions of this descriptor
171 */
172 public int getPermissions() {
173 return mPermissions;
174 }
175
176 /**
177 * Returns the stored value for this descriptor
178 *
179 * <p>This function returns the stored value for this descriptor as
Matthew Xieddf7e472013-03-01 18:41:02 -0800180 * retrieved by calling {@link BluetoothGatt#readDescriptor}. The cached
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800181 * value of the descriptor is updated as a result of a descriptor read
182 * operation.
183 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800184 * @return Cached value of the descriptor
185 */
186 public byte[] getValue() {
187 return mValue;
188 }
189
190 /**
191 * Updates the locally stored value of this descriptor.
192 *
193 * <p>This function modifies the locally stored cached value of this
194 * descriptor. To send the value to the remote device, call
195 * {@link BluetoothGatt#writeDescriptor} to send the value to the
196 * remote device.
197 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800198 * @param value New value for this descriptor
199 * @return true if the locally stored value has been set, false if the
200 * requested value could not be stored locally.
201 */
202 public boolean setValue(byte[] value) {
203 mValue = value;
204 return true;
205 }
206}