Java Code Examples for android.bluetooth.BluetoothGattCharacteristic#FORMAT_UINT8

The following examples show how to use android.bluetooth.BluetoothGattCharacteristic#FORMAT_UINT8 . 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: BleHeartRateSensor.java    From BLE-Heart-rate-variability-demo with MIT License 6 votes vote down vote up
private static double extractHeartRate(
		BluetoothGattCharacteristic characteristic) {

	int flag = characteristic.getProperties();
	Log.d(TAG, "Heart rate flag: " + flag);
	int format = -1;
	// Heart rate bit number format
	if ((flag & 0x01) != 0) {
		format = BluetoothGattCharacteristic.FORMAT_UINT16;
		Log.d(TAG, "Heart rate format UINT16.");
	} else {
		format = BluetoothGattCharacteristic.FORMAT_UINT8;
		Log.d(TAG, "Heart rate format UINT8.");
	}
	final int heartRate = characteristic.getIntValue(format, 1);
	Log.d(TAG, String.format("Received heart rate: %d", heartRate));
	return heartRate;
}
 
Example 2
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;
}
 
Example 3
Source File: BluetoothLeService.java    From connectivity-samples with Apache License 2.0 5 votes vote down vote up
private void broadcastUpdate(final String action,
                             final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);

    // This is special handling for the Heart Rate Measurement profile.  Data parsing is
    // carried out as per profile specifications:
    // http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
    if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
        int flag = characteristic.getProperties();
        int format = -1;
        if ((flag & 0x01) != 0) {
            format = BluetoothGattCharacteristic.FORMAT_UINT16;
            Log.d(TAG, "Heart rate format UINT16.");
        } else {
            format = BluetoothGattCharacteristic.FORMAT_UINT8;
            Log.d(TAG, "Heart rate format UINT8.");
        }
        final int heartRate = characteristic.getIntValue(format, 1);
        Log.d(TAG, String.format("Received heart rate: %d", heartRate));
        intent.putExtra(EXTRA_DATA, String.valueOf(heartRate));
    } else {
        // For all other profiles, writes the data formatted in HEX.
        final byte[] data = characteristic.getValue();
        if (data != null && data.length > 0) {
            final StringBuilder stringBuilder = new StringBuilder(data.length);
            for(byte byteChar : data)
                stringBuilder.append(String.format("%02X ", byteChar));
            intent.putExtra(EXTRA_DATA, new String(data) + "\n" + stringBuilder.toString());
        }
    }
    sendBroadcast(intent);
}
 
Example 4
Source File: BluetoothLeService.java    From BLEConnect with GNU General Public License v2.0 5 votes vote down vote up
private void broadcastUpdate(final String action,
		final BluetoothGattCharacteristic characteristic) {
	final Intent intent = new Intent(action);

	// This is special handling for the Heart Rate Measurement profile. Data
	// parsing is
	// carried out as per profile specifications:
	// http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
	if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
		int flag = characteristic.getProperties();
		int format = -1;
		if ((flag & 0x01) != 0) {
			format = BluetoothGattCharacteristic.FORMAT_UINT16;
			Log.d(TAG, "Heart rate format UINT16.");
		} else {
			format = BluetoothGattCharacteristic.FORMAT_UINT8;
			Log.d(TAG, "Heart rate format UINT8.");
		}
		final int heartRate = characteristic.getIntValue(format, 1);
		Log.d(TAG, String.format("Received heart rate: %d", heartRate));
		intent.putExtra(EXTRA_DATA, String.valueOf(heartRate));
	} else {
		// For all other profiles, writes the data formatted in HEX.
		final byte[] data = characteristic.getValue();
		if (data != null && data.length > 0) {
			final StringBuilder stringBuilder = new StringBuilder(
					data.length);
			for (byte byteChar : data)
				stringBuilder.append(String.format("%02X ", byteChar));
			intent.putExtra(EXTRA_DATA, new String(data) + "\n"
					+ stringBuilder.toString());
		}
	}
	sendBroadcast(intent);
}
 
