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