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