Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

WIP: Enable reading of sensor data #1081

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}
Expand Down