Java Code Examples for android.bluetooth.BluetoothGattCharacteristic#FORMAT_SFLOAT

The following examples show how to use android.bluetooth.BluetoothGattCharacteristic#FORMAT_SFLOAT . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BluetoothLEGatt.java    From EFRConnect-android with Apache License 2.0 5 votes vote down vote up
private static Object getCharacteristicsValue(BluetoothGattCharacteristic gattCharacteristic) {
    if (gattCharacteristic.getValue() == null) {
        return null;
    }

    GattCharacteristic characteristic = GATT_CHARACTER_DESCS.get(getIdentification(gattCharacteristic.getUuid()));
    if (characteristic == null) {
        return gattCharacteristic.getValue();
    }

    final int format = characteristic.format;
    switch (format) {
        case BluetoothGattCharacteristic.FORMAT_UINT8:
        case BluetoothGattCharacteristic.FORMAT_UINT16:
        case BluetoothGattCharacteristic.FORMAT_UINT32:
        case BluetoothGattCharacteristic.FORMAT_SINT8:
        case BluetoothGattCharacteristic.FORMAT_SINT16:
        case BluetoothGattCharacteristic.FORMAT_SINT32:
            return gattCharacteristic.getIntValue(format, 0);

        case BluetoothGattCharacteristic.FORMAT_FLOAT:
        case BluetoothGattCharacteristic.FORMAT_SFLOAT:
            return gattCharacteristic.getFloatValue(format, 0);

        case 0:
            final String value = gattCharacteristic.getStringValue(0);
            final int firstNullCharPos = value.indexOf('\u0000');
            return (firstNullCharPos >= 0) ? value.substring(0, firstNullCharPos) : value;

        default:
            return characteristic.createValue(gattCharacteristic);
    }
}
 
Example 2
Source File: ParserUtils.java    From Android-nRF-Beacon-for-Eddystone with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static int setValue(final byte[] dest, int offset, int mantissa, int exponent, int formatType) {
    int len = offset + getTypeLen(formatType);
    if (len > dest.length)
        return offset;

    switch (formatType) {
        case BluetoothGattCharacteristic.FORMAT_SFLOAT:
            mantissa = intToSignedBits(mantissa, 12);
            exponent = intToSignedBits(exponent, 4);
            dest[offset++] = (byte) (mantissa & 0xFF);
            dest[offset] = (byte) ((mantissa >> 8) & 0x0F);
            dest[offset] += (byte) ((exponent & 0x0F) << 4);
            break;

        case BluetoothGattCharacteristic.FORMAT_FLOAT:
            mantissa = intToSignedBits(mantissa, 24);
            exponent = intToSignedBits(exponent, 8);
            dest[offset++] = (byte) (mantissa & 0xFF);
            dest[offset++] = (byte) ((mantissa >> 8) & 0xFF);
            dest[offset++] = (byte) ((mantissa >> 16) & 0xFF);
            dest[offset] += (byte) (exponent & 0xFF);
            break;

        default:
            return offset;
    }

    return len;
}
 
Example 3
Source File: ParserUtils.java    From Android-nRF-Beacon-for-Eddystone with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static int getMantissa(final byte[] source, final int offset, final int formatType) {
    if ((offset + getTypeLen(formatType)) > source.length)
        throw new ArrayIndexOutOfBoundsException();

    switch (formatType) {
        case BluetoothGattCharacteristic.FORMAT_SFLOAT:
            return unsignedToSigned(unsignedByteToInt(source[offset]) + ((unsignedByteToInt(source[offset + 1]) & 0x0F) << 8), 12);
        case BluetoothGattCharacteristic.FORMAT_FLOAT:
            return unsignedToSigned(unsignedByteToInt(source[offset]) + (unsignedByteToInt(source[offset + 1]) << 8) + (unsignedByteToInt(source[offset + 2]) << 16), 24);
    }
    return 0;
}
 
