diff --git a/demos/Android-Demos/PolarSDK-ECG-HR-Demo/app/build.gradle b/demos/Android-Demos/PolarSDK-ECG-HR-Demo/app/build.gradle index 0fc4b96e..d78027e3 100644 --- a/demos/Android-Demos/PolarSDK-ECG-HR-Demo/app/build.gradle +++ b/demos/Android-Demos/PolarSDK-ECG-HR-Demo/app/build.gradle @@ -16,6 +16,10 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } } task copySdk { @@ -30,8 +34,9 @@ preBuild.dependsOn copySdk dependencies { implementation "com.androidplot:androidplot-core:1.5.6" implementation files('libs/polar-ble-sdk.aar') - implementation 'io.reactivex.rxjava2:rxjava:2.2.0' - implementation 'io.reactivex.rxjava2:rxandroid:2.1.0' + implementation 'io.reactivex.rxjava3:rxjava:3.0.0' + implementation group: 'commons-io', name: 'commons-io', version: '2.4' + implementation 'io.reactivex.rxjava3:rxandroid:3.0.0' implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' diff --git a/demos/Android-Demos/PolarSDK-ECG-HR-Demo/app/src/main/java/com/polar/polarsdkecghrdemo/ECGActivity.java b/demos/Android-Demos/PolarSDK-ECG-HR-Demo/app/src/main/java/com/polar/polarsdkecghrdemo/ECGActivity.java index 2d225ed7..06480db2 100755 --- a/demos/Android-Demos/PolarSDK-ECG-HR-Demo/app/src/main/java/com/polar/polarsdkecghrdemo/ECGActivity.java +++ b/demos/Android-Demos/PolarSDK-ECG-HR-Demo/app/src/main/java/com/polar/polarsdkecghrdemo/ECGActivity.java @@ -17,11 +17,11 @@ import java.util.List; import java.util.UUID; -import io.reactivex.android.schedulers.AndroidSchedulers; -import io.reactivex.disposables.Disposable; -import io.reactivex.functions.Action; -import io.reactivex.functions.Consumer; -import io.reactivex.functions.Function; +import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers; +import io.reactivex.rxjava3.disposables.Disposable; +import io.reactivex.rxjava3.functions.Action; +import io.reactivex.rxjava3.functions.Consumer; +import io.reactivex.rxjava3.functions.Function; import polar.com.sdk.api.PolarBleApi; import polar.com.sdk.api.PolarBleApiCallback; import polar.com.sdk.api.PolarBleApiDefaultImpl; diff --git a/demos/Android-Demos/PolarSDK-ECG-HR-Demo/app/src/main/java/com/polar/polarsdkecghrdemo/HRActivity.java b/demos/Android-Demos/PolarSDK-ECG-HR-Demo/app/src/main/java/com/polar/polarsdkecghrdemo/HRActivity.java index f21909c0..cf32d5a8 100755 --- a/demos/Android-Demos/PolarSDK-ECG-HR-Demo/app/src/main/java/com/polar/polarsdkecghrdemo/HRActivity.java +++ b/demos/Android-Demos/PolarSDK-ECG-HR-Demo/app/src/main/java/com/polar/polarsdkecghrdemo/HRActivity.java @@ -17,7 +17,7 @@ import java.util.List; import java.util.UUID; -import io.reactivex.disposables.Disposable; +import io.reactivex.rxjava3.disposables.Disposable; import polar.com.sdk.api.PolarBleApi; import polar.com.sdk.api.PolarBleApiCallback; import polar.com.sdk.api.PolarBleApiDefaultImpl; 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 3b167643..3693a07a 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 @@ -69,7 +69,7 @@ public BDDeviceListenerImpl( if (btManager != null) { bluetoothAdapter = btManager.getAdapter(); } - connectionHandler = new ConnectionHandler(context, this, this, this); + connectionHandler = new ConnectionHandler( this, this, this); gattCallback = new BDGattCallback(context, connectionHandler, sessions); bondingManager = new BDBondingListener(context); scanCallback = new BDScanCallback(context, btManager, this); 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 afaa4428..1a8573a4 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 @@ -7,6 +7,7 @@ import android.bluetooth.BluetoothGattDescriptor; import android.content.Context; import android.os.Build; +import android.os.Handler; import com.androidcommunications.polar.api.ble.BleLogger; import com.androidcommunications.polar.common.ble.RxUtils; @@ -27,9 +28,11 @@ class BDGattCallback extends BluetoothGattCallback { private ConnectionHandler connectionHandler; private BDDeviceList sessions; private Scheduler scheduler; + private Handler handler; BDGattCallback(Context context, ConnectionHandler connectionHandler, BDDeviceList sessions) { this.scheduler = AndroidSchedulers.from(context.getMainLooper()); + this.handler = new Handler(context.getMainLooper()); this.connectionHandler = connectionHandler; this.sessions = sessions; } @@ -45,7 +48,7 @@ public void onConnectionStateChange(final BluetoothGatt gatt, int status, int ne if (smartPolarDeviceSession != null) { if (newState == BluetoothGatt.STATE_CONNECTED) { if (status == BluetoothGatt.GATT_SUCCESS) { - connectionHandler.deviceConnected(smartPolarDeviceSession); + handler.post(() -> connectionHandler.deviceConnected(smartPolarDeviceSession)); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { gatt.setPreferredPhy(BluetoothDevice.PHY_LE_2M_MASK, BluetoothDevice.PHY_LE_2M_MASK, BluetoothDevice.PHY_OPTION_NO_PREFERRED); } @@ -56,13 +59,13 @@ public void onConnectionStateChange(final BluetoothGatt gatt, int status, int ne throwable -> BleLogger.e(TAG, "Wait encryption start failed: " + throwable.getLocalizedMessage()), () -> startDiscovery(smartPolarDeviceSession, gatt))); } else { - startDiscovery(smartPolarDeviceSession, gatt); + handler.post(() -> startDiscovery(smartPolarDeviceSession, gatt)); } } else { - connectionHandler.deviceDisconnected(smartPolarDeviceSession); + handler.post(() -> connectionHandler.deviceDisconnected(smartPolarDeviceSession)); } } else if (newState == BluetoothGatt.STATE_DISCONNECTED) { - connectionHandler.deviceDisconnected(smartPolarDeviceSession); + handler.post(() -> connectionHandler.deviceDisconnected(smartPolarDeviceSession)); } } else { BleLogger.e(TAG, "Dead gatt object received"); @@ -77,10 +80,12 @@ public void onServicesDiscovered(final BluetoothGatt gatt, int status) { super.onServicesDiscovered(gatt, status); final BDDeviceSessionImpl bdDeviceSession = sessions.getSession(gatt); if (bdDeviceSession != null) { - if (bdDeviceSession.serviceDiscovery != null) { - bdDeviceSession.serviceDiscovery.dispose(); - bdDeviceSession.serviceDiscovery = null; - } + handler.post(() -> { + if (bdDeviceSession.serviceDiscovery != null) { + bdDeviceSession.serviceDiscovery.dispose(); + bdDeviceSession.serviceDiscovery = null; + } + }); if (status == BluetoothGatt.GATT_SUCCESS) { bdDeviceSession.handleServicesDiscovered(); gatt.requestMtu(POLAR_MAX_MTU); diff --git a/sources/Android/android-communications/src/main/java/com/androidcommunications/polar/enpoints/ble/bluedroid/host/connection/ConnectionHandler.java b/sources/Android/android-communications/src/main/java/com/androidcommunications/polar/enpoints/ble/bluedroid/host/connection/ConnectionHandler.java index 32f2b26c..0c468f86 100755 --- a/sources/Android/android-communications/src/main/java/com/androidcommunications/polar/enpoints/ble/bluedroid/host/connection/ConnectionHandler.java +++ b/sources/Android/android-communications/src/main/java/com/androidcommunications/polar/enpoints/ble/bluedroid/host/connection/ConnectionHandler.java @@ -43,13 +43,10 @@ private enum ConnectionHandlerAction { private BDDeviceSessionImpl current; private ConnectionHandlerObserver observer; private boolean automaticReconnection = true; - private Handler stateHandler; - public ConnectionHandler(Context context, - ConnectionInterface connectionInterface, + public ConnectionHandler(ConnectionInterface connectionInterface, ScannerInterface scannerInterface, ConnectionHandlerObserver observer) { - this.stateHandler = new Handler(context.getMainLooper()); this.scannerInterface = scannerInterface; this.connectionInterface = connectionInterface; this.state = ConnectionHandlerState.FREE; @@ -83,17 +80,13 @@ public void disconnectDevice(BDDeviceSessionImpl bleDeviceSession) { } public void deviceConnected(final BDDeviceSessionImpl bleDeviceSession) { - stateHandler.post(() -> { - commandState(bleDeviceSession, ConnectionHandlerAction.DEVICE_CONNECTED); - observer.deviceConnected(bleDeviceSession); - }); + commandState(bleDeviceSession, ConnectionHandlerAction.DEVICE_CONNECTED); + observer.deviceConnected(bleDeviceSession); } public void deviceDisconnected(final BDDeviceSessionImpl bleDeviceSession) { - stateHandler.post(() -> { - commandState(bleDeviceSession, ConnectionHandlerAction.DEVICE_DISCONNECTED); - observer.deviceDisconnected(bleDeviceSession); - }); + commandState(bleDeviceSession, ConnectionHandlerAction.DEVICE_DISCONNECTED); + observer.deviceDisconnected(bleDeviceSession); } /** @@ -204,8 +197,8 @@ private void free(final BDDeviceSessionImpl session, ConnectionHandlerAction act private void connecting(final BDDeviceSessionImpl session, ConnectionHandlerAction action) { switch (action) { case ENTRY: { + scannerInterface.connectionHandlerRequestStopScanning(); if (connectionInterface.isPowered()) { - scannerInterface.connectionHandlerRequestStopScanning(); current = session; updateSessionState(session, BleDeviceSession.DeviceSessionState.SESSION_OPENING); connectionInterface.connectDevice(session);