blob: 1a4fa487a4d784a7b0511daf360416af2006ce8c [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
Jakub Pawlowski8d312a82016-03-01 18:50:27 -080019import android.os.Parcel;
20import android.os.Parcelable;
21import android.os.ParcelUuid;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080022import java.util.UUID;
23
24/**
Matthew Xieddf7e472013-03-01 18:41:02 -080025 * Represents a Bluetooth GATT Descriptor
Matthew Xie33ec9842013-04-03 00:29:27 -070026 *
27 * <p> GATT Descriptors contain additional information and attributes of a GATT
28 * characteristic, {@link BluetoothGattCharacteristic}. They can be used to describe
29 * the characteristic's features or to control certain behaviours of the characteristic.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080030 */
Jakub Pawlowski8d312a82016-03-01 18:50:27 -080031public class BluetoothGattDescriptor implements Parcelable {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080032
33 /**
34 * Value used to enable notification for a client configuration descriptor
35 */
36 public static final byte[] ENABLE_NOTIFICATION_VALUE = {0x01, 0x00};
37
38 /**
39 * Value used to enable indication for a client configuration descriptor
40 */
41 public static final byte[] ENABLE_INDICATION_VALUE = {0x02, 0x00};
42
43 /**
44 * Value used to disable notifications or indicatinos
45 */
46 public static final byte[] DISABLE_NOTIFICATION_VALUE = {0x00, 0x00};
47
48 /**
49 * Descriptor read permission
50 */
51 public static final int PERMISSION_READ = 0x01;
52
53 /**
54 * Descriptor permission: Allow encrypted read operations
55 */
56 public static final int PERMISSION_READ_ENCRYPTED = 0x02;
57
58 /**
59 * Descriptor permission: Allow reading with man-in-the-middle protection
60 */
61 public static final int PERMISSION_READ_ENCRYPTED_MITM = 0x04;
62
63 /**
64 * Descriptor write permission
65 */
66 public static final int PERMISSION_WRITE = 0x10;
67
68 /**
69 * Descriptor permission: Allow encrypted writes
70 */
71 public static final int PERMISSION_WRITE_ENCRYPTED = 0x20;
72
73 /**
74 * Descriptor permission: Allow encrypted writes with man-in-the-middle
75 * protection
76 */
77 public static final int PERMISSION_WRITE_ENCRYPTED_MITM = 0x40;
78
79 /**
80 * Descriptor permission: Allow signed write operations
81 */
82 public static final int PERMISSION_WRITE_SIGNED = 0x80;
83
84 /**
85 * Descriptor permission: Allow signed write operations with
86 * man-in-the-middle protection
87 */
88 public static final int PERMISSION_WRITE_SIGNED_MITM = 0x100;
89
90 /**
91 * The UUID of this descriptor.
92 * @hide
93 */
94 protected UUID mUuid;
95
96 /**
Andre Eisenbach25b9cf92013-07-08 23:58:16 -070097 * Instance ID for this descriptor.
98 * @hide
99 */
100 protected int mInstance;
101
102 /**
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800103 * Permissions for this descriptor
104 * @hide
105 */
106 protected int mPermissions;
107
108 /**
109 * Back-reference to the characteristic this descriptor belongs to.
110 * @hide
111 */
112 protected BluetoothGattCharacteristic mCharacteristic;
113
114 /**
115 * The value for this descriptor.
116 * @hide
117 */
118 protected byte[] mValue;
119
120 /**
121 * Create a new BluetoothGattDescriptor.
122 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
123 *
Matthew Xieddf7e472013-03-01 18:41:02 -0800124 * @param uuid The UUID for this descriptor
125 * @param permissions Permissions for this descriptor
126 */
127 public BluetoothGattDescriptor(UUID uuid, int permissions) {
Andre Eisenbach25b9cf92013-07-08 23:58:16 -0700128 initDescriptor(null, uuid, 0, permissions);
Matthew Xieddf7e472013-03-01 18:41:02 -0800129 }
130
131 /**
132 * Create a new BluetoothGattDescriptor.
133 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
134 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800135 * @param characteristic The characteristic this descriptor belongs to
136 * @param uuid The UUID for this descriptor
137 * @param permissions Permissions for this descriptor
138 */
139 /*package*/ BluetoothGattDescriptor(BluetoothGattCharacteristic characteristic, UUID uuid,
Andre Eisenbach25b9cf92013-07-08 23:58:16 -0700140 int instance, int permissions) {
141 initDescriptor(characteristic, uuid, instance, permissions);
Matthew Xieddf7e472013-03-01 18:41:02 -0800142 }
143
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800144 /**
145 * @hide
146 */
147 public BluetoothGattDescriptor(UUID uuid, int instance, int permissions) {
148 initDescriptor(null, uuid, instance, permissions);
149 }
150
Matthew Xieddf7e472013-03-01 18:41:02 -0800151 private void initDescriptor(BluetoothGattCharacteristic characteristic, UUID uuid,
Andre Eisenbach25b9cf92013-07-08 23:58:16 -0700152 int instance, int permissions) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800153 mCharacteristic = characteristic;
154 mUuid = uuid;
Andre Eisenbach25b9cf92013-07-08 23:58:16 -0700155 mInstance = instance;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800156 mPermissions = permissions;
157 }
158
159 /**
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800160 * @hide
161 */
162 public int describeContents() {
163 return 0;
164 }
165
166 public void writeToParcel(Parcel out, int flags) {
167 out.writeParcelable(new ParcelUuid(mUuid), 0);
168 out.writeInt(mInstance);
169 out.writeInt(mPermissions);
170 }
171
172 public static final Parcelable.Creator<BluetoothGattDescriptor> CREATOR
173 = new Parcelable.Creator<BluetoothGattDescriptor>() {
174 public BluetoothGattDescriptor createFromParcel(Parcel in) {
175 return new BluetoothGattDescriptor(in);
176 }
177
178 public BluetoothGattDescriptor[] newArray(int size) {
179 return new BluetoothGattDescriptor[size];
180 }
181 };
182
183 private BluetoothGattDescriptor(Parcel in) {
184 mUuid = ((ParcelUuid)in.readParcelable(null)).getUuid();
185 mInstance = in.readInt();
186 mPermissions = in.readInt();
187 }
188
189 /**
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800190 * Returns the characteristic this descriptor belongs to.
191 * @return The characteristic.
192 */
193 public BluetoothGattCharacteristic getCharacteristic() {
194 return mCharacteristic;
195 }
196
197 /**
Matthew Xieddf7e472013-03-01 18:41:02 -0800198 * Set the back-reference to the associated characteristic
199 * @hide
200 */
201 /*package*/ void setCharacteristic(BluetoothGattCharacteristic characteristic) {
202 mCharacteristic = characteristic;
203 }
204
205 /**
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800206 * Returns the UUID of this descriptor.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800207 *
208 * @return UUID of this descriptor
209 */
210 public UUID getUuid() {
211 return mUuid;
212 }
213
214 /**
Andre Eisenbach25b9cf92013-07-08 23:58:16 -0700215 * Returns the instance ID for this descriptor.
216 *
217 * <p>If a remote device offers multiple descriptors with the same UUID,
218 * the instance ID is used to distuinguish between descriptors.
219 *
220 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
221 *
222 * @return Instance ID of this descriptor
223 * @hide
224 */
225 public int getInstanceId() {
226 return mInstance;
227 }
228
229 /**
Jakub Pawlowskid75f5122016-04-01 07:51:45 -0700230 * Force the instance ID.
231 * @hide
232 */
233 public void setInstanceId(int instanceId) {
234 mInstance = instanceId;
235 }
236
237 /**
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800238 * Returns the permissions for this descriptor.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800239 *
240 * @return Permissions of this descriptor
241 */
242 public int getPermissions() {
243 return mPermissions;
244 }
245
246 /**
247 * Returns the stored value for this descriptor
248 *
249 * <p>This function returns the stored value for this descriptor as
Matthew Xieddf7e472013-03-01 18:41:02 -0800250 * retrieved by calling {@link BluetoothGatt#readDescriptor}. The cached
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800251 * value of the descriptor is updated as a result of a descriptor read
252 * operation.
253 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800254 * @return Cached value of the descriptor
255 */
256 public byte[] getValue() {
257 return mValue;
258 }
259
260 /**
261 * Updates the locally stored value of this descriptor.
262 *
263 * <p>This function modifies the locally stored cached value of this
264 * descriptor. To send the value to the remote device, call
265 * {@link BluetoothGatt#writeDescriptor} to send the value to the
266 * remote device.
267 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800268 * @param value New value for this descriptor
269 * @return true if the locally stored value has been set, false if the
270 * requested value could not be stored locally.
271 */
272 public boolean setValue(byte[] value) {
273 mValue = value;
274 return true;
275 }
276}