Fields Summary |
---|
public static final int | PROPERTY_BROADCASTCharacteristic proprty: Characteristic is broadcastable. |
public static final int | PROPERTY_READCharacteristic property: Characteristic is readable. |
public static final int | PROPERTY_WRITE_NO_RESPONSECharacteristic property: Characteristic can be written without response. |
public static final int | PROPERTY_WRITECharacteristic property: Characteristic can be written. |
public static final int | PROPERTY_NOTIFYCharacteristic property: Characteristic supports notification |
public static final int | PROPERTY_INDICATECharacteristic property: Characteristic supports indication |
public static final int | PROPERTY_SIGNED_WRITECharacteristic property: Characteristic supports write with signature |
public static final int | PROPERTY_EXTENDED_PROPSCharacteristic property: Characteristic has extended properties |
public static final int | PERMISSION_READCharacteristic read permission |
public static final int | PERMISSION_READ_ENCRYPTEDCharacteristic permission: Allow encrypted read operations |
public static final int | PERMISSION_READ_ENCRYPTED_MITMCharacteristic permission: Allow reading with man-in-the-middle protection |
public static final int | PERMISSION_WRITECharacteristic write permission |
public static final int | PERMISSION_WRITE_ENCRYPTEDCharacteristic permission: Allow encrypted writes |
public static final int | PERMISSION_WRITE_ENCRYPTED_MITMCharacteristic permission: Allow encrypted writes with man-in-the-middle
protection |
public static final int | PERMISSION_WRITE_SIGNEDCharacteristic permission: Allow signed write operations |
public static final int | PERMISSION_WRITE_SIGNED_MITMCharacteristic permission: Allow signed write operations with
man-in-the-middle protection |
public static final int | WRITE_TYPE_DEFAULTWrite characteristic, requesting acknoledgement by the remote device |
public static final int | WRITE_TYPE_NO_RESPONSEWrtite characteristic without requiring a response by the remote device |
public static final int | WRITE_TYPE_SIGNEDWrite characteristic including authentication signature |
public static final int | FORMAT_UINT8Characteristic value format type uint8 |
public static final int | FORMAT_UINT16Characteristic value format type uint16 |
public static final int | FORMAT_UINT32Characteristic value format type uint32 |
public static final int | FORMAT_SINT8Characteristic value format type sint8 |
public static final int | FORMAT_SINT16Characteristic value format type sint16 |
public static final int | FORMAT_SINT32Characteristic value format type sint32 |
public static final int | FORMAT_SFLOATCharacteristic value format type sfloat (16-bit float) |
public static final int | FORMAT_FLOATCharacteristic value format type float (32-bit float) |
protected UUID | mUuidThe UUID of this characteristic. |
protected int | mInstanceInstance ID for this characteristic. |
protected int | mPropertiesCharacteristic properties. |
protected int | mPermissionsCharacteristic permissions. |
protected int | mKeySizeKey size (default = 16). |
protected int | mWriteTypeWrite type for this characteristic.
See WRITE_TYPE_* constants. |
protected BluetoothGattService | mServiceBack-reference to the service this characteristic belongs to. |
protected byte[] | mValueThe cached value of this characteristic. |
protected List | mDescriptorsList of descriptors included in this characteristic. |
Methods Summary |
---|
public boolean | addDescriptor(BluetoothGattDescriptor descriptor)Adds a descriptor to this characteristic.
Requires {@link android.Manifest.permission#BLUETOOTH} permission.
mDescriptors.add(descriptor);
descriptor.setCharacteristic(this);
return true;
|
private float | bytesToFloat(byte b0, byte b1)Convert signed bytes to a 16-bit short float value.
int mantissa = unsignedToSigned(unsignedByteToInt(b0)
+ ((unsignedByteToInt(b1) & 0x0F) << 8), 12);
int exponent = unsignedToSigned(unsignedByteToInt(b1) >> 4, 4);
return (float)(mantissa * Math.pow(10, exponent));
|
private float | bytesToFloat(byte b0, byte b1, byte b2, byte b3)Convert signed bytes to a 32-bit short float value.
int mantissa = unsignedToSigned(unsignedByteToInt(b0)
+ (unsignedByteToInt(b1) << 8)
+ (unsignedByteToInt(b2) << 16), 24);
return (float)(mantissa * Math.pow(10, b3));
|
public BluetoothGattDescriptor | getDescriptor(java.util.UUID uuid)Returns a descriptor with a given UUID out of the list of
descriptors for this characteristic.
for(BluetoothGattDescriptor descriptor : mDescriptors) {
if (descriptor.getUuid().equals(uuid)) {
return descriptor;
}
}
return null;
|
BluetoothGattDescriptor | getDescriptor(java.util.UUID uuid, int instanceId)Get a descriptor by UUID and isntance id.
for(BluetoothGattDescriptor descriptor : mDescriptors) {
if (descriptor.getUuid().equals(uuid)
&& descriptor.getInstanceId() == instanceId) {
return descriptor;
}
}
return null;
|
public java.util.List | getDescriptors()Returns a list of descriptors for this characteristic.
return mDescriptors;
|
public java.lang.Float | getFloatValue(int formatType, int offset)Return the stored value of this characteristic.
See {@link #getValue} for details.
if ((offset + getTypeLen(formatType)) > mValue.length) return null;
switch (formatType) {
case FORMAT_SFLOAT:
return bytesToFloat(mValue[offset], mValue[offset+1]);
case FORMAT_FLOAT:
return bytesToFloat(mValue[offset], mValue[offset+1],
mValue[offset+2], mValue[offset+3]);
}
return null;
|
public int | getInstanceId()Returns the instance ID for this characteristic.
If a remote device offers multiple characteristics with the same UUID,
the instance ID is used to distuinguish between characteristics.
return mInstance;
|
public java.lang.Integer | getIntValue(int formatType, int offset)Return the stored value of this characteristic.
The formatType parameter determines how the characteristic value
is to be interpreted. For example, settting formatType to
{@link #FORMAT_UINT16} specifies that the first two bytes of the
characteristic value at the given offset are interpreted to generate the
return value.
if ((offset + getTypeLen(formatType)) > mValue.length) return null;
switch (formatType) {
case FORMAT_UINT8:
return unsignedByteToInt(mValue[offset]);
case FORMAT_UINT16:
return unsignedBytesToInt(mValue[offset], mValue[offset+1]);
case FORMAT_UINT32:
return unsignedBytesToInt(mValue[offset], mValue[offset+1],
mValue[offset+2], mValue[offset+3]);
case FORMAT_SINT8:
return unsignedToSigned(unsignedByteToInt(mValue[offset]), 8);
case FORMAT_SINT16:
return unsignedToSigned(unsignedBytesToInt(mValue[offset],
mValue[offset+1]), 16);
case FORMAT_SINT32:
return unsignedToSigned(unsignedBytesToInt(mValue[offset],
mValue[offset+1], mValue[offset+2], mValue[offset+3]), 32);
}
return null;
|
int | getKeySize()Returns the deisred key size.
return mKeySize;
|
public int | getPermissions()Returns the permissions for this characteristic.
return mPermissions;
|
public int | getProperties()Returns the properties of this characteristic.
The properties contain a bit mask of property flags indicating
the features of this characteristic.
return mProperties;
|
public BluetoothGattService | getService()Returns the service this characteristic belongs to.
return mService;
|
public java.lang.String | getStringValue(int offset)Return the stored value of this characteristic.
See {@link #getValue} for details.
if (mValue == null || offset > mValue.length) return null;
byte[] strBytes = new byte[mValue.length - offset];
for (int i=0; i != (mValue.length-offset); ++i) strBytes[i] = mValue[offset+i];
return new String(strBytes);
|
private int | getTypeLen(int formatType)Returns the size of a give value type.
return formatType & 0xF;
|
public java.util.UUID | getUuid()Returns the UUID of this characteristic
return mUuid;
|
public byte[] | getValue()Get the stored value for this characteristic.
This function returns the stored value for this characteristic as
retrieved by calling {@link BluetoothGatt#readCharacteristic}. The cached
value of the characteristic is updated as a result of a read characteristic
operation or if a characteristic update notification has been received.
return mValue;
|
public int | getWriteType()Gets the write type for this characteristic.
return mWriteType;
|
private void | initCharacteristic(BluetoothGattService service, java.util.UUID uuid, int instanceId, int properties, int permissions)
mUuid = uuid;
mInstance = instanceId;
mProperties = properties;
mPermissions = permissions;
mService = service;
mValue = null;
mDescriptors = new ArrayList<BluetoothGattDescriptor>();
if ((mProperties & PROPERTY_WRITE_NO_RESPONSE) != 0) {
mWriteType = WRITE_TYPE_NO_RESPONSE;
} else {
mWriteType = WRITE_TYPE_DEFAULT;
}
|
private int | intToSignedBits(int i, int size)Convert an integer into the signed bits of a given length.
if (i < 0) {
i = (1 << size-1) + (i & ((1 << size-1) - 1));
}
return i;
|
public void | setKeySize(int keySize)Set the desired key size.
mKeySize = keySize;
|
void | setService(BluetoothGattService service)Sets the service associated with this device.
mService = service;
|
public boolean | setValue(byte[] value)Updates the locally stored value of this characteristic.
This function modifies the locally stored cached value of this
characteristic. To send the value to the remote device, call
{@link BluetoothGatt#writeCharacteristic} to send the value to the
remote device.
mValue = value;
return true;
|
public boolean | setValue(int value, int formatType, int offset)Set the locally stored value of this characteristic.
See {@link #setValue(byte[])} for details.
int len = offset + getTypeLen(formatType);
if (mValue == null) mValue = new byte[len];
if (len > mValue.length) return false;
switch (formatType) {
case FORMAT_SINT8:
value = intToSignedBits(value, 8);
// Fall-through intended
case FORMAT_UINT8:
mValue[offset] = (byte)(value & 0xFF);
break;
case FORMAT_SINT16:
value = intToSignedBits(value, 16);
// Fall-through intended
case FORMAT_UINT16:
mValue[offset++] = (byte)(value & 0xFF);
mValue[offset] = (byte)((value >> 8) & 0xFF);
break;
case FORMAT_SINT32:
value = intToSignedBits(value, 32);
// Fall-through intended
case FORMAT_UINT32:
mValue[offset++] = (byte)(value & 0xFF);
mValue[offset++] = (byte)((value >> 8) & 0xFF);
mValue[offset++] = (byte)((value >> 16) & 0xFF);
mValue[offset] = (byte)((value >> 24) & 0xFF);
break;
default:
return false;
}
return true;
|
public boolean | setValue(int mantissa, int exponent, int formatType, int offset)Set the locally stored value of this characteristic.
See {@link #setValue(byte[])} for details.
int len = offset + getTypeLen(formatType);
if (mValue == null) mValue = new byte[len];
if (len > mValue.length) return false;
switch (formatType) {
case FORMAT_SFLOAT:
mantissa = intToSignedBits(mantissa, 12);
exponent = intToSignedBits(exponent, 4);
mValue[offset++] = (byte)(mantissa & 0xFF);
mValue[offset] = (byte)((mantissa >> 8) & 0x0F);
mValue[offset] += (byte)((exponent & 0x0F) << 4);
break;
case FORMAT_FLOAT:
mantissa = intToSignedBits(mantissa, 24);
exponent = intToSignedBits(exponent, 8);
mValue[offset++] = (byte)(mantissa & 0xFF);
mValue[offset++] = (byte)((mantissa >> 8) & 0xFF);
mValue[offset++] = (byte)((mantissa >> 16) & 0xFF);
mValue[offset] += (byte)(exponent & 0xFF);
break;
default:
return false;
}
return true;
|
public boolean | setValue(java.lang.String value)Set the locally stored value of this characteristic.
See {@link #setValue(byte[])} for details.
mValue = value.getBytes();
return true;
|
public void | setWriteType(int writeType)Set the write type for this characteristic
Setting the write type of a characteristic determines how the
{@link BluetoothGatt#writeCharacteristic} function write this
characteristic.
mWriteType = writeType;
|
private int | unsignedByteToInt(byte b)Convert a signed byte to an unsigned int.
return b & 0xFF;
|
private int | unsignedBytesToInt(byte b0, byte b1)Convert signed bytes to a 16-bit unsigned int.
return (unsignedByteToInt(b0) + (unsignedByteToInt(b1) << 8));
|
private int | unsignedBytesToInt(byte b0, byte b1, byte b2, byte b3)Convert signed bytes to a 32-bit unsigned int.
return (unsignedByteToInt(b0) + (unsignedByteToInt(b1) << 8))
+ (unsignedByteToInt(b2) << 16) + (unsignedByteToInt(b3) << 24);
|
private int | unsignedToSigned(int unsigned, int size)Convert an unsigned integer value to a two's-complement encoded
signed value.
if ((unsigned & (1 << size-1)) != 0) {
unsigned = -1 * ((1 << size-1) - (unsigned & ((1 << size-1) - 1)));
}
return unsigned;
|