Skip to content

Commit

Permalink
android ecg demo updated, android sources updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Erkki Silvola committed Apr 22, 2020
1 parent 0341250 commit d024bd8
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 30 deletions.
9 changes: 7 additions & 2 deletions demos/Android-Demos/PolarSDK-ECG-HR-Demo/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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);
}
Expand All @@ -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");
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit d024bd8

Please sign in to comment.