FileDocCategorySizeDatePackage
BluetoothGattCharacteristic.javaAPI DocAndroid 5.1 API21578Thu Mar 12 22:22:10 GMT 2015android.bluetooth

BluetoothGattCharacteristic

public class BluetoothGattCharacteristic extends Object
Represents a Bluetooth GATT Characteristic

A GATT characteristic is a basic data element used to construct a GATT service, {@link BluetoothGattService}. The characteristic contains a value as well as additional information and optional GATT descriptors, {@link BluetoothGattDescriptor}.

Fields Summary
public static final int
PROPERTY_BROADCAST
Characteristic proprty: Characteristic is broadcastable.
public static final int
PROPERTY_READ
Characteristic property: Characteristic is readable.
public static final int
PROPERTY_WRITE_NO_RESPONSE
Characteristic property: Characteristic can be written without response.
public static final int
PROPERTY_WRITE
Characteristic property: Characteristic can be written.
public static final int
PROPERTY_NOTIFY
Characteristic property: Characteristic supports notification
public static final int
PROPERTY_INDICATE
Characteristic property: Characteristic supports indication
public static final int
PROPERTY_SIGNED_WRITE
Characteristic property: Characteristic supports write with signature
public static final int
PROPERTY_EXTENDED_PROPS
Characteristic property: Characteristic has extended properties
public static final int
PERMISSION_READ
Characteristic read permission
public static final int
PERMISSION_READ_ENCRYPTED
Characteristic permission: Allow encrypted read operations
public static final int
PERMISSION_READ_ENCRYPTED_MITM
Characteristic permission: Allow reading with man-in-the-middle protection
public static final int
PERMISSION_WRITE
Characteristic write permission
public static final int
PERMISSION_WRITE_ENCRYPTED
Characteristic permission: Allow encrypted writes
public static final int
PERMISSION_WRITE_ENCRYPTED_MITM
Characteristic permission: Allow encrypted writes with man-in-the-middle protection
public static final int
PERMISSION_WRITE_SIGNED
Characteristic permission: Allow signed write operations
public static final int
PERMISSION_WRITE_SIGNED_MITM
Characteristic permission: Allow signed write operations with man-in-the-middle protection
public static final int
WRITE_TYPE_DEFAULT
Write characteristic, requesting acknoledgement by the remote device
public static final int
WRITE_TYPE_NO_RESPONSE
Wrtite characteristic without requiring a response by the remote device
public static final int
WRITE_TYPE_SIGNED
Write characteristic including authentication signature
public static final int
FORMAT_UINT8
Characteristic value format type uint8
public static final int
FORMAT_UINT16
Characteristic value format type uint16
public static final int
FORMAT_UINT32
Characteristic value format type uint32
public static final int
FORMAT_SINT8
Characteristic value format type sint8
public static final int
FORMAT_SINT16
Characteristic value format type sint16
public static final int
FORMAT_SINT32
Characteristic value format type sint32
public static final int
FORMAT_SFLOAT
Characteristic value format type sfloat (16-bit float)
public static final int
FORMAT_FLOAT
Characteristic value format type float (32-bit float)
protected UUID
mUuid
The UUID of this characteristic.
protected int
mInstance
Instance ID for this characteristic.
protected int
mProperties
Characteristic properties.
protected int
mPermissions
Characteristic permissions.
protected int
mKeySize
Key size (default = 16).
protected int
mWriteType
Write type for this characteristic. See WRITE_TYPE_* constants.
protected BluetoothGattService
mService
Back-reference to the service this characteristic belongs to.
protected byte[]
mValue
The cached value of this characteristic.
protected List
mDescriptors
List of descriptors included in this characteristic.
Constructors Summary
public BluetoothGattCharacteristic(UUID uuid, int properties, int permissions)
Create a new BluetoothGattCharacteristic.

