Avoid potential NPEs

This commit is contained in:
cpfeiffer 2016-07-04 22:40:01 +02:00
parent 73b2fc357e
commit 91d1cea51f
1 changed files with 23 additions and 15 deletions

View File

@ -817,9 +817,9 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
} else if (MiBandService.UUID_CHARACTERISTIC_BATTERY.equals(characteristicUUID)) { } else if (MiBandService.UUID_CHARACTERISTIC_BATTERY.equals(characteristicUUID)) {
handleBatteryInfo(characteristic.getValue(), status); handleBatteryInfo(characteristic.getValue(), status);
} else if (MiBandService.UUID_CHARACTERISTIC_HEART_RATE_MEASUREMENT.equals(characteristicUUID)) { } else if (MiBandService.UUID_CHARACTERISTIC_HEART_RATE_MEASUREMENT.equals(characteristicUUID)) {
logHeartrate(characteristic.getValue()); logHeartrate(characteristic.getValue(), status);
} else if (MiBandService.UUID_CHARACTERISTIC_DATE_TIME.equals(characteristicUUID)) { } else if (MiBandService.UUID_CHARACTERISTIC_DATE_TIME.equals(characteristicUUID)) {
logDate(characteristic.getValue()); logDate(characteristic.getValue(), status);
} else { } else {
LOG.info("Unhandled characteristic read: " + characteristicUUID); LOG.info("Unhandled characteristic read: " + characteristicUUID);
logMessageContent(characteristic.getValue()); logMessageContent(characteristic.getValue());
@ -845,27 +845,35 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
* @param value * @param value
*/ */
public void logMessageContent(byte[] value) { public void logMessageContent(byte[] value) {
LOG.info("RECEIVED DATA WITH LENGTH: " + value.length); LOG.info("RECEIVED DATA WITH LENGTH: " + ((value != null) ? value.length : "(null)"));
for (byte b : value) { if (value != null) {
LOG.warn("DATA: " + String.format("0x%2x", b)); for (byte b : value) {
LOG.warn("DATA: " + String.format("0x%2x", b));
}
} }
} }
public void logDate(byte[] value) { public void logDate(byte[] value, int status) {
GregorianCalendar calendar = MiBandDateConverter.rawBytesToCalendar(value); if (status == BluetoothGatt.GATT_SUCCESS) {
LOG.info("Got Mi Band Date: " + DateTimeUtils.formatDateTime(calendar.getTime())); GregorianCalendar calendar = MiBandDateConverter.rawBytesToCalendar(value);
} LOG.info("Got Mi Band Date: " + DateTimeUtils.formatDateTime(calendar.getTime()));
public void logHeartrate(byte[] value) {
LOG.info("Got heartrate:");
if (value.length == 2 && value[0] == 6) {
int hrValue = (value[1] & 0xff);
GB.toast(getContext(), "Heart Rate measured: " + hrValue, Toast.LENGTH_LONG, GB.INFO);
} else { } else {
logMessageContent(value); logMessageContent(value);
} }
} }
public void logHeartrate(byte[] value, int status) {
if (status == BluetoothGatt.GATT_SUCCESS && value != null) {
LOG.info("Got heartrate:");
if (value.length == 2 && value[0] == 6) {
int hrValue = (value[1] & 0xff);
GB.toast(getContext(), "Heart Rate measured: " + hrValue, Toast.LENGTH_LONG, GB.INFO);
}
return;
}
logMessageContent(value);
}
private void handleHeartrate(byte[] value) { private void handleHeartrate(byte[] value) {
if (value.length == 2 && value[0] == 6) { if (value.length == 2 && value[0] == 6) {
int hrValue = (value[1] & 0xff); int hrValue = (value[1] & 0xff);