Java Code Examples for android.bluetooth.BluetoothGattCharacteristic#getIntValue()

The following examples show how to use android.bluetooth.BluetoothGattCharacteristic#getIntValue() . 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: BLEService.java    From microbit with Apache License 2.0 6 votes vote down vote up
private void handleCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
    String UUID = characteristic.getUuid().toString();

    Integer integerValue = characteristic.getIntValue(GattFormats.FORMAT_UINT32, 0);

    if(integerValue == null) {
        return;
    }
    int value = integerValue;
    int eventSrc = value & 0x0ffff;
    if(eventSrc < 1001) {
        return;
    }
    logi("Characteristic UUID = " + UUID);
    logi("Characteristic Value = " + value);
    logi("eventSrc = " + eventSrc);

    int event = (value >> 16) & 0x0ffff;
    logi("event = " + event);
    sendMessage(eventSrc, event);
}
 
Example 2
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 3
Source File: TemperatureReading.java    From EFRConnect-android with Apache License 2.0 5 votes vote down vote up
public static TemperatureReading fromCharacteristic(BluetoothGattCharacteristic characteristic) {
    byte[] data = characteristic.getValue();
    int flags = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
    Type type = (flags & 0x01) > 0 ? Type.FAHRENHEIT : Type.CELSIUS;
    int tempData = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT32, 1); //ByteBuffer.wrap(data, 1, 4).getInt();
    int exponent = tempData >> 24;
    int mantissa = tempData & 0x00FFFFFF;
    double actualTemp = (double) mantissa * Math.pow(10, exponent);
    long time;
    if ((flags & 0x02) > 0) {
        int year = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 5);
        int month = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 7);
        int day = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 8);
        int hour = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 9);
        int min = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 10);
        int sec = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 11);

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, day);
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, min);
        cal.set(Calendar.SECOND, sec);
        cal.set(Calendar.MILLISECOND, 0);
        time = cal.getTimeInMillis();
    } else {
        time = System.currentTimeMillis();
    }
    return new TemperatureReading(type, actualTemp, time);
}
 
Example 4
Source File: BleSensorUtils.java    From BLE-Heart-rate-variability-demo with MIT License 5 votes vote down vote up
/**
 * Gyroscope, Magnetometer, Barometer, IR temperature
 * all store 16 bit two's complement values in the awkward format
 * LSB MSB, which cannot be directly parsed as getIntValue(FORMAT_SINT16, offset)
 * because the bytes are stored in the "wrong" direction.
 *
 * This function extracts these 16 bit two's complement values.
 * */
public static Integer shortSignedAtOffset(BluetoothGattCharacteristic c, int offset) {
    Integer lowerByte = c.getIntValue(FORMAT_UINT8, offset);
    if (lowerByte == null)
        return 0;
    Integer upperByte = c.getIntValue(FORMAT_SINT8, offset + 1); // Note: interpret MSB as signed.
    if (upperByte == null)
        return 0;

    return (upperByte << 8) + lowerByte;
}
 
Example 5
Source File: TiKeysSensor.java    From BleSensorTag with MIT License 5 votes vote down vote up
@Override
protected boolean apply(BluetoothGattCharacteristic c, TiSensorTag data) {
    /*
     * The key state is encoded into 1 unsigned byte.
     * bit 0 designates the right key.
     * bit 1 designates the left key.
     * bit 2 designates the side key.
     *
     * Weird, in the userguide left and right are opposite.
     */
    final int encodedInteger = c.getIntValue(FORMAT_UINT8, 0);
    data.setStatus(TiSensorTag.KeysStatus.valueAt(encodedInteger % 4));
    return true;
}
 
Example 6
Source File: UpdateService.java    From Android-nRF-Beacon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Obtains the cached value of the Manufacturer ID characteristic. If the value has not been obtained yet using {@link #read()}, or the characteristic has not been found on the beacon,
 * <code>null</code> is returned.
 * 
 * @return the Manufacturer ID or <code>null</code>
 */
public Integer getManufacturerId() {
	final BluetoothGattCharacteristic characteristic = mManufacturerIdCharacteristic;
	if (characteristic != null) {
		final byte[] data = characteristic.getValue();
		if (data == null || data.length < 2)
			return null;
		return characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 0);
	}
	return null;
}
 
