• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package android.bluetooth;
18 
19 import android.annotation.NonNull;
20 import android.compat.annotation.UnsupportedAppUsage;
21 import android.os.Parcel;
22 import android.os.ParcelUuid;
23 import android.os.Parcelable;
24 
25 import java.util.UUID;
26 
27 /**
28  * Represents a Bluetooth GATT Descriptor
29  *
30  * <p> GATT Descriptors contain additional information and attributes of a GATT
31  * characteristic, {@link BluetoothGattCharacteristic}. They can be used to describe
32  * the characteristic's features or to control certain behaviours of the characteristic.
33  */
34 public class BluetoothGattDescriptor implements Parcelable {
35 
36     /**
37      * Value used to enable notification for a client configuration descriptor
38      */
39     public static final byte[] ENABLE_NOTIFICATION_VALUE = {0x01, 0x00};
40 
41     /**
42      * Value used to enable indication for a client configuration descriptor
43      */
44     public static final byte[] ENABLE_INDICATION_VALUE = {0x02, 0x00};
45 
46     /**
47      * Value used to disable notifications or indicatinos
48      */
49     public static final byte[] DISABLE_NOTIFICATION_VALUE = {0x00, 0x00};
50 
51     /**
52      * Descriptor read permission
53      */
54     public static final int PERMISSION_READ = 0x01;
55 
56     /**
57      * Descriptor permission: Allow encrypted read operations
58      */
59     public static final int PERMISSION_READ_ENCRYPTED = 0x02;
60 
61     /**
62      * Descriptor permission: Allow reading with person-in-the-middle protection
63      */
64     public static final int PERMISSION_READ_ENCRYPTED_MITM = 0x04;
65 
66     /**
67      * Descriptor write permission
68      */
69     public static final int PERMISSION_WRITE = 0x10;
70 
71     /**
72      * Descriptor permission: Allow encrypted writes
73      */
74     public static final int PERMISSION_WRITE_ENCRYPTED = 0x20;
75 
76     /**
77      * Descriptor permission: Allow encrypted writes with person-in-the-middle
78      * protection
79      */
80     public static final int PERMISSION_WRITE_ENCRYPTED_MITM = 0x40;
81 
82     /**
83      * Descriptor permission: Allow signed write operations
84      */
85     public static final int PERMISSION_WRITE_SIGNED = 0x80;
86 
87     /**
88      * Descriptor permission: Allow signed write operations with
89      * person-in-the-middle protection
90      */
91     public static final int PERMISSION_WRITE_SIGNED_MITM = 0x100;
92 
93     /**
94      * The UUID of this descriptor.
95      *
96      * @hide
97      */
98     protected UUID mUuid;
99 
100     /**
101      * Instance ID for this descriptor.
102      *
103      * @hide
104      */
105     @UnsupportedAppUsage
106     protected int mInstance;
107 
108     /**
109      * Permissions for this descriptor
110      *
111      * @hide
112      */
113     protected int mPermissions;
114 
115     /**
116      * Back-reference to the characteristic this descriptor belongs to.
117      *
118      * @hide
119      */
120     @UnsupportedAppUsage
121     protected BluetoothGattCharacteristic mCharacteristic;
122 
123     /**
124      * The value for this descriptor.
125      *
126      * @hide
127      */
128     protected byte[] mValue;
129 
130     /**
131      * Create a new BluetoothGattDescriptor.
132      *
133      * @param uuid The UUID for this descriptor
134      * @param permissions Permissions for this descriptor
135      */
BluetoothGattDescriptor(UUID uuid, int permissions)136     public BluetoothGattDescriptor(UUID uuid, int permissions) {
137         initDescriptor(null, uuid, 0, permissions);
138     }
139 
140     /**
141      * Create a new BluetoothGattDescriptor.
142      *
143      * @param characteristic The characteristic this descriptor belongs to
144      * @param uuid The UUID for this descriptor
145      * @param permissions Permissions for this descriptor
146      */
BluetoothGattDescriptor(BluetoothGattCharacteristic characteristic, UUID uuid, int instance, int permissions)147     /*package*/ BluetoothGattDescriptor(BluetoothGattCharacteristic characteristic, UUID uuid,
148             int instance, int permissions) {
149         initDescriptor(characteristic, uuid, instance, permissions);
150     }
151 
152     /**
153      * @hide
154      */
BluetoothGattDescriptor(UUID uuid, int instance, int permissions)155     public BluetoothGattDescriptor(UUID uuid, int instance, int permissions) {
156         initDescriptor(null, uuid, instance, permissions);
157     }
158 
initDescriptor(BluetoothGattCharacteristic characteristic, UUID uuid, int instance, int permissions)159     private void initDescriptor(BluetoothGattCharacteristic characteristic, UUID uuid,
160             int instance, int permissions) {
161         mCharacteristic = characteristic;
162         mUuid = uuid;
163         mInstance = instance;
164         mPermissions = permissions;
165     }
166 
167     @Override
describeContents()168     public int describeContents() {
169         return 0;
170     }
171 
172     @Override
writeToParcel(Parcel out, int flags)173     public void writeToParcel(Parcel out, int flags) {
174         out.writeParcelable(new ParcelUuid(mUuid), 0);
175         out.writeInt(mInstance);
176         out.writeInt(mPermissions);
177     }
178 
179     public static final @NonNull Creator<BluetoothGattDescriptor> CREATOR = new Creator<>() {
180         public BluetoothGattDescriptor createFromParcel(Parcel in) {
181             return new BluetoothGattDescriptor(in);
182         }
183 
184         public BluetoothGattDescriptor[] newArray(int size) {
185             return new BluetoothGattDescriptor[size];
186         }
187     };
188 
BluetoothGattDescriptor(Parcel in)189     private BluetoothGattDescriptor(Parcel in) {
190         mUuid = ((ParcelUuid) in.readParcelable(null)).getUuid();
191         mInstance = in.readInt();
192         mPermissions = in.readInt();
193     }
194 
195     /**
196      * Returns the characteristic this descriptor belongs to.
197      *
198      * @return The characteristic.
199      */
getCharacteristic()200     public BluetoothGattCharacteristic getCharacteristic() {
201         return mCharacteristic;
202     }
203 
204     /**
205      * Set the back-reference to the associated characteristic
206      *
207      * @hide
208      */
209     @UnsupportedAppUsage
setCharacteristic(BluetoothGattCharacteristic characteristic)210     /*package*/ void setCharacteristic(BluetoothGattCharacteristic characteristic) {
211         mCharacteristic = characteristic;
212     }
213 
214     /**
215      * Returns the UUID of this descriptor.
216      *
217      * @return UUID of this descriptor
218      */
getUuid()219     public UUID getUuid() {
220         return mUuid;
221     }
222 
223     /**
224      * Returns the instance ID for this descriptor.
225      *
226      * <p>If a remote device offers multiple descriptors with the same UUID,
227      * the instance ID is used to distuinguish between descriptors.
228      *
229      * @return Instance ID of this descriptor
230      * @hide
231      */
getInstanceId()232     public int getInstanceId() {
233         return mInstance;
234     }
235 
236     /**
237      * Force the instance ID.
238      *
239      * @hide
240      */
setInstanceId(int instanceId)241     public void setInstanceId(int instanceId) {
242         mInstance = instanceId;
243     }
244 
245     /**
246      * Returns the permissions for this descriptor.
247      *
248      * @return Permissions of this descriptor
249      */
getPermissions()250     public int getPermissions() {
251         return mPermissions;
252     }
253 
254     /**
255      * Returns the stored value for this descriptor
256      *
257      * <p>This function returns the stored value for this descriptor as
258      * retrieved by calling {@link BluetoothGatt#readDescriptor}. The cached
259      * value of the descriptor is updated as a result of a descriptor read
260      * operation.
261      *
262      * @return Cached value of the descriptor
263      *
264      * @deprecated  Use {@link BluetoothGatt#readDescriptor(BluetoothGattDescriptor)} instead
265      */
266     @Deprecated
getValue()267     public byte[] getValue() {
268         return mValue;
269     }
270 
271     /**
272      * Updates the locally stored value of this descriptor.
273      *
274      * <p>This function modifies the locally stored cached value of this
275      * descriptor. To send the value to the remote device, call
276      * {@link BluetoothGatt#writeDescriptor} to send the value to the
277      * remote device.
278      *
279      * @param value New value for this descriptor
280      * @return true if the locally stored value has been set, false if the requested value could not
281      * be stored locally.
282      *
283      * @deprecated Pass the descriptor value directly into
284      * {@link BluetoothGatt#writeDescriptor(BluetoothGattDescriptor, byte[])}
285      */
286     @Deprecated
setValue(byte[] value)287     public boolean setValue(byte[] value) {
288         mValue = value;
289         return true;
290     }
291 }
292