From 3c1626777de23e040e62fe261e2e02fbb5a8cd9e Mon Sep 17 00:00:00 2001 From: Erkki Silvola Date: Fri, 17 Apr 2020 12:50:51 +0300 Subject: [PATCH] added mtu set to android api --- .../java/polar/com/androidblesdk/MainActivity.java | 1 + .../polar/api/ble/BleDeviceListener.java | 11 +++++++---- .../polar/api/ble/model/gatt/client/BlePMDClient.java | 11 +++++++---- .../ble/bluedroid/host/BDDeviceListenerImpl.java | 5 +++++ .../enpoints/ble/bluedroid/host/BDGattCallback.java | 6 +++++- .../polar/enpoints/ble/common/BleDeviceListener2.java | 1 + .../src/main/java/polar/com/sdk/api/PolarBleApi.java | 8 ++++++++ .../main/java/polar/com/sdk/impl/BDBleApiImpl.java | 5 +++++ 8 files changed, 39 insertions(+), 9 deletions(-) diff --git a/examples/example-android/androidBleSdkTestApp/app/src/main/java/polar/com/androidblesdk/MainActivity.java b/examples/example-android/androidBleSdkTestApp/app/src/main/java/polar/com/androidblesdk/MainActivity.java index 3f7cc5db..8ab56a41 100644 --- a/examples/example-android/androidBleSdkTestApp/app/src/main/java/polar/com/androidblesdk/MainActivity.java +++ b/examples/example-android/androidBleSdkTestApp/app/src/main/java/polar/com/androidblesdk/MainActivity.java @@ -57,6 +57,7 @@ protected void onCreate(Bundle savedInstanceState) { // Notice PolarBleApi.ALL_FEATURES are enabled api = PolarBleApiDefaultImpl.defaultImplementation(this, PolarBleApi.ALL_FEATURES); api.setPolarFilter(false); + api.setMtu(140); final Button broadcast = this.findViewById(R.id.broadcast_button); final Button connect = this.findViewById(R.id.connect_button); diff --git a/sources/Android/android-communications/src/main/java/com/androidcommunications/polar/api/ble/BleDeviceListener.java b/sources/Android/android-communications/src/main/java/com/androidcommunications/polar/api/ble/BleDeviceListener.java index 5ad94977..d51bc35a 100755 --- a/sources/Android/android-communications/src/main/java/com/androidcommunications/polar/api/ble/BleDeviceListener.java +++ b/sources/Android/android-communications/src/main/java/com/androidcommunications/polar/api/ble/BleDeviceListener.java @@ -2,6 +2,7 @@ import android.bluetooth.le.ScanFilter; import android.support.annotation.IntDef; +import android.support.annotation.IntRange; import android.support.annotation.Nullable; import android.util.Pair; @@ -102,6 +103,8 @@ public interface BlePowerStateChangedCallback { */ abstract public Flowable search(boolean fetchKnownDevices); + abstract public void setMtu(@IntRange(from = 70, to = 512) int mtu); + /** * As java does not support destructor/RAII, Client/App should call this whenever the application is being destroyed */ @@ -123,9 +126,9 @@ public interface BlePowerStateChangedCallback { /** * Deprecated use setDeviceSessionStateChangedCallback instead * - * Produces: onNext: When a device session state has changed, Note use pair.second to check the state (see BleDeviceSession.DeviceSessionState)
- * onError: should not be produced
- * onCompleted: This is never propagated, NOTE to get completed event configure observable with some end rule e.g. take(1), takeUntil() etc...
+ * Produces: onNext: When a device session state has changed, Note use pair.second to check the state (see BleDeviceSession.DeviceSessionState)
+ * onError: should not be produced
+ * onCompleted: If stream is further configured
* @param session, a specific session or null = monitor all sessions * @return Observable stream */ @@ -149,7 +152,7 @@ public interface BleDeviceSessionStateChangedCallback { abstract public void setDeviceSessionStateChangedCallback(@Nullable BleDeviceSessionStateChangedCallback changedCallback); /** - * aquires disconnection establishment directly without Observable returned + * aquires disconnection directly without Observable returned * @param session device */ abstract public void closeSessionDirect(BleDeviceSession session); diff --git a/sources/Android/android-communications/src/main/java/com/androidcommunications/polar/api/ble/model/gatt/client/BlePMDClient.java b/sources/Android/android-communications/src/main/java/com/androidcommunications/polar/api/ble/model/gatt/client/BlePMDClient.java index 1fe5f87b..a3d7b071 100644 --- a/sources/Android/android-communications/src/main/java/com/androidcommunications/polar/api/ble/model/gatt/client/BlePMDClient.java +++ b/sources/Android/android-communications/src/main/java/com/androidcommunications/polar/api/ble/model/gatt/client/BlePMDClient.java @@ -167,7 +167,8 @@ public enum PmdControlPointResponseCode { ERROR_INVALID_STATE(6), ERROR_INVALID_RESOLUTION(7), ERROR_INVALID_SAMPLE_RATE(8), - ERROR_INVALID_RANGE(9); + ERROR_INVALID_RANGE(9), + ERROR_INVALID_MTU(10); private int numVal; PmdControlPointResponseCode(int numVal) { @@ -183,9 +184,11 @@ public PmdControlPointResponse(byte[] data) { opCode = PmdControlPointCommand.values()[data[1]]; measurementType = data[2]; status = PmdControlPointResponseCode.values()[data[3]]; - more = data.length > 4 && data[4] != 0; - if (data.length > 5) { - parameters.write(data, 5, data.length - 5); + if (status == PmdControlPointResponseCode.SUCCESS) { + more = data.length > 4 && data[4] != 0; + if (data.length > 5) { + parameters.write(data, 5, data.length - 5); + } } } } diff --git a/sources/Android/android-communications/src/main/java/com/androidcommunications/polar/enpoints/ble/bluedroid/host/BDDeviceListenerImpl.java b/sources/Android/android-communications/src/main/java/com/androidcommunications/polar/enpoints/ble/bluedroid/host/BDDeviceListenerImpl.java index c17e6357..bdc842cc 100755 --- a/sources/Android/android-communications/src/main/java/com/androidcommunications/polar/enpoints/ble/bluedroid/host/BDDeviceListenerImpl.java +++ b/sources/Android/android-communications/src/main/java/com/androidcommunications/polar/enpoints/ble/bluedroid/host/BDDeviceListenerImpl.java @@ -202,6 +202,11 @@ public void run() { }); } + @Override + public void setMtu(int mtu) { + gattCallback.setPolarMaxMtu(mtu); + } + @Override public void shutDown(){ super.shutDown(); diff --git a/sources/Android/android-communications/src/main/java/com/androidcommunications/polar/enpoints/ble/bluedroid/host/BDGattCallback.java b/sources/Android/android-communications/src/main/java/com/androidcommunications/polar/enpoints/ble/bluedroid/host/BDGattCallback.java index 77b488ab..ed37526a 100755 --- a/sources/Android/android-communications/src/main/java/com/androidcommunications/polar/enpoints/ble/bluedroid/host/BDGattCallback.java +++ b/sources/Android/android-communications/src/main/java/com/androidcommunications/polar/enpoints/ble/bluedroid/host/BDGattCallback.java @@ -26,7 +26,7 @@ class BDGattCallback extends BluetoothGattCallback { private final static String TAG = BDGattCallback.class.getSimpleName(); - private final static int POLAR_MAX_MTU = 512; + private int POLAR_MAX_MTU = 512; private ConnectionHandler connectionHandler; private BDDeviceList sessions; @@ -38,6 +38,10 @@ class BDGattCallback extends BluetoothGattCallback { this.sessions = sessions; } + void setPolarMaxMtu(int mtu) { + POLAR_MAX_MTU = mtu; + } + @Override public void onConnectionStateChange(final BluetoothGatt gatt, int status, int newState) { final BDDeviceSessionImpl smartPolarDeviceSession = sessions.getSession(gatt); diff --git a/sources/Android/android-communications/src/main/java/com/androidcommunications/polar/enpoints/ble/common/BleDeviceListener2.java b/sources/Android/android-communications/src/main/java/com/androidcommunications/polar/enpoints/ble/common/BleDeviceListener2.java index 0b660ed6..557e0b9c 100644 --- a/sources/Android/android-communications/src/main/java/com/androidcommunications/polar/enpoints/ble/common/BleDeviceListener2.java +++ b/sources/Android/android-communications/src/main/java/com/androidcommunications/polar/enpoints/ble/common/BleDeviceListener2.java @@ -1,6 +1,7 @@ package com.androidcommunications.polar.enpoints.ble.common; import android.content.Context; +import android.support.annotation.IntRange; import android.support.annotation.Nullable; import android.util.Pair; diff --git a/sources/Android/android-communications/src/main/java/polar/com/sdk/api/PolarBleApi.java b/sources/Android/android-communications/src/main/java/polar/com/sdk/api/PolarBleApi.java index 33e28c97..1f4e12f0 100644 --- a/sources/Android/android-communications/src/main/java/polar/com/sdk/api/PolarBleApi.java +++ b/sources/Android/android-communications/src/main/java/polar/com/sdk/api/PolarBleApi.java @@ -1,6 +1,7 @@ // Copyright © 2019 Polar Electro Oy. All rights reserved. package polar.com.sdk.api; +import android.support.annotation.IntRange; import android.support.annotation.Nullable; import android.support.annotation.Size; import android.util.Pair; @@ -103,6 +104,13 @@ protected PolarBleApi(final int features) { this.features = features; } + /** + * set mtu to lower than default(232 is the default for polar devices, minimum for H10 is 70 and for OH1 is 140) + * to minimize latency + * @param mtu value between 64-512 to be set + */ + public abstract void setMtu(@IntRange(from = 70, to = 512) int mtu); + /** * Must be called when application is destroyed. */ diff --git a/sources/Android/android-communications/src/main/java/polar/com/sdk/impl/BDBleApiImpl.java b/sources/Android/android-communications/src/main/java/polar/com/sdk/impl/BDBleApiImpl.java index 985ee3d3..6fa0daff 100644 --- a/sources/Android/android-communications/src/main/java/polar/com/sdk/impl/BDBleApiImpl.java +++ b/sources/Android/android-communications/src/main/java/polar/com/sdk/impl/BDBleApiImpl.java @@ -165,6 +165,11 @@ protected void enableAndroidScanFilter() { } } + @Override + public void setMtu(int mtu) { + listener.setMtu(mtu); + } + @Override public void shutDown() { listener.shutDown();