Example 5
Source File: BluetoothLeService.java    From android-BluetoothLeGatt with Apache License 2.0 5 votes vote down vote up
private void broadcastUpdate(final String action,
                             final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);

    // This is special handling for the Heart Rate Measurement profile.  Data parsing is
    // carried out as per profile specifications:
    // http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
    if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
        int flag = characteristic.getProperties();
        int format = -1;
        if ((flag & 0x01) != 0) {
            format = BluetoothGattCharacteristic.FORMAT_UINT16;
            Log.d(TAG, "Heart rate format UINT16.");
        } else {
            format = BluetoothGattCharacteristic.FORMAT_UINT8;
            Log.d(TAG, "Heart rate format UINT8.");
        }
        final int heartRate = characteristic.getIntValue(format, 1);
        Log.d(TAG, String.format("Received heart rate: %d", heartRate));
        intent.putExtra(EXTRA_DATA, String.valueOf(heartRate));
    } else {
        // For all other profiles, writes the data formatted in HEX.
        final byte[] data = characteristic.getValue();
        if (data != null && data.length > 0) {
            final StringBuilder stringBuilder = new StringBuilder(data.length);
            for(byte byteChar : data)
                stringBuilder.append(String.format("%02X ", byteChar));
            intent.putExtra(EXTRA_DATA, new String(data) + "\n" + stringBuilder.toString());
        }
    }
    sendBroadcast(intent);
}
 
Example 6
Source File: BluetoothLeService.java    From IoT-Firstep with GNU General Public License v3.0 5 votes vote down vote up
private void broadcastUpdate(final String action,
                             final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);

    // This is special handling for the Heart Rate Measurement profile.  Data parsing is
    // carried out as per profile specifications:
    // http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
    if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
        int flag = characteristic.getProperties();
        int format = -1;
        if ((flag & 0x01) != 0) {
            format = BluetoothGattCharacteristic.FORMAT_UINT16;
            Log.d(TAG, "Heart rate format UINT16.");
        } else {
            format = BluetoothGattCharacteristic.FORMAT_UINT8;
            Log.d(TAG, "Heart rate format UINT8.");
        }
        final int heartRate = characteristic.getIntValue(format, 1);
        Log.d(TAG, String.format("Received heart rate: %d", heartRate));
        intent.putExtra(EXTRA_DATA, String.valueOf(heartRate));
    } else {
        // For all other profiles, writes the data formatted in HEX.
        final byte[] data = characteristic.getValue();
        //System.out.println("LeService ---->" + new String(data));
        if (data != null && data.length > 0) {
            intent.putExtra(EXTRA_DATA, data);
        }
    }
    sendBroadcast(intent);
}
 
Example 7
Source File: BluetoothLeService.java    From BLEConnect with GNU General Public License v2.0 5 votes vote down vote up
private void broadcastUpdate(final String action,
		final BluetoothGattCharacteristic characteristic) {
	final Intent intent = new Intent(action);

	// This is special handling for the Heart Rate Measurement profile. Data
	// parsing is
	// carried out as per profile specifications:
	// http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
	if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
		int flag = characteristic.getProperties();
		int format = -1;
		if ((flag & 0x01) != 0) {
			format = BluetoothGattCharacteristic.FORMAT_UINT16;
			Log.d(TAG, "Heart rate format UINT16.");
		} else {
			format = BluetoothGattCharacteristic.FORMAT_UINT8;
			Log.d(TAG, "Heart rate format UINT8.");
		}
		final int heartRate = characteristic.getIntValue(format, 1);
		Log.d(TAG, String.format("Received heart rate: %d", heartRate));
		intent.putExtra(EXTRA_DATA, String.valueOf(heartRate));
	} else {
		// For all other profiles, writes the data formatted in HEX.
		final byte[] data = characteristic.getValue();
		if (data != null && data.length > 0) {
			final StringBuilder stringBuilder = new StringBuilder(
					data.length);
			for (byte byteChar : data)
				stringBuilder.append(String.format("%02X ", byteChar));
			intent.putExtra(EXTRA_DATA, new String(data) + "\n"
					+ stringBuilder.toString());
		}
	}
	sendBroadcast(intent);
}
 