Example 4
Source File: ParserUtils.java    From Android-nRF-Beacon-for-Eddystone with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static int getExponent(final byte[] source, final int offset, final int formatType) {
    if ((offset + getTypeLen(formatType)) > source.length)
        throw new ArrayIndexOutOfBoundsException();

    switch (formatType) {
        case BluetoothGattCharacteristic.FORMAT_SFLOAT:
            return unsignedToSigned(unsignedByteToInt(source[offset + 1]) >> 4, 4);
        case BluetoothGattCharacteristic.FORMAT_FLOAT:
            return source[offset + 3];
    }
    return 0;
}
 
Example 5
Source File: ByteUtil.java    From EasyBle with Apache License 2.0 5 votes vote down vote up
/**
 * Return the stored value of this characteristic.
 * <p>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.
 */
public static Float getFloatValue(byte[] data, int formatType, int offset) {
    if ((offset + getTypeLen(formatType)) > data.length) return null;

    switch (formatType) {
        case BluetoothGattCharacteristic.FORMAT_SFLOAT:
            return bytesToFloat(data[offset], data[offset+1]);

        case BluetoothGattCharacteristic.FORMAT_FLOAT:
            return bytesToFloat(data[offset],   data[offset+1],
                    data[offset+2], data[offset+3]);
    }

    return null;
}
 
Example 6
Source File: CharacteristicDetailsActivity.java    From BLEService with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public int getValueFormat(BluetoothGattCharacteristic ch) {
	int properties = ch.getProperties();
	
	if((BluetoothGattCharacteristic.FORMAT_FLOAT & properties) != 0) return BluetoothGattCharacteristic.FORMAT_FLOAT;
	if((BluetoothGattCharacteristic.FORMAT_SFLOAT & properties) != 0) return BluetoothGattCharacteristic.FORMAT_SFLOAT;
	if((BluetoothGattCharacteristic.FORMAT_SINT16 & properties) != 0) return BluetoothGattCharacteristic.FORMAT_SINT16;
	if((BluetoothGattCharacteristic.FORMAT_SINT32 & properties) != 0) return BluetoothGattCharacteristic.FORMAT_SINT32;
	if((BluetoothGattCharacteristic.FORMAT_SINT8 & properties) != 0) return BluetoothGattCharacteristic.FORMAT_SINT8;
	if((BluetoothGattCharacteristic.FORMAT_UINT16 & properties) != 0) return BluetoothGattCharacteristic.FORMAT_UINT16;
	if((BluetoothGattCharacteristic.FORMAT_UINT32 & properties) != 0) return BluetoothGattCharacteristic.FORMAT_UINT32;
	if((BluetoothGattCharacteristic.FORMAT_UINT8 & properties) != 0) return BluetoothGattCharacteristic.FORMAT_UINT8;
	
	return 0;
}
 
Example 7
Source File: BleWrapper.java    From BLEService with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public int getValueFormat(BluetoothGattCharacteristic ch) {
	int properties = ch.getProperties();
	
	if((BluetoothGattCharacteristic.FORMAT_FLOAT & properties) != 0) return BluetoothGattCharacteristic.FORMAT_FLOAT;
	if((BluetoothGattCharacteristic.FORMAT_SFLOAT & properties) != 0) return BluetoothGattCharacteristic.FORMAT_SFLOAT;
	if((BluetoothGattCharacteristic.FORMAT_SINT16 & properties) != 0) return BluetoothGattCharacteristic.FORMAT_SINT16;
	if((BluetoothGattCharacteristic.FORMAT_SINT32 & properties) != 0) return BluetoothGattCharacteristic.FORMAT_SINT32;
	if((BluetoothGattCharacteristic.FORMAT_SINT8 & properties) != 0) return BluetoothGattCharacteristic.FORMAT_SINT8;
	if((BluetoothGattCharacteristic.FORMAT_UINT16 & properties) != 0) return BluetoothGattCharacteristic.FORMAT_UINT16;
	if((BluetoothGattCharacteristic.FORMAT_UINT32 & properties) != 0) return BluetoothGattCharacteristic.FORMAT_UINT32;
	if((BluetoothGattCharacteristic.FORMAT_UINT8 & properties) != 0) return BluetoothGattCharacteristic.FORMAT_UINT8;
	
	return 0;
}