diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBand2Service.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBand2Service.java index 71ffacf1b5..f91eecb293 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBand2Service.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBand2Service.java @@ -35,8 +35,8 @@ public class MiBand2Service { public static final UUID UUID_CHARACTERISTIC_FIRMWARE_DATA = UUID.fromString("00001532-0000-3512-2118-0009af100700"); public static final UUID UUID_UNKNOWN_CHARACTERISTIC0 = UUID.fromString("00000000-0000-3512-2118-0009af100700"); - public static final UUID UUID_UNKNOWN_CHARACTERISTIC1 = UUID.fromString("00000001-0000-3512-2118-0009af100700"); - public static final UUID UUID_UNKNOWN_CHARACTERISTIC2 = UUID.fromString("00000002-0000-3512-2118-0009af100700"); + public static final UUID UUID_CHARACTERISTIC_1_SENSOR_CONTROL = UUID.fromString("00000001-0000-3512-2118-0009af100700"); + public static final UUID UUID_CHARACTERISTIC_2_SENSOR_DATA = UUID.fromString("00000002-0000-3512-2118-0009af100700"); /** * Alarms, Display and other configuration. */ diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/MiBand2Support.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/MiBand2Support.java index a6d236d44a..7773ca4faa 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/MiBand2Support.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/MiBand2Support.java @@ -159,6 +159,7 @@ public void onReceive(Context context, Intent intent) { private boolean needsAuth; private volatile boolean telephoneRinging; private volatile boolean isLocatingDevice; + private volatile boolean isReadingSensorData; private final GBDeviceEventVersionInfo versionCmd = new GBDeviceEventVersionInfo(); private final GBDeviceEventBatteryInfo batteryCmd = new GBDeviceEventBatteryInfo(); @@ -287,6 +288,7 @@ public MiBand2Support enableFurtherNotifications(TransactionBuilder builder, boo builder.notify(getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_3_CONFIGURATION), enable); builder.notify(getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_6_BATTERY_INFO), enable); builder.notify(getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_DEVICEEVENT), enable); + builder.notify(getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_2_SENSOR_DATA), enable); return this; } @@ -342,11 +344,17 @@ public NotificationStrategy getNotificationStrategy() { return new Mi2NotificationStrategy(this); } + // HR commands private static final byte[] startHeartMeasurementManual = new byte[]{0x15, MiBandService.COMMAND_SET_HR_MANUAL, 1}; private static final byte[] stopHeartMeasurementManual = new byte[]{0x15, MiBandService.COMMAND_SET_HR_MANUAL, 0}; private static final byte[] startHeartMeasurementContinuous = new byte[]{0x15, MiBandService.COMMAND_SET__HR_CONTINUOUS, 1}; private static final byte[] stopHeartMeasurementContinuous = new byte[]{0x15, MiBandService.COMMAND_SET__HR_CONTINUOUS, 0}; + // Raw sensor commands + private static final byte[] startSensorRead1 = new byte[]{0x01, 0x01, 0x19}; + private static final byte[] startSensorRead2 = new byte[]{0x02}; + private static final byte[] stopSensorRead = new byte[]{0x03}; + private MiBand2Support requestBatteryInfo(TransactionBuilder builder) { LOG.debug("Requesting Battery Info!"); BluetoothGattCharacteristic characteristic = getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_6_BATTERY_INFO); @@ -1069,6 +1077,9 @@ public boolean onCharacteristicChanged(BluetoothGatt gatt, } else if (MiBand2Service.UUID_CHARACTERISTIC_7_REALTIME_STEPS.equals(characteristicUUID)) { handleRealtimeSteps(characteristic.getValue()); return true; + } else if (MiBand2Service.UUID_CHARACTERISTIC_2_SENSOR_DATA.equals(characteristicUUID)) { + handleSensorData(characteristic.getValue()); + return true; } else { LOG.info("Unhandled characteristic changed: " + characteristicUUID); logMessageContent(characteristic.getValue()); @@ -1145,6 +1156,24 @@ private void handleHeartrate(byte[] value) { } } + /** + * Handles the raw-data from the sensor. + * @see nodomain.freeyourgadget.gadgetbridge.service.devices.miband.MiBandSupport::handleSensorData + * + * @param value The data bytes + */ + private void handleSensorData(byte[] value) { + String string = ""; + for (byte b : value) { + string = string.concat(String.format(" 0x%4x", b)); + } + LOG.warn("Received sensor data:" + string); + + if ((value.length - 2) % 6 != 0) { + LOG.warn("Got unexpected sensor data with length: " + value.length); + } + } + private void handleRealtimeSteps(byte[] value) { if (value == null) { LOG.error("realtime steps: value is null"); @@ -1383,7 +1412,17 @@ public void onSendConfiguration(String config) { @Override public void onTestNewFunction() { try { - new FetchSportsSummaryOperation(this).perform(); + TransactionBuilder builder = performInitialized("Test get realtime sensor data"); + if (isReadingSensorData) { + builder.write(getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_1_SENSOR_CONTROL), stopSensorRead); + } else { + builder.write(getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_1_SENSOR_CONTROL), startSensorRead1); + builder.write(getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_1_SENSOR_CONTROL), startSensorRead2); + } + builder.queue(getQueue()); + isReadingSensorData = !isReadingSensorData; + + // new FetchSportsSummaryOperation(this).perform(); } catch (IOException ex) { LOG.error("Unable to fetch MI activity data", ex); }