Example 8
Source File: BluetoothLeService.java    From IoT-Firstep with GNU General Public License v3.0 5 votes vote down vote up
private void broadcastUpdate(final String action,
                             final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);

    // This is special handling for the Heart Rate Measurement profile.  Data parsing is
    // carried out as per profile specifications:
    // http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
    if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
        int flag = characteristic.getProperties();
        int format = -1;
        if ((flag & 0x01) != 0) {
            format = BluetoothGattCharacteristic.FORMAT_UINT16;
            Log.d(TAG, "Heart rate format UINT16.");
        } else {
            format = BluetoothGattCharacteristic.FORMAT_UINT8;
            Log.d(TAG, "Heart rate format UINT8.");
        }
        final int heartRate = characteristic.getIntValue(format, 1);
        Log.d(TAG, String.format("Received heart rate: %d", heartRate));
        intent.putExtra(EXTRA_DATA, String.valueOf(heartRate));
    } else {
        // For all other profiles, writes the data formatted in HEX.
        final byte[] data = characteristic.getValue();
        //System.out.println("LeService ---->" + new String(data));
        if (data != null && data.length > 0) {
            intent.putExtra(EXTRA_DATA, data);
        }
    }
    sendBroadcast(intent);
}
 
Example 9
Source File: BluetoothLeService.java    From IoT-Firstep with GNU General Public License v3.0 5 votes vote down vote up
private void broadcastUpdate(final String action,
                             final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);

    // This is special handling for the Heart Rate Measurement profile.  Data parsing is
    // carried out as per profile specifications:
    // http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
    if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
        int flag = characteristic.getProperties();
        int format = -1;
        if ((flag & 0x01) != 0) {
            format = BluetoothGattCharacteristic.FORMAT_UINT16;
            Log.d(TAG, "Heart rate format UINT16.");
        } else {
            format = BluetoothGattCharacteristic.FORMAT_UINT8;
            Log.d(TAG, "Heart rate format UINT8.");
        }
        final int heartRate = characteristic.getIntValue(format, 1);
        Log.d(TAG, String.format("Received heart rate: %d", heartRate));
        intent.putExtra(EXTRA_DATA, String.valueOf(heartRate));
    } else {
        // For all other profiles, writes the data formatted in HEX.
        final byte[] data = characteristic.getValue();
        //System.out.println("LeService ---->" + new String(data));
        if (data != null && data.length > 0) {
            intent.putExtra(EXTRA_DATA, data);
        }
    }
    sendBroadcast(intent);
}
 
Example 10
Source File: BluetoothLeService.java    From IoT-Firstep with GNU General Public License v3.0 5 votes vote down vote up
private void broadcastUpdate(final String action,
                             final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);

    // This is special handling for the Heart Rate Measurement profile.  Data parsing is
    // carried out as per profile specifications:
    // http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
    if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
        int flag = characteristic.getProperties();
        int format = -1;
        if ((flag & 0x01) != 0) {
            format = BluetoothGattCharacteristic.FORMAT_UINT16;
            Log.d(TAG, "Heart rate format UINT16.");
        } else {
            format = BluetoothGattCharacteristic.FORMAT_UINT8;
            Log.d(TAG, "Heart rate format UINT8.");
        }
        final int heartRate = characteristic.getIntValue(format, 1);
        Log.d(TAG, String.format("Received heart rate: %d", heartRate));
        intent.putExtra(EXTRA_DATA, String.valueOf(heartRate));
    } else {
        // For all other profiles, writes the data formatted in HEX.
        final byte[] data = characteristic.getValue();
        //System.out.println("LeService ---->" + new String(data));
        if (data != null && data.length > 0) {
            intent.putExtra(EXTRA_DATA, data);
        }
    }
    sendBroadcast(intent);
}
 
