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.compat.annotation.UnsupportedAppUsage; 20 import android.os.Parcel; 21 import android.os.ParcelUuid; 22 import android.os.Parcelable; 23 24 import java.util.UUID; 25 26 /** 27 * Represents a Bluetooth GATT Descriptor 28 * 29 * <p> GATT Descriptors contain additional information and attributes of a GATT 30 * characteristic, {@link BluetoothGattCharacteristic}. They can be used to describe 31 * the characteristic's features or to control certain behaviours of the characteristic. 32 */ 33 public class BluetoothGattDescriptor implements Parcelable { 34 35 /** 36 * Value used to enable notification for a client configuration descriptor 37 */ 38 public static final byte[] ENABLE_NOTIFICATION_VALUE = {0x01, 0x00}; 39 40 /** 41 * Value used to enable indication for a client configuration descriptor 42 */ 43 public static final byte[] ENABLE_INDICATION_VALUE = {0x02, 0x00}; 44 45 /** 46 * Value used to disable notifications or indicatinos 47 */ 48 public static final byte[] DISABLE_NOTIFICATION_VALUE = {0x00, 0x00}; 49 50 /** 51 * Descriptor read permission 52 */ 53 public static final int PERMISSION_READ = 0x01; 54 55 /** 56 * Descriptor permission: Allow encrypted read operations 57 */ 58 public static final int PERMISSION_READ_ENCRYPTED = 0x02; 59 60 /** 61 * Descriptor permission: Allow reading with person-in-the-middle protection 62 */ 63 public static final int PERMISSION_READ_ENCRYPTED_MITM = 0x04; 64 65 /** 66 * Descriptor write permission 67 */ 68 public static final int PERMISSION_WRITE = 0x10; 69 70 /** 71 * Descriptor permission: Allow encrypted writes 72 */ 73 public static final int PERMISSION_WRITE_ENCRYPTED = 0x20; 74 75 /** 76 * Descriptor permission: Allow encrypted writes with person-in-the-middle 77 * protection 78 */ 79 public static final int PERMISSION_WRITE_ENCRYPTED_MITM = 0x40; 80 81 /** 82 * Descriptor permission: Allow signed write operations 83 */ 84 public static final int PERMISSION_WRITE_SIGNED = 0x80; 85 86 /** 87 * Descriptor permission: Allow signed write operations with 88 * person-in-the-middle protection 89 */ 90 public static final int PERMISSION_WRITE_SIGNED_MITM = 0x100; 91 92 /** 93 * The UUID of this descriptor. 94 * 95 * @hide 96 */ 97 protected UUID mUuid; 98 99 /** 100 * Instance ID for this descriptor. 101 * 102 * @hide 103 */ 104 @UnsupportedAppUsage 105 protected int mInstance; 106 107 /** 108 * Permissions for this descriptor 109 * 110 * @hide 111 */ 112 protected int mPermissions; 113 114 /** 115 * Back-reference to the characteristic this descriptor belongs to. 116 * 117 * @hide 118 */ 119 @UnsupportedAppUsage 120 protected BluetoothGattCharacteristic mCharacteristic; 121 122 /** 123 * The value for this descriptor. 124 * 125 * @hide 126 */ 127 protected byte[] mValue; 128 129 /** 130 * Create a new BluetoothGattDescriptor. 131 * 132 * @param uuid The UUID for this descriptor 133 * @param permissions Permissions for this descriptor 134 */ BluetoothGattDescriptor(UUID uuid, int permissions)135 public BluetoothGattDescriptor(UUID uuid, int permissions) { 136 initDescriptor(null, uuid, 0, permissions); 137 } 138 139 /** 140 * Create a new BluetoothGattDescriptor. 141 * 142 * @param characteristic The characteristic this descriptor belongs to 143 * @param uuid The UUID for this descriptor 144 * @param permissions Permissions for this descriptor 145 */ BluetoothGattDescriptor(BluetoothGattCharacteristic characteristic, UUID uuid, int instance, int permissions)146 /*package*/ BluetoothGattDescriptor(BluetoothGattCharacteristic characteristic, UUID uuid, 147 int instance, int permissions) { 148 initDescriptor(characteristic, uuid, instance, permissions); 149 } 150 151 /** 152 * @hide 153 */ BluetoothGattDescriptor(UUID uuid, int instance, int permissions)154 public BluetoothGattDescriptor(UUID uuid, int instance, int permissions) { 155 initDescriptor(null, uuid, instance, permissions); 156 } 157 initDescriptor(BluetoothGattCharacteristic characteristic, UUID uuid, int instance, int permissions)158 private void initDescriptor(BluetoothGattCharacteristic characteristic, UUID uuid, 159 int instance, int permissions) { 160 mCharacteristic = characteristic; 161 mUuid = uuid; 162 mInstance = instance; 163 mPermissions = permissions; 164 } 165 166 @Override describeContents()167 public int describeContents() { 168 return 0; 169 } 170 171 @Override writeToParcel(Parcel out, int flags)172 public void writeToParcel(Parcel out, int flags) { 173 out.writeParcelable(new ParcelUuid(mUuid), 0); 174 out.writeInt(mInstance); 175 out.writeInt(mPermissions); 176 } 177 178 public static final @android.annotation.NonNull Parcelable.Creator<BluetoothGattDescriptor> CREATOR = 179 new Parcelable.Creator<BluetoothGattDescriptor>() { 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 */ getValue()264 public byte[] getValue() { 265 return mValue; 266 } 267 268 /** 269 * Updates the locally stored value of this descriptor. 270 * 271 * <p>This function modifies the locally stored cached value of this 272 * descriptor. To send the value to the remote device, call 273 * {@link BluetoothGatt#writeDescriptor} to send the value to the 274 * remote device. 275 * 276 * @param value New value for this descriptor 277 * @return true if the locally stored value has been set, false if the requested value could not 278 * be stored locally. 279 */ setValue(byte[] value)280 public boolean setValue(byte[] value) { 281 mValue = value; 282 return true; 283 } 284 } 285