blob: 481680465247fb5c0a6b5d9a3e0a5d3d8477b0f6 [file] [log] [blame]
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001/*
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 */
16package android.bluetooth;
17
Jakub Pawlowski8d312a82016-03-01 18:50:27 -080018import android.os.Parcel;
Jakub Pawlowski8d312a82016-03-01 18:50:27 -080019import android.os.ParcelUuid;
Jack Hea355e5e2017-08-22 16:06:54 -070020import android.os.Parcelable;
21
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080022import java.util.ArrayList;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080023import java.util.List;
24import java.util.UUID;
25
26/**
Matthew Xieddf7e472013-03-01 18:41:02 -080027 * Represents a Bluetooth GATT Characteristic
Matthew Xie33ec9842013-04-03 00:29:27 -070028 *
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 Batta99081122013-02-05 15:28:33 -080032 */
Jakub Pawlowski8d312a82016-03-01 18:50:27 -080033public class BluetoothGattCharacteristic implements Parcelable {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080034
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 Xieddf7e472013-03-01 18:41:02 -0800128 * Write characteristic including authentication signature
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800129 */
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 Hea355e5e2017-08-22 16:06:54 -0700175 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800176 * @hide
177 */
178 protected UUID mUuid;
179
180 /**
181 * Instance ID for this characteristic.
Jack Hea355e5e2017-08-22 16:06:54 -0700182 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800183 * @hide
184 */
185 protected int mInstance;
186
187 /**
188 * Characteristic properties.
Jack Hea355e5e2017-08-22 16:06:54 -0700189 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800190 * @hide
191 */
192 protected int mProperties;
193
194 /**
195 * Characteristic permissions.
Jack Hea355e5e2017-08-22 16:06:54 -0700196 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800197 * @hide
198 */
199 protected int mPermissions;
200
201 /**
202 * Key size (default = 16).
Jack Hea355e5e2017-08-22 16:06:54 -0700203 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800204 * @hide
205 */
206 protected int mKeySize = 16;
207
208 /**
209 * Write type for this characteristic.
210 * See WRITE_TYPE_* constants.
Jack Hea355e5e2017-08-22 16:06:54 -0700211 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800212 * @hide
213 */
214 protected int mWriteType;
215
216 /**
217 * Back-reference to the service this characteristic belongs to.
Jack Hea355e5e2017-08-22 16:06:54 -0700218 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800219 * @hide
220 */
221 protected BluetoothGattService mService;
222
223 /**
224 * The cached value of this characteristic.
Jack Hea355e5e2017-08-22 16:06:54 -0700225 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800226 * @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 Xieddf7e472013-03-01 18:41:02 -0800236 * 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 Batta99081122013-02-05 15:28:33 -0800248 * Create a new BluetoothGattCharacteristic
Jack Hea355e5e2017-08-22 16:06:54 -0700249 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800250 * @hide
251 */
252 /*package*/ BluetoothGattCharacteristic(BluetoothGattService service,
Jack Hea355e5e2017-08-22 16:06:54 -0700253 UUID uuid, int instanceId,
254 int properties, int permissions) {
Matthew Xieddf7e472013-03-01 18:41:02 -0800255 initCharacteristic(service, uuid, instanceId, properties, permissions);
256 }
257
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800258 /**
259 * Create a new BluetoothGattCharacteristic
Jack Hea355e5e2017-08-22 16:06:54 -0700260 *
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800261 * @hide
262 */
263 public BluetoothGattCharacteristic(UUID uuid, int instanceId,
Jack Hea355e5e2017-08-22 16:06:54 -0700264 int properties, int permissions) {
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800265 initCharacteristic(null, uuid, instanceId, properties, permissions);
266 }
267
Matthew Xieddf7e472013-03-01 18:41:02 -0800268 private void initCharacteristic(BluetoothGattService service,
Jack Hea355e5e2017-08-22 16:06:54 -0700269 UUID uuid, int instanceId,
270 int properties, int permissions) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800271 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 Pawlowski8d312a82016-03-01 18:50:27 -0800287 * @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 Pawlowski2168fc22016-04-01 07:17:15 -0700298 out.writeInt(mKeySize);
299 out.writeInt(mWriteType);
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800300 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 Hea355e5e2017-08-22 16:06:54 -0700315 mUuid = ((ParcelUuid) in.readParcelable(null)).getUuid();
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800316 mInstance = in.readInt();
317 mProperties = in.readInt();
318 mPermissions = in.readInt();
Jakub Pawlowski2168fc22016-04-01 07:17:15 -0700319 mKeySize = in.readInt();
320 mWriteType = in.readInt();
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800321
322 mDescriptors = new ArrayList<BluetoothGattDescriptor>();
323
324 ArrayList<BluetoothGattDescriptor> descs =
325 in.createTypedArrayList(BluetoothGattDescriptor.CREATOR);
326 if (descs != null) {
Jack Hea355e5e2017-08-22 16:06:54 -0700327 for (BluetoothGattDescriptor desc : descs) {
Jakub Pawlowski8d312a82016-03-01 18:50:27 -0800328 desc.setCharacteristic(this);
329 mDescriptors.add(desc);
330 }
331 }
332 }
333
334 /**
Jakub Pawlowskid75f5122016-04-01 07:51:45 -0700335 * Returns the desired key size.
Jack Hea355e5e2017-08-22 16:06:54 -0700336 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800337 * @hide
338 */
Jakub Pawlowskid75f5122016-04-01 07:51:45 -0700339 public int getKeySize() {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800340 return mKeySize;
341 }
342
343 /**
Matthew Xieddf7e472013-03-01 18:41:02 -0800344 * 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 Batta99081122013-02-05 15:28:33 -0800349 */
Matthew Xieddf7e472013-03-01 18:41:02 -0800350 public boolean addDescriptor(BluetoothGattDescriptor descriptor) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800351 mDescriptors.add(descriptor);
Matthew Xieddf7e472013-03-01 18:41:02 -0800352 descriptor.setCharacteristic(this);
353 return true;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800354 }
355
356 /**
Andre Eisenbach25b9cf92013-07-08 23:58:16 -0700357 * Get a descriptor by UUID and isntance id.
Jack Hea355e5e2017-08-22 16:06:54 -0700358 *
Andre Eisenbach25b9cf92013-07-08 23:58:16 -0700359 * @hide
360 */
361 /*package*/ BluetoothGattDescriptor getDescriptor(UUID uuid, int instanceId) {
Jack Hea355e5e2017-08-22 16:06:54 -0700362 for (BluetoothGattDescriptor descriptor : mDescriptors) {
Andre Eisenbach25b9cf92013-07-08 23:58:16 -0700363 if (descriptor.getUuid().equals(uuid)
Jack Hea355e5e2017-08-22 16:06:54 -0700364 && descriptor.getInstanceId() == instanceId) {
Andre Eisenbach25b9cf92013-07-08 23:58:16 -0700365 return descriptor;
366 }
367 }
368 return null;
369 }
370
371 /**
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800372 * Returns the service this characteristic belongs to.
Jack Hea355e5e2017-08-22 16:06:54 -0700373 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800374 * @return The asscociated service
375 */
376 public BluetoothGattService getService() {
377 return mService;
378 }
379
380 /**
Matthew Xieddf7e472013-03-01 18:41:02 -0800381 * Sets the service associated with this device.
Jack Hea355e5e2017-08-22 16:06:54 -0700382 *
Matthew Xieddf7e472013-03-01 18:41:02 -0800383 * @hide
384 */
385 /*package*/ void setService(BluetoothGattService service) {
386 mService = service;
387 }
388
389 /**
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800390 * Returns the UUID of this characteristic
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800391 *
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 Batta99081122013-02-05 15:28:33 -0800404 * @return Instance ID of this characteristic
405 */
406 public int getInstanceId() {
407 return mInstance;
408 }
409
410 /**
Jakub Pawlowskid75f5122016-04-01 07:51:45 -0700411 * Force the instance ID.
Jack Hea355e5e2017-08-22 16:06:54 -0700412 *
Jakub Pawlowskid75f5122016-04-01 07:51:45 -0700413 * @hide
414 */
415 public void setInstanceId(int instanceId) {
416 mInstance = instanceId;
417 }
418
419 /**
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800420 * 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 Batta99081122013-02-05 15:28:33 -0800425 * @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 Batta99081122013-02-05 15:28:33 -0800433 *
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 Batta99081122013-02-05 15:28:33 -0800442 *
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 Hea355e5e2017-08-22 16:06:54 -0700456 * @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 Batta99081122013-02-05 15:28:33 -0800458 */
459 public void setWriteType(int writeType) {
460 mWriteType = writeType;
461 }
462
463 /**
Matthew Xieddf7e472013-03-01 18:41:02 -0800464 * Set the desired key size.
Jack Hea355e5e2017-08-22 16:06:54 -0700465 *
Matthew Xieddf7e472013-03-01 18:41:02 -0800466 * @hide
467 */
468 public void setKeySize(int keySize) {
469 mKeySize = keySize;
470 }
471
472 /**
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800473 * Returns a list of descriptors for this characteristic.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800474 *
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 Hea355e5e2017-08-22 16:06:54 -0700485 * @return GATT descriptor object or null if no descriptor with the given UUID was found.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800486 */
487 public BluetoothGattDescriptor getDescriptor(UUID uuid) {
Jack Hea355e5e2017-08-22 16:06:54 -0700488 for (BluetoothGattDescriptor descriptor : mDescriptors) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800489 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 Xieddf7e472013-03-01 18:41:02 -0800500 * retrieved by calling {@link BluetoothGatt#readCharacteristic}. The cached
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800501 * 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 Batta99081122013-02-05 15:28:33 -0800504 * @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 Hea355e5e2017-08-22 16:06:54 -0700519 * @param formatType The format type used to interpret the characteristic value.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800520 * @param offset Offset at which the integer value can be found.
Jack Hea355e5e2017-08-22 16:06:54 -0700521 * @return Cached value of the characteristic or null of offset exceeds value size.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800522 */
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 Hea355e5e2017-08-22 16:06:54 -0700531 return unsignedBytesToInt(mValue[offset], mValue[offset + 1]);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800532
533 case FORMAT_UINT32:
Jack Hea355e5e2017-08-22 16:06:54 -0700534 return unsignedBytesToInt(mValue[offset], mValue[offset + 1],
535 mValue[offset + 2], mValue[offset + 3]);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800536 case FORMAT_SINT8:
537 return unsignedToSigned(unsignedByteToInt(mValue[offset]), 8);
538
539 case FORMAT_SINT16:
540 return unsignedToSigned(unsignedBytesToInt(mValue[offset],
Jack Hea355e5e2017-08-22 16:06:54 -0700541 mValue[offset + 1]), 16);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800542
543 case FORMAT_SINT32:
544 return unsignedToSigned(unsignedBytesToInt(mValue[offset],
Jack Hea355e5e2017-08-22 16:06:54 -0700545 mValue[offset + 1], mValue[offset + 2], mValue[offset + 3]), 32);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800546 }
547
548 return null;
549 }
550
551 /**
552 * Return the stored value of this characteristic.
553 * <p>See {@link #getValue} for details.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800554 *
Jack Hea355e5e2017-08-22 16:06:54 -0700555 * @param formatType The format type used to interpret the characteristic value.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800556 * @param offset Offset at which the float value can be found.
Jack Hea355e5e2017-08-22 16:06:54 -0700557 * @return Cached value of the characteristic at a given offset or null if the requested offset
558 * exceeds the value size.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800559 */
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 Hea355e5e2017-08-22 16:06:54 -0700565 return bytesToFloat(mValue[offset], mValue[offset + 1]);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800566
567 case FORMAT_FLOAT:
Jack Hea355e5e2017-08-22 16:06:54 -0700568 return bytesToFloat(mValue[offset], mValue[offset + 1],
569 mValue[offset + 2], mValue[offset + 3]);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800570 }
571
572 return null;
573 }
574
575 /**
576 * Return the stored value of this characteristic.
577 * <p>See {@link #getValue} for details.
Matthew Xieddf7e472013-03-01 18:41:02 -0800578 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800579 * @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 Eisenbach0fd06a12014-12-09 16:42:14 -0800583 if (mValue == null || offset > mValue.length) return null;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800584 byte[] strBytes = new byte[mValue.length - offset];
Jack Hea355e5e2017-08-22 16:06:54 -0700585 for (int i = 0; i != (mValue.length - offset); ++i) strBytes[i] = mValue[offset + i];
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800586 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 Batta99081122013-02-05 15:28:33 -0800597 * @param value New value for this characteristic
Jack Hea355e5e2017-08-22 16:06:54 -0700598 * @return true if the locally stored value has been set, false if the requested value could not
599 * be stored locally.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800600 */
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 Batta99081122013-02-05 15:28:33 -0800609 *
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 Hea355e5e2017-08-22 16:06:54 -0700625 mValue[offset] = (byte) (value & 0xFF);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800626 break;
627
628 case FORMAT_SINT16:
629 value = intToSignedBits(value, 16);
630 // Fall-through intended
631 case FORMAT_UINT16:
Jack Hea355e5e2017-08-22 16:06:54 -0700632 mValue[offset++] = (byte) (value & 0xFF);
633 mValue[offset] = (byte) ((value >> 8) & 0xFF);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800634 break;
635
636 case FORMAT_SINT32:
637 value = intToSignedBits(value, 32);
638 // Fall-through intended
639 case FORMAT_UINT32:
Jack Hea355e5e2017-08-22 16:06:54 -0700640 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 Batta99081122013-02-05 15:28:33 -0800644 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 Xieddf7e472013-03-01 18:41:02 -0800655 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800656 * @param mantissa Mantissa for this characteristic
Jack Hea355e5e2017-08-22 16:06:54 -0700657 * @param exponent exponent value for this characteristic
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800658 * @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 Hea355e5e2017-08-22 16:06:54 -0700671 mValue[offset++] = (byte) (mantissa & 0xFF);
672 mValue[offset] = (byte) ((mantissa >> 8) & 0x0F);
673 mValue[offset] += (byte) ((exponent & 0x0F) << 4);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800674 break;
675
676 case FORMAT_FLOAT:
677 mantissa = intToSignedBits(mantissa, 24);
678 exponent = intToSignedBits(exponent, 8);
Jack Hea355e5e2017-08-22 16:06:54 -0700679 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 Batta99081122013-02-05 15:28:33 -0800683 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 Xieddf7e472013-03-01 18:41:02 -0800695 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800696 * @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 Batta99081122013-02-05 15:28:33 -0800706 */
707 private int getTypeLen(int formatType) {
708 return formatType & 0xF;
709 }
710
711 /**
712 * Convert a signed byte to an unsigned int.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800713 */
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 Batta99081122013-02-05 15:28:33 -0800720 */
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 Batta99081122013-02-05 15:28:33 -0800727 */
728 private int unsignedBytesToInt(byte b0, byte b1, byte b2, byte b3) {
729 return (unsignedByteToInt(b0) + (unsignedByteToInt(b1) << 8))
Jack Hea355e5e2017-08-22 16:06:54 -0700730 + (unsignedByteToInt(b2) << 16) + (unsignedByteToInt(b3) << 24);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800731 }
732
733 /**
734 * Convert signed bytes to a 16-bit short float value.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800735 */
736 private float bytesToFloat(byte b0, byte b1) {
737 int mantissa = unsignedToSigned(unsignedByteToInt(b0)
Jack Hea355e5e2017-08-22 16:06:54 -0700738 + ((unsignedByteToInt(b1) & 0x0F) << 8), 12);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800739 int exponent = unsignedToSigned(unsignedByteToInt(b1) >> 4, 4);
Jack Hea355e5e2017-08-22 16:06:54 -0700740 return (float) (mantissa * Math.pow(10, exponent));
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800741 }
742
743 /**
744 * Convert signed bytes to a 32-bit short float value.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800745 */
746 private float bytesToFloat(byte b0, byte b1, byte b2, byte b3) {
747 int mantissa = unsignedToSigned(unsignedByteToInt(b0)
Jack Hea355e5e2017-08-22 16:06:54 -0700748 + (unsignedByteToInt(b1) << 8)
749 + (unsignedByteToInt(b2) << 16), 24);
750 return (float) (mantissa * Math.pow(10, b3));
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800751 }
752
753 /**
754 * Convert an unsigned integer value to a two's-complement encoded
755 * signed value.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800756 */
757 private int unsignedToSigned(int unsigned, int size) {
Jack Hea355e5e2017-08-22 16:06:54 -0700758 if ((unsigned & (1 << size - 1)) != 0) {
759 unsigned = -1 * ((1 << size - 1) - (unsigned & ((1 << size - 1) - 1)));
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800760 }
761 return unsigned;
762 }
763
764 /**
765 * Convert an integer into the signed bits of a given length.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800766 */
767 private int intToSignedBits(int i, int size) {
768 if (i < 0) {
Jack Hea355e5e2017-08-22 16:06:54 -0700769 i = (1 << size - 1) + (i & ((1 << size - 1) - 1));
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800770 }
771 return i;
772 }
773}