Example 11
Source File: BluetoothLeService.java    From IoT-Firstep with GNU General Public License v3.0 5 votes vote down vote up
private void broadcastUpdate(final String action,
                             final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);

    // This is special handling for the Heart Rate Measurement profile.  Data parsing is
    // carried out as per profile specifications:
    // http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
    if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
        int flag = characteristic.getProperties();
        int format = -1;
        if ((flag & 0x01) != 0) {
            format = BluetoothGattCharacteristic.FORMAT_UINT16;
            Log.d(TAG, "Heart rate format UINT16.");
        } else {
            format = BluetoothGattCharacteristic.FORMAT_UINT8;
            Log.d(TAG, "Heart rate format UINT8.");
        }
        final int heartRate = characteristic.getIntValue(format, 1);
        Log.d(TAG, String.format("Received heart rate: %d", heartRate));
        intent.putExtra(EXTRA_DATA, String.valueOf(heartRate));
    } else {
        // For all other profiles, writes the data formatted in HEX.
        final byte[] data = characteristic.getValue();
        //System.out.println("LeService ---->" + new String(data));
        if (data != null && data.length > 0) {
            intent.putExtra(EXTRA_DATA, data);
        }
    }
    sendBroadcast(intent);
}
 
Example 12
Source File: BluetoothLeService.java    From IoT-Firstep with GNU General Public License v3.0 5 votes vote down vote up
private void broadcastUpdate(final String action,
                             final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);

    // This is special handling for the Heart Rate Measurement profile.  Data parsing is
    // carried out as per profile specifications:
    // http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
    if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
        int flag = characteristic.getProperties();
        int format = -1;
        if ((flag & 0x01) != 0) {
            format = BluetoothGattCharacteristic.FORMAT_UINT16;
            Log.d(TAG, "Heart rate format UINT16.");
        } else {
            format = BluetoothGattCharacteristic.FORMAT_UINT8;
            Log.d(TAG, "Heart rate format UINT8.");
        }
        final int heartRate = characteristic.getIntValue(format, 1);
        Log.d(TAG, String.format("Received heart rate: %d", heartRate));
        intent.putExtra(EXTRA_DATA, String.valueOf(heartRate));
    } else {
        // For all other profiles, writes the data formatted in HEX.
        final byte[] data = characteristic.getValue();
        //System.out.println("LeService ---->" + new String(data));
        if (data != null && data.length > 0) {
            intent.putExtra(EXTRA_DATA, data);
        }
    }
    sendBroadcast(intent);
}
 
Example 13
Source File: BluetoothLeService.java    From IoT-Firstep with GNU General Public License v3.0 5 votes vote down vote up
private void broadcastUpdate(final String action,
                             final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);

    // This is special handling for the Heart Rate Measurement profile.  Data parsing is
    // carried out as per profile specifications:
    // http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
    if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
        int flag = characteristic.getProperties();
        int format = -1;
        if ((flag & 0x01) != 0) {
            format = BluetoothGattCharacteristic.FORMAT_UINT16;
            Log.d(TAG, "Heart rate format UINT16.");
        } else {
            format = BluetoothGattCharacteristic.FORMAT_UINT8;
            Log.d(TAG, "Heart rate format UINT8.");
        }
        final int heartRate = characteristic.getIntValue(format, 1);
        Log.d(TAG, String.format("Received heart rate: %d", heartRate));
        intent.putExtra(EXTRA_DATA, String.valueOf(heartRate));
    } else {
        // For all other profiles, writes the data formatted in HEX.
        final byte[] data = characteristic.getValue();
        //System.out.println("LeService ---->" + new String(data));
        if (data != null && data.length > 0) {
            intent.putExtra(EXTRA_DATA, data);
        }
    }
    sendBroadcast(intent);
}
 