Requires {@link android.Manifest.permission#BLUETOOTH} permission.

param
uuid The UUID for this characteristic
param
properties Properties of this characteristic
param
permissions Permissions for this characteristic


                                    
           
        initCharacteristic(null, uuid, 0, properties, permissions);
    
BluetoothGattCharacteristic(BluetoothGattService service, UUID uuid, int instanceId, int properties, int permissions)
Create a new BluetoothGattCharacteristic

hide

        initCharacteristic(service, uuid, instanceId, properties, permissions);
    
Methods Summary
public booleanaddDescriptor(BluetoothGattDescriptor descriptor)
Adds a descriptor to this characteristic.

Requires {@link android.Manifest.permission#BLUETOOTH} permission.

param
descriptor Descriptor to be added to this characteristic.
return
true, if the descriptor was added to the characteristic

        mDescriptors.add(descriptor);
        descriptor.setCharacteristic(this);
        return true;
    
private floatbytesToFloat(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 floatbytesToFloat(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 BluetoothGattDescriptorgetDescriptor(java.util.UUID uuid)
Returns a descriptor with a given UUID out of the list of descriptors for this characteristic.

return
GATT descriptor object or null if no descriptor with the given UUID was found.

        for(BluetoothGattDescriptor descriptor : mDescriptors) {
            if (descriptor.getUuid().equals(uuid)) {
                return descriptor;
            }
        }
        return null;
    
BluetoothGattDescriptorgetDescriptor(java.util.UUID uuid, int instanceId)
Get a descriptor by UUID and isntance id.

hide

        for(BluetoothGattDescriptor descriptor : mDescriptors) {
            if (descriptor.getUuid().equals(uuid)
             && descriptor.getInstanceId() == instanceId) {
                return descriptor;
            }
        }
        return null;
    
public java.util.ListgetDescriptors()
Returns a list of descriptors for this characteristic.

return
Descriptors for this characteristic

        return mDescriptors;
    
public java.lang.FloatgetFloatValue(int formatType, int offset)
Return the stored value of this characteristic.

See {@link #getValue} for details.

param
formatType The format type used to interpret the characteristic value.
param
offset Offset at which the float value can be found.
return
Cached value of the characteristic at a given offset or null if the requested offset exceeds the value size.

        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 intgetInstanceId()
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
Instance ID of this characteristic

        return mInstance;
    
public java.lang.IntegergetIntValue(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.

param
formatType The format type used to interpret the characteristic value.
param
offset Offset at which the integer value can be found.
return
Cached value of the characteristic or null of offset exceeds value size.

        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;
    
intgetKeySize()
Returns the deisred key size.

hide

        return mKeySize;
    
public intgetPermissions()
Returns the permissions for this characteristic.

return
Permissions of this characteristic

        return mPermissions;
    
public intgetProperties()
Returns the properties of this characteristic.

The properties contain a bit mask of property flags indicating the features of this characteristic.

return
Properties of this characteristic

        return mProperties;
    
public BluetoothGattServicegetService()
Returns the service this characteristic belongs to.

return
The asscociated service

        return mService;
    
public java.lang.StringgetStringValue(int offset)
Return the stored value of this characteristic.

See {@link #getValue} for details.

param
offset Offset at which the string value can be found.
return
Cached value of the characteristic

        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 intgetTypeLen(int formatType)
Returns the size of a give value type.

        return formatType & 0xF;
    
public java.util.UUIDgetUuid()
Returns the UUID of this characteristic

return
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
Cached value of the characteristic

        return mValue;
    
public intgetWriteType()
Gets the write type for this characteristic.

return
Write type for this characteristic

        return mWriteType;
    
private voidinitCharacteristic(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 intintToSignedBits(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 voidsetKeySize(int keySize)
Set the desired key size.

hide

        mKeySize = keySize;
    
voidsetService(BluetoothGattService service)
Sets the service associated with this device.

hide

        mService = service;
    
public booleansetValue(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.

param
value New value for this characteristic
return
true if the locally stored value has been set, false if the requested value could not be stored locally.

        mValue = value;
        return true;
    
public booleansetValue(int value, int formatType, int offset)
Set the locally stored value of this characteristic.

See {@link #setValue(byte[])} for details.

param
value New value for this characteristic
param
formatType Integer format type used to transform the value parameter
param
offset Offset at which the value should be placed
return
true if the locally stored value has been set

        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 booleansetValue(int mantissa, int exponent, int formatType, int offset)
Set the locally stored value of this characteristic.

See {@link #setValue(byte[])} for details.

param
mantissa Mantissa for this characteristic
param
exponent exponent value for this characteristic
param
formatType Float format type used to transform the value parameter
param
offset Offset at which the value should be placed
return
true if the locally stored value has been set

        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 booleansetValue(java.lang.String value)
Set the locally stored value of this characteristic.

See {@link #setValue(byte[])} for details.

param
value New value for this characteristic
return
true if the locally stored value has been set

        mValue = value.getBytes();
        return true;
    
public voidsetWriteType(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.

param
writeType The write type to for this characteristic. Can be one of: {@link #WRITE_TYPE_DEFAULT}, {@link #WRITE_TYPE_NO_RESPONSE} or {@link #WRITE_TYPE_SIGNED}.

        mWriteType = writeType;
    
private intunsignedByteToInt(byte b)
Convert a signed byte to an unsigned int.

        return b & 0xFF;
    
private intunsignedBytesToInt(byte b0, byte b1)
Convert signed bytes to a 16-bit unsigned int.

        return (unsignedByteToInt(b0) + (unsignedByteToInt(b1) << 8));
    
private intunsignedBytesToInt(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 intunsignedToSigned(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;