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 | package android.bluetooth; |
| 17 | |
Jakub Pawlowski | 8d312a8 | 2016-03-01 18:50:27 -0800 | [diff] [blame] | 18 | import android.os.Parcel; |
Jakub Pawlowski | 8d312a8 | 2016-03-01 18:50:27 -0800 | [diff] [blame] | 19 | import android.os.ParcelUuid; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 20 | import android.os.Parcelable; |
| 21 | |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 22 | import java.util.ArrayList; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 23 | import java.util.List; |
| 24 | import java.util.UUID; |
| 25 | |
| 26 | /** |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 27 | * Represents a Bluetooth GATT Characteristic |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 28 | * |
| 29 | * <p>A GATT characteristic is a basic data element used to construct a GATT service, |
| 30 | * {@link BluetoothGattService}. The characteristic contains a value as well as |
| 31 | * additional information and optional GATT descriptors, {@link BluetoothGattDescriptor}. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 32 | */ |
Jakub Pawlowski | 8d312a8 | 2016-03-01 18:50:27 -0800 | [diff] [blame] | 33 | public class BluetoothGattCharacteristic implements Parcelable { |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Characteristic proprty: Characteristic is broadcastable. |
| 37 | */ |
| 38 | public static final int PROPERTY_BROADCAST = 0x01; |
| 39 | |
| 40 | /** |
| 41 | * Characteristic property: Characteristic is readable. |
| 42 | */ |
| 43 | public static final int PROPERTY_READ = 0x02; |
| 44 | |
| 45 | /** |
| 46 | * Characteristic property: Characteristic can be written without response. |
| 47 | */ |
| 48 | public static final int PROPERTY_WRITE_NO_RESPONSE = 0x04; |
| 49 | |
| 50 | /** |
| 51 | * Characteristic property: Characteristic can be written. |
| 52 | */ |
| 53 | public static final int PROPERTY_WRITE = 0x08; |
| 54 | |
| 55 | /** |
| 56 | * Characteristic property: Characteristic supports notification |
| 57 | */ |
| 58 | public static final int PROPERTY_NOTIFY = 0x10; |
| 59 | |
| 60 | /** |
| 61 | * Characteristic property: Characteristic supports indication |
| 62 | */ |
| 63 | public static final int PROPERTY_INDICATE = 0x20; |
| 64 | |
| 65 | /** |
| 66 | * Characteristic property: Characteristic supports write with signature |
| 67 | */ |
| 68 | public static final int PROPERTY_SIGNED_WRITE = 0x40; |
| 69 | |
| 70 | /** |
| 71 | * Characteristic property: Characteristic has extended properties |
| 72 | */ |
| 73 | public static final int PROPERTY_EXTENDED_PROPS = 0x80; |
| 74 | |
| 75 | /** |
| 76 | * Characteristic read permission |
| 77 | */ |
| 78 | public static final int PERMISSION_READ = 0x01; |
| 79 | |
| 80 | /** |
| 81 | * Characteristic permission: Allow encrypted read operations |
| 82 | */ |
| 83 | public static final int PERMISSION_READ_ENCRYPTED = 0x02; |
| 84 | |
| 85 | /** |
| 86 | * Characteristic permission: Allow reading with man-in-the-middle protection |
| 87 | */ |
| 88 | public static final int PERMISSION_READ_ENCRYPTED_MITM = 0x04; |
| 89 | |
| 90 | /** |
| 91 | * Characteristic write permission |
| 92 | */ |
| 93 | public static final int PERMISSION_WRITE = 0x10; |
| 94 | |
| 95 | /** |
| 96 | * Characteristic permission: Allow encrypted writes |
| 97 | */ |
| 98 | public static final int PERMISSION_WRITE_ENCRYPTED = 0x20; |
| 99 | |
| 100 | /** |
| 101 | * Characteristic permission: Allow encrypted writes with man-in-the-middle |
| 102 | * protection |
| 103 | */ |
| 104 | public static final int PERMISSION_WRITE_ENCRYPTED_MITM = 0x40; |
| 105 | |
| 106 | /** |
| 107 | * Characteristic permission: Allow signed write operations |
| 108 | */ |
| 109 | public static final int PERMISSION_WRITE_SIGNED = 0x80; |
| 110 | |
| 111 | /** |
| 112 | * Characteristic permission: Allow signed write operations with |
| 113 | * man-in-the-middle protection |
| 114 | */ |
| 115 | public static final int PERMISSION_WRITE_SIGNED_MITM = 0x100; |
| 116 | |
| 117 | /** |
| 118 | * Write characteristic, requesting acknoledgement by the remote device |
| 119 | */ |
| 120 | public static final int WRITE_TYPE_DEFAULT = 0x02; |
| 121 | |
| 122 | /** |
| 123 | * Wrtite characteristic without requiring a response by the remote device |
| 124 | */ |
| 125 | public static final int WRITE_TYPE_NO_RESPONSE = 0x01; |
| 126 | |
| 127 | /** |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 128 | * Write characteristic including authentication signature |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 129 | */ |
| 130 | public static final int WRITE_TYPE_SIGNED = 0x04; |
| 131 | |
| 132 | /** |
| 133 | * Characteristic value format type uint8 |
| 134 | */ |
| 135 | public static final int FORMAT_UINT8 = 0x11; |
| 136 | |
| 137 | /** |
| 138 | * Characteristic value format type uint16 |
| 139 | */ |
| 140 | public static final int FORMAT_UINT16 = 0x12; |
| 141 | |
| 142 | /** |
| 143 | * Characteristic value format type uint32 |
| 144 | */ |
| 145 | public static final int FORMAT_UINT32 = 0x14; |
| 146 | |
| 147 | /** |
| 148 | * Characteristic value format type sint8 |
| 149 | */ |
| 150 | public static final int FORMAT_SINT8 = 0x21; |
| 151 | |
| 152 | /** |
| 153 | * Characteristic value format type sint16 |
| 154 | */ |
| 155 | public static final int FORMAT_SINT16 = 0x22; |
| 156 | |
| 157 | /** |
| 158 | * Characteristic value format type sint32 |
| 159 | */ |
| 160 | public static final int FORMAT_SINT32 = 0x24; |
| 161 | |
| 162 | /** |
| 163 | * Characteristic value format type sfloat (16-bit float) |
| 164 | */ |
| 165 | public static final int FORMAT_SFLOAT = 0x32; |
| 166 | |
| 167 | /** |
| 168 | * Characteristic value format type float (32-bit float) |
| 169 | */ |
| 170 | public static final int FORMAT_FLOAT = 0x34; |
| 171 | |
| 172 | |
| 173 | /** |
| 174 | * The UUID of this characteristic. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 175 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 176 | * @hide |
| 177 | */ |
| 178 | protected UUID mUuid; |
| 179 | |
| 180 | /** |
| 181 | * Instance ID for this characteristic. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 182 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 183 | * @hide |
| 184 | */ |
| 185 | protected int mInstance; |
| 186 | |
| 187 | /** |
| 188 | * Characteristic properties. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 189 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 190 | * @hide |
| 191 | */ |
| 192 | protected int mProperties; |
| 193 | |
| 194 | /** |
| 195 | * Characteristic permissions. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 196 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 197 | * @hide |
| 198 | */ |
| 199 | protected int mPermissions; |
| 200 | |
| 201 | /** |
| 202 | * Key size (default = 16). |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 203 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 204 | * @hide |
| 205 | */ |
| 206 | protected int mKeySize = 16; |
| 207 | |
| 208 | /** |
| 209 | * Write type for this characteristic. |
| 210 | * See WRITE_TYPE_* constants. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 211 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 212 | * @hide |
| 213 | */ |
| 214 | protected int mWriteType; |
| 215 | |
| 216 | /** |
| 217 | * Back-reference to the service this characteristic belongs to. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 218 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 219 | * @hide |
| 220 | */ |
| 221 | protected BluetoothGattService mService; |
| 222 | |
| 223 | /** |
| 224 | * The cached value of this characteristic. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 225 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 226 | * @hide |
| 227 | */ |
| 228 | protected byte[] mValue; |
| 229 | |
| 230 | /** |
| 231 | * List of descriptors included in this characteristic. |
| 232 | */ |
| 233 | protected List<BluetoothGattDescriptor> mDescriptors; |
| 234 | |
| 235 | /** |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 236 | * Create a new BluetoothGattCharacteristic. |
| 237 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 238 | * |
| 239 | * @param uuid The UUID for this characteristic |
| 240 | * @param properties Properties of this characteristic |
| 241 | * @param permissions Permissions for this characteristic |
| 242 | */ |
| 243 | public BluetoothGattCharacteristic(UUID uuid, int properties, int permissions) { |
| 244 | initCharacteristic(null, uuid, 0, properties, permissions); |
| 245 | } |
| 246 | |
| 247 | /** |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 248 | * Create a new BluetoothGattCharacteristic |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 249 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 250 | * @hide |
| 251 | */ |
| 252 | /*package*/ BluetoothGattCharacteristic(BluetoothGattService service, |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 253 | UUID uuid, int instanceId, |
| 254 | int properties, int permissions) { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 255 | initCharacteristic(service, uuid, instanceId, properties, permissions); |
| 256 | } |
| 257 | |
Jakub Pawlowski | 8d312a8 | 2016-03-01 18:50:27 -0800 | [diff] [blame] | 258 | /** |
| 259 | * Create a new BluetoothGattCharacteristic |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 260 | * |
Jakub Pawlowski | 8d312a8 | 2016-03-01 18:50:27 -0800 | [diff] [blame] | 261 | * @hide |
| 262 | */ |
| 263 | public BluetoothGattCharacteristic(UUID uuid, int instanceId, |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 264 | int properties, int permissions) { |
Jakub Pawlowski | 8d312a8 | 2016-03-01 18:50:27 -0800 | [diff] [blame] | 265 | initCharacteristic(null, uuid, instanceId, properties, permissions); |
| 266 | } |
| 267 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 268 | private void initCharacteristic(BluetoothGattService service, |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 269 | UUID uuid, int instanceId, |
| 270 | int properties, int permissions) { |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 271 | mUuid = uuid; |
| 272 | mInstance = instanceId; |
| 273 | mProperties = properties; |
| 274 | mPermissions = permissions; |
| 275 | mService = service; |
| 276 | mValue = null; |
| 277 | mDescriptors = new ArrayList<BluetoothGattDescriptor>(); |
| 278 | |
| 279 | if ((mProperties & PROPERTY_WRITE_NO_RESPONSE) != 0) { |
| 280 | mWriteType = WRITE_TYPE_NO_RESPONSE; |
| 281 | } else { |
| 282 | mWriteType = WRITE_TYPE_DEFAULT; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | /** |
Jakub Pawlowski | 8d312a8 | 2016-03-01 18:50:27 -0800 | [diff] [blame] | 287 | * @hide |
| 288 | */ |
| 289 | public int describeContents() { |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | public void writeToParcel(Parcel out, int flags) { |
| 294 | out.writeParcelable(new ParcelUuid(mUuid), 0); |
| 295 | out.writeInt(mInstance); |
| 296 | out.writeInt(mProperties); |
| 297 | out.writeInt(mPermissions); |
Jakub Pawlowski | 2168fc2 | 2016-04-01 07:17:15 -0700 | [diff] [blame] | 298 | out.writeInt(mKeySize); |
| 299 | out.writeInt(mWriteType); |
Jakub Pawlowski | 8d312a8 | 2016-03-01 18:50:27 -0800 | [diff] [blame] | 300 | out.writeTypedList(mDescriptors); |
| 301 | } |
| 302 | |
| 303 | public static final Parcelable.Creator<BluetoothGattCharacteristic> CREATOR |
| 304 | = new Parcelable.Creator<BluetoothGattCharacteristic>() { |
| 305 | public BluetoothGattCharacteristic createFromParcel(Parcel in) { |
| 306 | return new BluetoothGattCharacteristic(in); |
| 307 | } |
| 308 | |
| 309 | public BluetoothGattCharacteristic[] newArray(int size) { |
| 310 | return new BluetoothGattCharacteristic[size]; |
| 311 | } |
| 312 | }; |
| 313 | |
| 314 | private BluetoothGattCharacteristic(Parcel in) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 315 | mUuid = ((ParcelUuid) in.readParcelable(null)).getUuid(); |
Jakub Pawlowski | 8d312a8 | 2016-03-01 18:50:27 -0800 | [diff] [blame] | 316 | mInstance = in.readInt(); |
| 317 | mProperties = in.readInt(); |
| 318 | mPermissions = in.readInt(); |
Jakub Pawlowski | 2168fc2 | 2016-04-01 07:17:15 -0700 | [diff] [blame] | 319 | mKeySize = in.readInt(); |
| 320 | mWriteType = in.readInt(); |
Jakub Pawlowski | 8d312a8 | 2016-03-01 18:50:27 -0800 | [diff] [blame] | 321 | |
| 322 | mDescriptors = new ArrayList<BluetoothGattDescriptor>(); |
| 323 | |
| 324 | ArrayList<BluetoothGattDescriptor> descs = |
| 325 | in.createTypedArrayList(BluetoothGattDescriptor.CREATOR); |
| 326 | if (descs != null) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 327 | for (BluetoothGattDescriptor desc : descs) { |
Jakub Pawlowski | 8d312a8 | 2016-03-01 18:50:27 -0800 | [diff] [blame] | 328 | desc.setCharacteristic(this); |
| 329 | mDescriptors.add(desc); |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | /** |
Jakub Pawlowski | d75f512 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 335 | * Returns the desired key size. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 336 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 337 | * @hide |
| 338 | */ |
Jakub Pawlowski | d75f512 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 339 | public int getKeySize() { |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 340 | return mKeySize; |
| 341 | } |
| 342 | |
| 343 | /** |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 344 | * Adds a descriptor to this characteristic. |
| 345 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 346 | * |
| 347 | * @param descriptor Descriptor to be added to this characteristic. |
| 348 | * @return true, if the descriptor was added to the characteristic |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 349 | */ |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 350 | public boolean addDescriptor(BluetoothGattDescriptor descriptor) { |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 351 | mDescriptors.add(descriptor); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 352 | descriptor.setCharacteristic(this); |
| 353 | return true; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /** |
Andre Eisenbach | 25b9cf9 | 2013-07-08 23:58:16 -0700 | [diff] [blame] | 357 | * Get a descriptor by UUID and isntance id. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 358 | * |
Andre Eisenbach | 25b9cf9 | 2013-07-08 23:58:16 -0700 | [diff] [blame] | 359 | * @hide |
| 360 | */ |
| 361 | /*package*/ BluetoothGattDescriptor getDescriptor(UUID uuid, int instanceId) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 362 | for (BluetoothGattDescriptor descriptor : mDescriptors) { |
Andre Eisenbach | 25b9cf9 | 2013-07-08 23:58:16 -0700 | [diff] [blame] | 363 | if (descriptor.getUuid().equals(uuid) |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 364 | && descriptor.getInstanceId() == instanceId) { |
Andre Eisenbach | 25b9cf9 | 2013-07-08 23:58:16 -0700 | [diff] [blame] | 365 | return descriptor; |
| 366 | } |
| 367 | } |
| 368 | return null; |
| 369 | } |
| 370 | |
| 371 | /** |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 372 | * Returns the service this characteristic belongs to. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 373 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 374 | * @return The asscociated service |
| 375 | */ |
| 376 | public BluetoothGattService getService() { |
| 377 | return mService; |
| 378 | } |
| 379 | |
| 380 | /** |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 381 | * Sets the service associated with this device. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 382 | * |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 383 | * @hide |
| 384 | */ |
| 385 | /*package*/ void setService(BluetoothGattService service) { |
| 386 | mService = service; |
| 387 | } |
| 388 | |
| 389 | /** |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 390 | * Returns the UUID of this characteristic |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 391 | * |
| 392 | * @return UUID of this characteristic |
| 393 | */ |
| 394 | public UUID getUuid() { |
| 395 | return mUuid; |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Returns the instance ID for this characteristic. |
| 400 | * |
| 401 | * <p>If a remote device offers multiple characteristics with the same UUID, |
| 402 | * the instance ID is used to distuinguish between characteristics. |
| 403 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 404 | * @return Instance ID of this characteristic |
| 405 | */ |
| 406 | public int getInstanceId() { |
| 407 | return mInstance; |
| 408 | } |
| 409 | |
| 410 | /** |
Jakub Pawlowski | d75f512 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 411 | * Force the instance ID. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 412 | * |
Jakub Pawlowski | d75f512 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 413 | * @hide |
| 414 | */ |
| 415 | public void setInstanceId(int instanceId) { |
| 416 | mInstance = instanceId; |
| 417 | } |
| 418 | |
| 419 | /** |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 420 | * Returns the properties of this characteristic. |
| 421 | * |
| 422 | * <p>The properties contain a bit mask of property flags indicating |
| 423 | * the features of this characteristic. |
| 424 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 425 | * @return Properties of this characteristic |
| 426 | */ |
| 427 | public int getProperties() { |
| 428 | return mProperties; |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Returns the permissions for this characteristic. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 433 | * |
| 434 | * @return Permissions of this characteristic |
| 435 | */ |
| 436 | public int getPermissions() { |
| 437 | return mPermissions; |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * Gets the write type for this characteristic. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 442 | * |
| 443 | * @return Write type for this characteristic |
| 444 | */ |
| 445 | public int getWriteType() { |
| 446 | return mWriteType; |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Set the write type for this characteristic |
| 451 | * |
| 452 | * <p>Setting the write type of a characteristic determines how the |
| 453 | * {@link BluetoothGatt#writeCharacteristic} function write this |
| 454 | * characteristic. |
| 455 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 456 | * @param writeType The write type to for this characteristic. Can be one of: {@link |
| 457 | * #WRITE_TYPE_DEFAULT}, {@link #WRITE_TYPE_NO_RESPONSE} or {@link #WRITE_TYPE_SIGNED}. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 458 | */ |
| 459 | public void setWriteType(int writeType) { |
| 460 | mWriteType = writeType; |
| 461 | } |
| 462 | |
| 463 | /** |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 464 | * Set the desired key size. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 465 | * |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 466 | * @hide |
| 467 | */ |
| 468 | public void setKeySize(int keySize) { |
| 469 | mKeySize = keySize; |
| 470 | } |
| 471 | |
| 472 | /** |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 473 | * Returns a list of descriptors for this characteristic. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 474 | * |
| 475 | * @return Descriptors for this characteristic |
| 476 | */ |
| 477 | public List<BluetoothGattDescriptor> getDescriptors() { |
| 478 | return mDescriptors; |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * Returns a descriptor with a given UUID out of the list of |
| 483 | * descriptors for this characteristic. |
| 484 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 485 | * @return GATT descriptor object or null if no descriptor with the given UUID was found. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 486 | */ |
| 487 | public BluetoothGattDescriptor getDescriptor(UUID uuid) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 488 | for (BluetoothGattDescriptor descriptor : mDescriptors) { |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 489 | if (descriptor.getUuid().equals(uuid)) { |
| 490 | return descriptor; |
| 491 | } |
| 492 | } |
| 493 | return null; |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * Get the stored value for this characteristic. |
| 498 | * |
| 499 | * <p>This function returns the stored value for this characteristic as |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 500 | * retrieved by calling {@link BluetoothGatt#readCharacteristic}. The cached |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 501 | * value of the characteristic is updated as a result of a read characteristic |
| 502 | * operation or if a characteristic update notification has been received. |
| 503 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 504 | * @return Cached value of the characteristic |
| 505 | */ |
| 506 | public byte[] getValue() { |
| 507 | return mValue; |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * Return the stored value of this characteristic. |
| 512 | * |
| 513 | * <p>The formatType parameter determines how the characteristic value |
| 514 | * is to be interpreted. For example, settting formatType to |
| 515 | * {@link #FORMAT_UINT16} specifies that the first two bytes of the |
| 516 | * characteristic value at the given offset are interpreted to generate the |
| 517 | * return value. |
| 518 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 519 | * @param formatType The format type used to interpret the characteristic value. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 520 | * @param offset Offset at which the integer value can be found. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 521 | * @return Cached value of the characteristic or null of offset exceeds value size. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 522 | */ |
| 523 | public Integer getIntValue(int formatType, int offset) { |
| 524 | if ((offset + getTypeLen(formatType)) > mValue.length) return null; |
| 525 | |
| 526 | switch (formatType) { |
| 527 | case FORMAT_UINT8: |
| 528 | return unsignedByteToInt(mValue[offset]); |
| 529 | |
| 530 | case FORMAT_UINT16: |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 531 | return unsignedBytesToInt(mValue[offset], mValue[offset + 1]); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 532 | |
| 533 | case FORMAT_UINT32: |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 534 | return unsignedBytesToInt(mValue[offset], mValue[offset + 1], |
| 535 | mValue[offset + 2], mValue[offset + 3]); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 536 | case FORMAT_SINT8: |
| 537 | return unsignedToSigned(unsignedByteToInt(mValue[offset]), 8); |
| 538 | |
| 539 | case FORMAT_SINT16: |
| 540 | return unsignedToSigned(unsignedBytesToInt(mValue[offset], |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 541 | mValue[offset + 1]), 16); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 542 | |
| 543 | case FORMAT_SINT32: |
| 544 | return unsignedToSigned(unsignedBytesToInt(mValue[offset], |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 545 | mValue[offset + 1], mValue[offset + 2], mValue[offset + 3]), 32); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | return null; |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * Return the stored value of this characteristic. |
| 553 | * <p>See {@link #getValue} for details. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 554 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 555 | * @param formatType The format type used to interpret the characteristic value. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 556 | * @param offset Offset at which the float value can be found. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 557 | * @return Cached value of the characteristic at a given offset or null if the requested offset |
| 558 | * exceeds the value size. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 559 | */ |
| 560 | public Float getFloatValue(int formatType, int offset) { |
| 561 | if ((offset + getTypeLen(formatType)) > mValue.length) return null; |
| 562 | |
| 563 | switch (formatType) { |
| 564 | case FORMAT_SFLOAT: |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 565 | return bytesToFloat(mValue[offset], mValue[offset + 1]); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 566 | |
| 567 | case FORMAT_FLOAT: |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 568 | return bytesToFloat(mValue[offset], mValue[offset + 1], |
| 569 | mValue[offset + 2], mValue[offset + 3]); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | return null; |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * Return the stored value of this characteristic. |
| 577 | * <p>See {@link #getValue} for details. |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 578 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 579 | * @param offset Offset at which the string value can be found. |
| 580 | * @return Cached value of the characteristic |
| 581 | */ |
| 582 | public String getStringValue(int offset) { |
Andre Eisenbach | 0fd06a1 | 2014-12-09 16:42:14 -0800 | [diff] [blame] | 583 | if (mValue == null || offset > mValue.length) return null; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 584 | byte[] strBytes = new byte[mValue.length - offset]; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 585 | for (int i = 0; i != (mValue.length - offset); ++i) strBytes[i] = mValue[offset + i]; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 586 | return new String(strBytes); |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * Updates the locally stored value of this characteristic. |
| 591 | * |
| 592 | * <p>This function modifies the locally stored cached value of this |
| 593 | * characteristic. To send the value to the remote device, call |
| 594 | * {@link BluetoothGatt#writeCharacteristic} to send the value to the |
| 595 | * remote device. |
| 596 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 597 | * @param value New value for this characteristic |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 598 | * @return true if the locally stored value has been set, false if the requested value could not |
| 599 | * be stored locally. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 600 | */ |
| 601 | public boolean setValue(byte[] value) { |
| 602 | mValue = value; |
| 603 | return true; |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * Set the locally stored value of this characteristic. |
| 608 | * <p>See {@link #setValue(byte[])} for details. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 609 | * |
| 610 | * @param value New value for this characteristic |
| 611 | * @param formatType Integer format type used to transform the value parameter |
| 612 | * @param offset Offset at which the value should be placed |
| 613 | * @return true if the locally stored value has been set |
| 614 | */ |
| 615 | public boolean setValue(int value, int formatType, int offset) { |
| 616 | int len = offset + getTypeLen(formatType); |
| 617 | if (mValue == null) mValue = new byte[len]; |
| 618 | if (len > mValue.length) return false; |
| 619 | |
| 620 | switch (formatType) { |
| 621 | case FORMAT_SINT8: |
| 622 | value = intToSignedBits(value, 8); |
| 623 | // Fall-through intended |
| 624 | case FORMAT_UINT8: |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 625 | mValue[offset] = (byte) (value & 0xFF); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 626 | break; |
| 627 | |
| 628 | case FORMAT_SINT16: |
| 629 | value = intToSignedBits(value, 16); |
| 630 | // Fall-through intended |
| 631 | case FORMAT_UINT16: |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 632 | mValue[offset++] = (byte) (value & 0xFF); |
| 633 | mValue[offset] = (byte) ((value >> 8) & 0xFF); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 634 | break; |
| 635 | |
| 636 | case FORMAT_SINT32: |
| 637 | value = intToSignedBits(value, 32); |
| 638 | // Fall-through intended |
| 639 | case FORMAT_UINT32: |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 640 | mValue[offset++] = (byte) (value & 0xFF); |
| 641 | mValue[offset++] = (byte) ((value >> 8) & 0xFF); |
| 642 | mValue[offset++] = (byte) ((value >> 16) & 0xFF); |
| 643 | mValue[offset] = (byte) ((value >> 24) & 0xFF); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 644 | break; |
| 645 | |
| 646 | default: |
| 647 | return false; |
| 648 | } |
| 649 | return true; |
| 650 | } |
| 651 | |
| 652 | /** |
| 653 | * Set the locally stored value of this characteristic. |
| 654 | * <p>See {@link #setValue(byte[])} for details. |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 655 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 656 | * @param mantissa Mantissa for this characteristic |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 657 | * @param exponent exponent value for this characteristic |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 658 | * @param formatType Float format type used to transform the value parameter |
| 659 | * @param offset Offset at which the value should be placed |
| 660 | * @return true if the locally stored value has been set |
| 661 | */ |
| 662 | public boolean setValue(int mantissa, int exponent, int formatType, int offset) { |
| 663 | int len = offset + getTypeLen(formatType); |
| 664 | if (mValue == null) mValue = new byte[len]; |
| 665 | if (len > mValue.length) return false; |
| 666 | |
| 667 | switch (formatType) { |
| 668 | case FORMAT_SFLOAT: |
| 669 | mantissa = intToSignedBits(mantissa, 12); |
| 670 | exponent = intToSignedBits(exponent, 4); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 671 | mValue[offset++] = (byte) (mantissa & 0xFF); |
| 672 | mValue[offset] = (byte) ((mantissa >> 8) & 0x0F); |
| 673 | mValue[offset] += (byte) ((exponent & 0x0F) << 4); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 674 | break; |
| 675 | |
| 676 | case FORMAT_FLOAT: |
| 677 | mantissa = intToSignedBits(mantissa, 24); |
| 678 | exponent = intToSignedBits(exponent, 8); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 679 | mValue[offset++] = (byte) (mantissa & 0xFF); |
| 680 | mValue[offset++] = (byte) ((mantissa >> 8) & 0xFF); |
| 681 | mValue[offset++] = (byte) ((mantissa >> 16) & 0xFF); |
| 682 | mValue[offset] += (byte) (exponent & 0xFF); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 683 | break; |
| 684 | |
| 685 | default: |
| 686 | return false; |
| 687 | } |
| 688 | |
| 689 | return true; |
| 690 | } |
| 691 | |
| 692 | /** |
| 693 | * Set the locally stored value of this characteristic. |
| 694 | * <p>See {@link #setValue(byte[])} for details. |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 695 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 696 | * @param value New value for this characteristic |
| 697 | * @return true if the locally stored value has been set |
| 698 | */ |
| 699 | public boolean setValue(String value) { |
| 700 | mValue = value.getBytes(); |
| 701 | return true; |
| 702 | } |
| 703 | |
| 704 | /** |
| 705 | * Returns the size of a give value type. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 706 | */ |
| 707 | private int getTypeLen(int formatType) { |
| 708 | return formatType & 0xF; |
| 709 | } |
| 710 | |
| 711 | /** |
| 712 | * Convert a signed byte to an unsigned int. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 713 | */ |
| 714 | private int unsignedByteToInt(byte b) { |
| 715 | return b & 0xFF; |
| 716 | } |
| 717 | |
| 718 | /** |
| 719 | * Convert signed bytes to a 16-bit unsigned int. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 720 | */ |
| 721 | private int unsignedBytesToInt(byte b0, byte b1) { |
| 722 | return (unsignedByteToInt(b0) + (unsignedByteToInt(b1) << 8)); |
| 723 | } |
| 724 | |
| 725 | /** |
| 726 | * Convert signed bytes to a 32-bit unsigned int. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 727 | */ |
| 728 | private int unsignedBytesToInt(byte b0, byte b1, byte b2, byte b3) { |
| 729 | return (unsignedByteToInt(b0) + (unsignedByteToInt(b1) << 8)) |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 730 | + (unsignedByteToInt(b2) << 16) + (unsignedByteToInt(b3) << 24); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | /** |
| 734 | * Convert signed bytes to a 16-bit short float value. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 735 | */ |
| 736 | private float bytesToFloat(byte b0, byte b1) { |
| 737 | int mantissa = unsignedToSigned(unsignedByteToInt(b0) |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 738 | + ((unsignedByteToInt(b1) & 0x0F) << 8), 12); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 739 | int exponent = unsignedToSigned(unsignedByteToInt(b1) >> 4, 4); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 740 | return (float) (mantissa * Math.pow(10, exponent)); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | /** |
| 744 | * Convert signed bytes to a 32-bit short float value. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 745 | */ |
| 746 | private float bytesToFloat(byte b0, byte b1, byte b2, byte b3) { |
| 747 | int mantissa = unsignedToSigned(unsignedByteToInt(b0) |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 748 | + (unsignedByteToInt(b1) << 8) |
| 749 | + (unsignedByteToInt(b2) << 16), 24); |
| 750 | return (float) (mantissa * Math.pow(10, b3)); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | /** |
| 754 | * Convert an unsigned integer value to a two's-complement encoded |
| 755 | * signed value. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 756 | */ |
| 757 | private int unsignedToSigned(int unsigned, int size) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 758 | if ((unsigned & (1 << size - 1)) != 0) { |
| 759 | unsigned = -1 * ((1 << size - 1) - (unsigned & ((1 << size - 1) - 1))); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 760 | } |
| 761 | return unsigned; |
| 762 | } |
| 763 | |
| 764 | /** |
| 765 | * Convert an integer into the signed bits of a given length. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 766 | */ |
| 767 | private int intToSignedBits(int i, int size) { |
| 768 | if (i < 0) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 769 | i = (1 << size - 1) + (i & ((1 << size - 1) - 1)); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 770 | } |
| 771 | return i; |
| 772 | } |
| 773 | } |