Example 14
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>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.
 */
public static Integer getIntValue(byte[] data, int formatType, int offset) {
    if ((offset + getTypeLen(formatType)) > data.length) return null;

    switch (formatType) {
        case BluetoothGattCharacteristic.FORMAT_UINT8:
            return unsignedByteToInt(data[offset]);

        case BluetoothGattCharacteristic.FORMAT_UINT16:
            return unsignedBytesToInt(data[offset], data[offset+1]);

        case BluetoothGattCharacteristic.FORMAT_UINT32:
            return unsignedBytesToInt(data[offset],   data[offset+1],
                    data[offset+2], data[offset+3]);
        case BluetoothGattCharacteristic.FORMAT_SINT8:
            return unsignedToSigned(unsignedByteToInt(data[offset]), 8);

        case BluetoothGattCharacteristic.FORMAT_SINT16:
            return unsignedToSigned(unsignedBytesToInt(data[offset],
                    data[offset+1]), 16);

        case BluetoothGattCharacteristic.FORMAT_SINT32:
            return unsignedToSigned(unsignedBytesToInt(data[offset],
                    data[offset+1], data[offset+2], data[offset+3]), 32);
    }

    return null;
}
 
Example 15
Source File: HRDemoActivity.java    From BLEService with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void getAndDisplayHrValue() {
   	byte[] raw = mBTValueCharacteristic.getValue();
   	int index = ((raw[0] & 0x01) == 1) ? 2 : 1;
   	int format = (index == 1) ? BluetoothGattCharacteristic.FORMAT_UINT8 : BluetoothGattCharacteristic.FORMAT_UINT16;
   	int value = mBTValueCharacteristic.getIntValue(format, index);
   	final String description = value + " bpm";

   	runOnUiThread(new Runnable() {
		@Override
		public void run() {
			mTextView.setText(description);
		}
   	});
}
 
Example 16
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 getIntValue(final byte[] source, final int offset, final int formatType) {
    if ((offset + getTypeLen(formatType)) > source.length)
        throw new ArrayIndexOutOfBoundsException();

    switch (formatType) {
        case BluetoothGattCharacteristic.FORMAT_UINT8:
            return unsignedByteToInt(source[offset]);

        case BluetoothGattCharacteristic.FORMAT_UINT16:
            return unsignedBytesToInt(source[offset], source[offset + 1]);

        case FORMAT_UINT24:
            return unsignedBytesToInt(source[offset], source[offset + 1], source[offset + 2]);

        case BluetoothGattCharacteristic.FORMAT_UINT32:
            return unsignedBytesToInt(source[offset], source[offset + 1], source[offset + 2], source[offset + 3]);

        case FORMAT_UINT16_BIG_INDIAN:
            return unsignedBytesToInt(source[offset + 1], source[offset]);

        case FORMAT_UINT32_BIG_INDIAN:
            return unsignedBytesToInt(source[offset + 3], source[offset + 2], source[offset + 1], source[offset]);

        case BluetoothGattCharacteristic.FORMAT_SINT8:
            return unsignedToSigned(unsignedByteToInt(source[offset]), 8);

        case BluetoothGattCharacteristic.FORMAT_SINT16:
            return unsignedToSigned(unsignedBytesToInt(source[offset], source[offset + 1]), 16);

        case FORMAT_SINT24:
            return unsignedToSigned(unsignedBytesToInt(source[offset], source[offset + 1], source[offset + 2]), 24);

        case BluetoothGattCharacteristic.FORMAT_SINT32:
            return unsignedToSigned(unsignedBytesToInt(source[offset], source[offset + 1], source[offset + 2], source[offset + 3]), 32);
    }
    return 0;
}
 