Example 7
Source File: TIMagnetometerTranslator.java    From ibm-wearables-android-sdk with Apache License 2.0 5 votes vote down vote up
private Integer shortSignedAtOffset(BluetoothGattCharacteristic c, int offset) {
    Integer lowerByte = c.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, offset);
    if (lowerByte == null)
        return 0;
    Integer upperByte = c.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT8, offset + 1); // Note: interpret MSB as signed.
    if (upperByte == null)
        return 0;
    return (upperByte << 8) + lowerByte;
}
 
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: DfuBaseService.java    From microbit with Apache License 2.0 5 votes vote down vote up
/**
 * Reads the DFU Version characteristic if such exists. Otherwise it returns 0.
 *
 * @param gatt           the GATT device
 * @param characteristic the characteristic to read
 * @return a version number or 0 if not present on the bootloader
 * @throws DeviceDisconnectedException
 * @throws DfuException
 * @throws UploadAbortedException
 */
private int readVersion(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) throws DeviceDisconnectedException, DfuException, UploadAbortedException {
    if (mConnectionState != STATE_CONNECTED_AND_READY)
        throw new DeviceDisconnectedException("Unable to read version number", mConnectionState);
    // If the DFU Version characteristic is not available we return 0.
    if (characteristic == null)
        return 0;

    mReceivedData = null;
    mError = 0;

    logi("Reading DFU version number...");
    sendLogBroadcast(LOG_LEVEL_VERBOSE, "Reading DFU version number...");

    gatt.readCharacteristic(characteristic);

    // We have to wait until device receives a response or an error occur
    try {
        synchronized (mLock) {
            while ((!mRequestCompleted && mConnectionState == STATE_CONNECTED_AND_READY && mError == 0 && !mAborted) || mPaused)
                mLock.wait();
        }
    } catch (final InterruptedException e) {
        loge("Sleeping interrupted", e);
    }

    if (mAborted)
        throw new UploadAbortedException();

    if (mError != 0)
        throw new DfuException("Unable to read version number", mError);

    if (mConnectionState != STATE_CONNECTED_AND_READY)
        throw new DeviceDisconnectedException("Unable to read version number", mConnectionState);

    // The version is a 16-bit unsigned int
    return characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 0);
}
 
Example 10
Source File: TxPowerLevel.java    From bletools with MIT License 5 votes vote down vote up
public static TxPowerLevel fromCharacteristic(BluetoothGattCharacteristic characteristic) {
	Integer value = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT8, 0);
	if (value != null) {
		return new TxPowerLevel(value.intValue());
	}
	else
		throw new RuntimeException("getIntValue retured null");
}
 
Example 11
Source File: UpdateService.java    From Android-nRF-Beacon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onCharacteristicWrite(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic, final int status) {
	if (status != BluetoothGatt.GATT_SUCCESS) {
		logw("Characteristic write error: " + status);
		broadcastError(status);
		return;
	}

	if (CONFIG_UUID_CHARACTERISTIC_UUID.equals(characteristic.getUuid())) {
		final UUID uuid = decodeBeaconUUID(characteristic);
		broadcastUuid(uuid);
	} else if (CONFIG_MAJOR_MINOR_CHARACTERISTIC_UUID.equals(characteristic.getUuid())) {
		final int major = decodeUInt16(characteristic, 0);
		final int minor = decodeUInt16(characteristic, 2);
		broadcastMajorAndMinor(major, minor);
	} else if (CONFIG_RSSI_CHARACTERISTIC_UUID.equals(characteristic.getUuid())) {
		final int rssi = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT8, 0);
		broadcastRssi(rssi);
	} else if (CONFIG_MANUFACTURER_ID_CHARACTERISTIC_UUID.equals(characteristic.getUuid())) {
		final int id = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 0);
		broadcastManufacturerId(id);
	} else if (CONFIG_ADV_INTERVAL_CHARACTERISTIC_UUID.equals(characteristic.getUuid())) {
		final int interval = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 0);
		broadcastAdvInterval(interval);
	} else if (CONFIG_LED_SETTINGS_CHARACTERISTIC_UUID.equals(characteristic.getUuid())) {
		final boolean on = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0) == 1;
		broadcastLedStatus(on);
	}
}
 
