diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiSupport.java index 557430b60..d4caabf7e 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/HuamiSupport.java @@ -301,7 +301,7 @@ public abstract class HuamiSupport extends AbstractBTLEDeviceSupport implements } }; - private BluetoothGattCharacteristic characteristicHRControlPoint; + protected BluetoothGattCharacteristic characteristicHRControlPoint; private BluetoothGattCharacteristic characteristicChunked; private BluetoothGattCharacteristic characteristicChunked2021Write; @@ -2284,7 +2284,7 @@ public abstract class HuamiSupport extends AbstractBTLEDeviceSupport implements logMessageContent(value); } - private void handleHeartrate(byte[] value) { + protected void handleHeartrate(byte[] value) { if (value.length == 2 && value[0] == 0) { int hrValue = (value[1] & 0xff); if (LOG.isDebugEnabled()) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/amazfitneo/AmazfitNeoSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/amazfitneo/AmazfitNeoSupport.java index e784c6fd7..949c0d111 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/amazfitneo/AmazfitNeoSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/amazfitneo/AmazfitNeoSupport.java @@ -44,6 +44,10 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.operations.Upd public class AmazfitNeoSupport extends MiBand5Support { private static final Logger LOG = LoggerFactory.getLogger(AmazfitNeoSupport.class); + private boolean heartRateRealtimeStarted = false; + private boolean heartRateTestStarted = false; + private byte heartRateRealtimeCount = 0; + @Override protected boolean notificationHasExtraHeader() { return false; @@ -97,6 +101,62 @@ public class AmazfitNeoSupport extends MiBand5Support { return this; } + @Override + public void onHeartRateTest() { + if (characteristicHRControlPoint == null) { + return; + } + try { + TransactionBuilder builder = performInitialized("HeartRateTest"); + enableNotifyHeartRateMeasurements(true, builder); + builder.write(characteristicHRControlPoint, new byte[]{ 0x15, 0x01, 0x01 }); + builder.queue(getQueue()); + heartRateTestStarted = true; + } catch (IOException ex) { + LOG.error("Unable to read heart rate from Huami device", ex); + } + } + + @Override + public void onEnableRealtimeHeartRateMeasurement(boolean enable) { + heartRateTestStarted = false; + if (characteristicHRControlPoint == null) { + return; + } + try { + TransactionBuilder builder = performInitialized("Enable realtime heart rate measurement"); + enableNotifyHeartRateMeasurements(enable, builder); + if (enable) { + if (heartRateRealtimeStarted) { + if(heartRateRealtimeCount >= 10) { + builder.write(characteristicHRControlPoint, new byte[]{ 0x16 }); //send continue every 10 seconds + heartRateRealtimeCount = 0; + } + heartRateRealtimeCount++; + } + else { + builder.write(characteristicHRControlPoint, new byte[]{ 0x15, 0x01, 0x01 }); + heartRateRealtimeCount = 10; //sometimes first measurement times out, send first keep alive immediately + } + heartRateRealtimeStarted = true; + } else { + builder.write(characteristicHRControlPoint, new byte[] { 0x15, 0x01, 0x00 }); + heartRateRealtimeStarted = false; + } + builder.queue(getQueue()); + enableRealtimeSamplesTimer(enable); + } catch (IOException ex) { + LOG.error("Unable to enable realtime heart rate measurement", ex); + } + } + + @Override + protected void handleHeartrate(byte[] value) { + super.handleHeartrate(value); + if (heartRateTestStarted) + onEnableRealtimeHeartRateMeasurement(false); //stop test after single measurement, disable HR notify + } + @Override public HuamiFWHelper createFWHelper(Uri uri, Context context) throws IOException { return new AmazfitNeoFWHelper(uri, context);