Example 17
Source File: ParserUtils.java    From Android-nRF-Beacon-for-Eddystone with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static int setValue(final byte[] dest, int offset, int value, int formatType) {
    int len = offset + getTypeLen(formatType);
    if (len > dest.length)
        return offset;

    switch (formatType) {
        case BluetoothGattCharacteristic.FORMAT_SINT8:
            value = intToSignedBits(value, 8);
            // Fall-through intended
        case BluetoothGattCharacteristic.FORMAT_UINT8:
            dest[offset] = (byte) (value & 0xFF);
            break;

        case BluetoothGattCharacteristic.FORMAT_SINT16:
            value = intToSignedBits(value, 16);
            // Fall-through intended
        case BluetoothGattCharacteristic.FORMAT_UINT16:
            dest[offset++] = (byte) (value & 0xFF);
            dest[offset] = (byte) ((value >> 8) & 0xFF);
            break;

        case FORMAT_SINT24:
            value = intToSignedBits(value, 24);
            // Fall-through intended
        case FORMAT_UINT24:
            dest[offset++] = (byte) (value & 0xFF);
            dest[offset++] = (byte) ((value >> 8) & 0xFF);
            dest[offset] = (byte) ((value >> 16) & 0xFF);
            break;

        case FORMAT_UINT16_BIG_INDIAN:
            dest[offset++] = (byte) ((value >> 8) & 0xFF);
            dest[offset] = (byte) (value & 0xFF);
            break;

        case BluetoothGattCharacteristic.FORMAT_SINT32:
            value = intToSignedBits(value, 32);
            // Fall-through intended
        case BluetoothGattCharacteristic.FORMAT_UINT32:
            dest[offset++] = (byte) (value & 0xFF);
            dest[offset++] = (byte) ((value >> 8) & 0xFF);
            dest[offset++] = (byte) ((value >> 16) & 0xFF);
            dest[offset] = (byte) ((value >> 24) & 0xFF);
            break;

        case FORMAT_UINT32_BIG_INDIAN:
            dest[offset++] = (byte) ((value >> 24) & 0xFF);
            dest[offset++] = (byte) ((value >> 16) & 0xFF);
            dest[offset++] = (byte) ((value >> 8) & 0xFF);
            dest[offset] = (byte) (value & 0xFF);
            break;

        default:
            return offset;
    }
    return len;
}
 