Example 12
Source File: TemperatureValue.java    From EFRConnect-android with Apache License 2.0 5 votes vote down vote up
private TemperatureValue(BluetoothGattCharacteristic value) {
    // https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml
    final int flags = value.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0) & 0x00000007;
    temperature = value.getFloatValue(BluetoothGattCharacteristic.FORMAT_FLOAT, 1);
    isFahrenheit = (flags & 0x00000001) != 0;

    switch (flags) {
        case 2:
        case 3:
            timeStamp = getTimeStamp(value, 1 + 4);
            temperatureType = null;
            break;

        case 4:
        case 5:
            timeStamp = null;
            // https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_type.xml
            temperatureType = value.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1 + 4);
            break;

        case 6:
        case 7:
            timeStamp = getTimeStamp(value, 1 + 4);
            temperatureType = value.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1 + 4 + 7);
            break;

        default:
            timeStamp = null;
            temperatureType = null;
            break;

    }
}
 
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: OWDevice.java    From android-ponewheel with MIT License 5 votes vote down vote up
private void processRidingMode(byte[] incomingValue, DeviceCharacteristic dc, BluetoothGattCharacteristic incomingCharacteristic) {

        int ridemode = incomingCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);
        String rideMode1 = Integer.toString(ridemode);
        Timber.d("rideMode1 = " + rideMode1);

        dc.value.set(rideMode1);
    }
 
Example 15
Source File: OWDevice.java    From android-ponewheel with MIT License 5 votes vote down vote up
public void processBatteryTemp(BluetoothGattCharacteristic incomingCharacteristic, DeviceCharacteristic dc) {
    int batteryTemp1 = incomingCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
    int batteryTemp2 = incomingCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1);

    Timber.d("batteryTemp = %d, %d", batteryTemp1, batteryTemp2);

    setFormattedTempWithMetricPreference(dc, batteryTemp1, batteryTemp2);
    updateBatteryChanges |= Battery.setBatteryTemp((batteryTemp1+batteryTemp2) / 2);
}
 
Example 16
Source File: UpdateService.java    From Android-nRF-Beacon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onCharacteristicRead(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic, final int status) {
	if (status != BluetoothGatt.GATT_SUCCESS) {
		logw("Characteristic read error: " + status);
		broadcastError(status);
		return;
	}

	if (CONFIG_UUID_CHARACTERISTIC_UUID.equals(characteristic.getUuid())) {
		final UUID uuid = decodeBeaconUUID(characteristic);
		broadcastUuid(uuid);
		if (mMajorMinorCharacteristic != null && mMajorMinorCharacteristic.getValue() == null)
			gatt.readCharacteristic(mMajorMinorCharacteristic);
	} else if (CONFIG_MAJOR_MINOR_CHARACTERISTIC_UUID.equals(characteristic.getUuid())) {
		final int major = decodeUInt16(characteristic, 0);
		final int minor = decodeUInt16(characteristic, 2);
		broadcastMajorAndMinor(major, minor);
		if (mRssiCharacteristic != null && mRssiCharacteristic.getValue() == null)
			gatt.readCharacteristic(mRssiCharacteristic);
	} else if (CONFIG_RSSI_CHARACTERISTIC_UUID.equals(characteristic.getUuid())) {
		final int rssi = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT8, 0);
		broadcastRssi(rssi);
		if (mManufacturerIdCharacteristic != null && mManufacturerIdCharacteristic.getValue() == null)
			gatt.readCharacteristic(mManufacturerIdCharacteristic);
	} else if (CONFIG_MANUFACTURER_ID_CHARACTERISTIC_UUID.equals(characteristic.getUuid())) {
		final int id = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 0);
		broadcastManufacturerId(id);
		if (mAdvIntervalCharacteristic != null && mAdvIntervalCharacteristic.getValue() == null)
			gatt.readCharacteristic(mAdvIntervalCharacteristic);
	} else if (CONFIG_ADV_INTERVAL_CHARACTERISTIC_UUID.equals(characteristic.getUuid())) {
		final int interval = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 0);
		broadcastAdvInterval(interval);
		if (mLedSettingsCharacteristic != null && mLedSettingsCharacteristic.getValue() == null)
			gatt.readCharacteristic(mLedSettingsCharacteristic);
	} else if (CONFIG_LED_SETTINGS_CHARACTERISTIC_UUID.equals(characteristic.getUuid())) {
		final boolean on = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0) == 1;
		broadcastLedStatus(on);
	}
}
 
Example 17
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 18
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;
}
 
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: AHServiceProfile.java    From sony-smartband-open-api with MIT License 4 votes vote down vote up
public static int readCurrentTime( BluetoothGattCharacteristic characteristic ) {
    return characteristic.getIntValue( BluetoothGattCharacteristic.FORMAT_UINT32, 0 );
}