Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 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 | |
Jakub Pawlowski | 8d312a8 | 2016-03-01 18:50:27 -0800 | [diff] [blame] | 19 | import android.os.Parcel; |
| 20 | import android.os.Parcelable; |
| 21 | import android.os.ParcelUuid; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 22 | import java.util.UUID; |
| 23 | |
| 24 | /** |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 25 | * Represents a Bluetooth GATT Descriptor |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 26 | * |
| 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 Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 30 | */ |
Jakub Pawlowski | 8d312a8 | 2016-03-01 18:50:27 -0800 | [diff] [blame] | 31 | public class BluetoothGattDescriptor implements Parcelable { |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 32 | |
| 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 Eisenbach | 25b9cf9 | 2013-07-08 23:58:16 -0700 | [diff] [blame] | 97 | * Instance ID for this descriptor. |
| 98 | * @hide |
| 99 | */ |
| 100 | protected int mInstance; |
| 101 | |
| 102 | /** |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 103 | * 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 Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 124 | * @param uuid The UUID for this descriptor |
| 125 | * @param permissions Permissions for this descriptor |
| 126 | */ |
| 127 | public BluetoothGattDescriptor(UUID uuid, int permissions) { |
Andre Eisenbach | 25b9cf9 | 2013-07-08 23:58:16 -0700 | [diff] [blame] | 128 | initDescriptor(null, uuid, 0, permissions); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Create a new BluetoothGattDescriptor. |
| 133 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 134 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 135 | * @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 Eisenbach | 25b9cf9 | 2013-07-08 23:58:16 -0700 | [diff] [blame] | 140 | int instance, int permissions) { |
| 141 | initDescriptor(characteristic, uuid, instance, permissions); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Jakub Pawlowski | 8d312a8 | 2016-03-01 18:50:27 -0800 | [diff] [blame] | 144 | /** |
| 145 | * @hide |
| 146 | */ |
| 147 | public BluetoothGattDescriptor(UUID uuid, int instance, int permissions) { |
| 148 | initDescriptor(null, uuid, instance, permissions); |
| 149 | } |
| 150 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 151 | private void initDescriptor(BluetoothGattCharacteristic characteristic, UUID uuid, |
Andre Eisenbach | 25b9cf9 | 2013-07-08 23:58:16 -0700 | [diff] [blame] | 152 | int instance, int permissions) { |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 153 | mCharacteristic = characteristic; |
| 154 | mUuid = uuid; |
Andre Eisenbach | 25b9cf9 | 2013-07-08 23:58:16 -0700 | [diff] [blame] | 155 | mInstance = instance; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 156 | mPermissions = permissions; |
| 157 | } |
| 158 | |
| 159 | /** |
Jakub Pawlowski | 8d312a8 | 2016-03-01 18:50:27 -0800 | [diff] [blame] | 160 | * @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 Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 190 | * Returns the characteristic this descriptor belongs to. |
| 191 | * @return The characteristic. |
| 192 | */ |
| 193 | public BluetoothGattCharacteristic getCharacteristic() { |
| 194 | return mCharacteristic; |
| 195 | } |
| 196 | |
| 197 | /** |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 198 | * 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 Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 206 | * Returns the UUID of this descriptor. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 207 | * |
| 208 | * @return UUID of this descriptor |
| 209 | */ |
| 210 | public UUID getUuid() { |
| 211 | return mUuid; |
| 212 | } |
| 213 | |
| 214 | /** |
Andre Eisenbach | 25b9cf9 | 2013-07-08 23:58:16 -0700 | [diff] [blame] | 215 | * 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 Pawlowski | d75f512 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 230 | * Force the instance ID. |
| 231 | * @hide |
| 232 | */ |
| 233 | public void setInstanceId(int instanceId) { |
| 234 | mInstance = instanceId; |
| 235 | } |
| 236 | |
| 237 | /** |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 238 | * Returns the permissions for this descriptor. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 239 | * |
| 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 Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 250 | * retrieved by calling {@link BluetoothGatt#readDescriptor}. The cached |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 251 | * value of the descriptor is updated as a result of a descriptor read |
| 252 | * operation. |
| 253 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 254 | * @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 Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 268 | * @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 | } |