Example 18
Source File: CharacteristicDetailsActivity.java    From BLEService with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void getCharacteristicValue(BluetoothGattCharacteristic ch) {   
    byte[] rawValue = ch.getValue();
    String strValue = null;
    int intValue = 0;
    
    // lets read and do real parsing of some characteristic to get meaningful value from it 
    UUID uuid = ch.getUuid();
    
    if(uuid.equals(BleDefinedUUIDs.Characteristic.HEART_RATE_MEASUREMENT)) { // heart rate
    	// follow https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
    	// first check format used by the device - it is specified in bit 0 and tells us if we should ask for index 1 (and uint8) or index 2 (and uint16)
    	int index = ((rawValue[0] & 0x01) == 1) ? 2 : 1;
    	// also we need to define format
    	int format = (index == 1) ? BluetoothGattCharacteristic.FORMAT_UINT8 : BluetoothGattCharacteristic.FORMAT_UINT16;
    	// now we have everything, get the value
    	intValue = ch.getIntValue(format, index);
    	strValue = intValue + " bpm"; // it is always in bpm units
    }
    else if (uuid.equals(BleDefinedUUIDs.Characteristic.HEART_RATE_MEASUREMENT) || // manufacturer name string
    		 uuid.equals(BleDefinedUUIDs.Characteristic.MODEL_NUMBER_STRING) || // model number string)
    		 uuid.equals(BleDefinedUUIDs.Characteristic.FIRMWARE_REVISION_STRING)) // firmware revision string
    {
    	// follow https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.manufacturer_name_string.xml etc.
    	// string value are usually simple utf8s string at index 0
    	strValue = ch.getStringValue(0);
    }
    else if(uuid.equals(BleDefinedUUIDs.Characteristic.APPEARANCE)) { // appearance
    	// follow: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.gap.appearance.xml
    	intValue  = ((int)rawValue[1]) << 8;
    	intValue += rawValue[0];
    	strValue = BleNamesResolver.resolveAppearance(intValue);
    }
    else if(uuid.equals(BleDefinedUUIDs.Characteristic.BODY_SENSOR_LOCATION)) { // body sensor location
    	// follow: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.body_sensor_location.xml
    	intValue = rawValue[0];
    	strValue = BleNamesResolver.resolveHeartRateSensorLocation(intValue);
    }
    else if(uuid.equals(BleDefinedUUIDs.Characteristic.BATTERY_LEVEL)) { // battery level
    	// follow: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.battery_level.xml
    	intValue = rawValue[0];
    	strValue = "" + intValue + "% battery level";
    }        
    else {
    	// not known type of characteristic, so we need to handle this in "general" way
    	// get first four bytes and transform it to integer
    	intValue = 0;
    	if(rawValue.length > 0) intValue = (int)rawValue[0];
    	if(rawValue.length > 1) intValue = intValue + ((int)rawValue[1] << 8); 
    	if(rawValue.length > 2) intValue = intValue + ((int)rawValue[2] << 8); 
    	if(rawValue.length > 3) intValue = intValue + ((int)rawValue[3] << 8); 
    	
        if (rawValue.length > 0) {
            final StringBuilder stringBuilder = new StringBuilder(rawValue.length);
            for(byte byteChar : rawValue) {
            	try {
            		stringBuilder.append(String.format("%c", byteChar));
            	} catch (Exception ex) {
            		// swallow illegal characters
            	}
            }
            strValue = stringBuilder.toString();
        }
    }
    
    String timestamp = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss.SSS").format(new Date());
	mRawValue = rawValue;
	mIntValue = intValue;
	mAsciiValue = getHexString(rawValue);
	mStrValue = strValue;
	mLastUpdateTime = timestamp;
}
 
Example 19
Source File: BleWrapper.java    From BLEService with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void getCharacteristicValue(BluetoothGattCharacteristic ch) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null || ch == null) return;
    
    byte[] rawValue = ch.getValue();
    String strValue = null;
    int intValue = 0;
    
    // lets read and do real parsing of some characteristic to get meaningful value from it 
    UUID uuid = ch.getUuid();
    
    if(uuid.equals(BleDefinedUUIDs.Characteristic.HEART_RATE_MEASUREMENT)) { // heart rate
    	// follow https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
    	// first check format used by the device - it is specified in bit 0 and tells us if we should ask for index 1 (and uint8) or index 2 (and uint16)
    	int index = ((rawValue[0] & 0x01) == 1) ? 2 : 1;
    	// also we need to define format
    	int format = (index == 1) ? BluetoothGattCharacteristic.FORMAT_UINT8 : BluetoothGattCharacteristic.FORMAT_UINT16;
    	// now we have everything, get the value
    	intValue = ch.getIntValue(format, index);
    	strValue = intValue + " bpm"; // it is always in bpm units
    }
    else if (uuid.equals(BleDefinedUUIDs.Characteristic.HEART_RATE_MEASUREMENT) || // manufacturer name string
    		 uuid.equals(BleDefinedUUIDs.Characteristic.MODEL_NUMBER_STRING) || // model number string)
    		 uuid.equals(BleDefinedUUIDs.Characteristic.FIRMWARE_REVISION_STRING)) // firmware revision string
    {
    	// follow https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.manufacturer_name_string.xml etc.
    	// string value are usually simple utf8s string at index 0
    	strValue = ch.getStringValue(0);
    }
    else if(uuid.equals(BleDefinedUUIDs.Characteristic.APPEARANCE)) { // appearance
    	// follow: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.gap.appearance.xml
    	intValue  = ((int)rawValue[1]) << 8;
    	intValue += rawValue[0];
    	strValue = BleNamesResolver.resolveAppearance(intValue);
    }
    else if(uuid.equals(BleDefinedUUIDs.Characteristic.BODY_SENSOR_LOCATION)) { // body sensor location
    	// follow: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.body_sensor_location.xml
    	intValue = rawValue[0];
    	strValue = BleNamesResolver.resolveHeartRateSensorLocation(intValue);
    }
    else if(uuid.equals(BleDefinedUUIDs.Characteristic.BATTERY_LEVEL)) { // battery level
    	// follow: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.battery_level.xml
    	intValue = rawValue[0];
    	strValue = "" + intValue + "% battery level";
    }        
    else {
    	// not known type of characteristic, so we need to handle this in "general" way
    	// get first four bytes and transform it to integer
    	intValue = 0;
    	if(rawValue.length > 0) intValue = (int)rawValue[0];
    	if(rawValue.length > 1) intValue = intValue + ((int)rawValue[1] << 8); 
    	if(rawValue.length > 2) intValue = intValue + ((int)rawValue[2] << 8); 
    	if(rawValue.length > 3) intValue = intValue + ((int)rawValue[3] << 8); 
    	
        if (rawValue.length > 0) {
            final StringBuilder stringBuilder = new StringBuilder(rawValue.length);
            for(byte byteChar : rawValue) {
                stringBuilder.append(String.format("%c", byteChar));
            }
            strValue = stringBuilder.toString();
        }
    }
    
    String timestamp = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss.SSS").format(new Date());
    mUiCallback.uiNewValueForCharacteristic(mBluetoothGatt,
                                            mBluetoothDevice,
                                            mBluetoothSelectedService,
    		                                ch,
    		                                strValue,
    		                                intValue,
    		                                rawValue,
    		                                timestamp);
}
 
Example 20
Source File: BleHeartRateSensor.java    From BLE-Heart-rate-variability-demo with MIT License 4 votes vote down vote up
private static Integer[] extractBeatToBeatInterval(
		BluetoothGattCharacteristic characteristic) {

       int flag = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
       int format = -1;
       int energy = -1;
       int offset = 1; // This depends on hear rate value format and if there is energy data
       int rr_count = 0;
       
       if ((flag & 0x01) != 0) {
           format = BluetoothGattCharacteristic.FORMAT_UINT16;
           Log.d(TAG, "Heart rate format UINT16.");
           offset = 3;
       } else {
           format = BluetoothGattCharacteristic.FORMAT_UINT8;
           Log.d(TAG, "Heart rate format UINT8.");
           offset = 2;
       }
       if ((flag & 0x08) != 0) {
           // calories present
           energy = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, offset);
           offset += 2;
           Log.d(TAG, "Received energy: {}"+ energy);
       }
       if ((flag & 0x16) != 0){
           // RR stuff.
           Log.d(TAG, "RR stuff found at offset: "+ offset);
           Log.d(TAG, "RR length: "+ (characteristic.getValue()).length);
           rr_count = ((characteristic.getValue()).length - offset) / 2;
           Log.d(TAG, "RR length: "+ (characteristic.getValue()).length);
           Log.d(TAG, "rr_count: "+ rr_count);
		if (rr_count > 0) {
			Integer[] mRr_values = new Integer[rr_count];
			for (int i = 0; i < rr_count; i++) {
				mRr_values[i] = characteristic.getIntValue(
						BluetoothGattCharacteristic.FORMAT_UINT16, offset);
				offset += 2;
				Log.d(TAG, "Received RR: " + mRr_values[i]);
			}
			return mRr_values;
		}
       }
       Log.d(TAG, "No RR data on this update: ");
       return null;
}