Skip to content

Commit

Permalink
android ecg demo updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Erkki Silvola committed Apr 3, 2019
1 parent 57e91d4 commit 0904de7
Show file tree
Hide file tree
Showing 30 changed files with 542 additions and 67 deletions.
9 changes: 8 additions & 1 deletion demos/Android-Demos/PolarSDK-ECG-HR-Demo/app/src/main/AndroidManifest.xml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.polar.polarsdkecghrdemo.ECGActivity" android:screenOrientation="portrait"/>
<activity
android:name="com.polar.polarsdkecghrdemo.ECGActivity"
android:label="@string/ecg_name" android:screenOrientation="portrait">
</activity>
<activity
android:name="com.polar.polarsdkecghrdemo.HRActivity"
android:label="@string/hr_name" android:screenOrientation="portrait">
</activity>
</application>

</manifest>
118 changes: 68 additions & 50 deletions ...Demos/PolarSDK-ECG-HR-Demo/app/src/main/java/com/polar/polarsdkecghrdemo/ECGActivity.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.androidplot.xy.BoundaryMode;
import com.androidplot.xy.StepMode;
import com.androidplot.xy.XYPlot;

import org.reactivestreams.Publisher;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
Expand All @@ -31,7 +34,7 @@ public class ECGActivity extends AppCompatActivity implements PlotterListener {
private XYPlot plot;
private Plotter plotter;

TextView textViewHR;
TextView textViewHR, textViewFW;
private String TAG = "Polar_ECGActivity";
public PolarBleApi api;
private Disposable ecgDisposable = null;
Expand All @@ -44,20 +47,26 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
setContentView(R.layout.activity_ecg);
DEVICE_ID = getIntent().getStringExtra("id");
textViewHR = findViewById(R.id.info);
textViewFW = findViewById(R.id.fw);

plot = findViewById(R.id.plot);

api = PolarBleApiDefaultImpl.defaultImplementation(this, PolarBleApi.FEATURE_POLAR_SENSOR_STREAMING |PolarBleApi.FEATURE_HR);
api = PolarBleApiDefaultImpl.defaultImplementation(this,
PolarBleApi.FEATURE_POLAR_SENSOR_STREAMING |
PolarBleApi.FEATURE_BATTERY_INFO |
PolarBleApi.FEATURE_DEVICE_INFO |
PolarBleApi.FEATURE_HR);
api.setApiCallback(new PolarBleApiCallback() {
@Override
public void blePowerStateChanged(boolean b) {
Log.d(TAG,"BluetoothStateChanged " + b);
Log.d(TAG, "BluetoothStateChanged " + b);
}

@Override
public void polarDeviceConnected(PolarDeviceInfo s) {
Log.d(TAG,"Device connected " + s.deviceId);
Toast.makeText(classContext, R.string.connected, Toast.LENGTH_SHORT).show();
Log.d(TAG, "Device connected " + s.deviceId);
Toast.makeText(classContext, R.string.connected,
Toast.LENGTH_SHORT).show();
}

@Override
Expand All @@ -67,29 +76,29 @@ public void polarDeviceConnecting(PolarDeviceInfo polarDeviceInfo) {

@Override
public void polarDeviceDisconnected(PolarDeviceInfo s) {
Log.d(TAG,"Device disconnected " + s);
Log.d(TAG, "Device disconnected " + s);

}

@Override
public void ecgFeatureReady(String s) {
Log.d(TAG,"ECG Feature ready " + s);
Log.d(TAG, "ECG Feature ready " + s);
streamECG();
}

@Override
public void accelerometerFeatureReady(String s) {
Log.d(TAG,"ACC Feature ready " + s);
Log.d(TAG, "ACC Feature ready " + s);
}

@Override
public void ppgFeatureReady(String s) {
Log.d(TAG,"PPG Feature ready " + s);
Log.d(TAG, "PPG Feature ready " + s);
}

@Override
public void ppiFeatureReady(String s) {
Log.d(TAG,"PPI Feature ready " + s);
Log.d(TAG, "PPI Feature ready " + s);
}

@Override
Expand All @@ -99,39 +108,45 @@ public void biozFeatureReady(String s) {

@Override
public void hrFeatureReady(String s) {
Log.d(TAG,"HR Feature ready " + s);
Log.d(TAG, "HR Feature ready " + s);
}

@Override
public void fwInformationReceived(String s, String s1) {
Log.d(TAG,"FW Information received " + s + s1);
String msg = "Firmware: " + s1.trim();
Log.d(TAG, "Firmware: " + s + " " + s1.trim());
textViewFW.append(msg + "\n");
}

@Override
public void batteryLevelReceived(String s, int i) {
Log.d(TAG,"Battery level " + s + i);
String msg = "ID: " + s + "\nBattery level: " + i;
Log.d(TAG, "Battery level " + s + " " + i);
// Toast.makeText(classContext, msg, Toast.LENGTH_LONG).show();
textViewFW.append(msg + "\n");
}

@Override
public void hrNotificationReceived(String s, PolarHrData polarHrData) {
Log.d(TAG,"HR " + polarHrData.hr);
public void hrNotificationReceived(String s,
PolarHrData polarHrData) {
Log.d(TAG, "HR " + polarHrData.hr);
textViewHR.setText(String.valueOf(polarHrData.hr));
}

@Override
public void polarFtpFeatureReady(String s) {
Log.d(TAG,"Polar FTP ready " + s);
Log.d(TAG, "Polar FTP ready " + s);
}
});
api.connectToPolarDevice(DEVICE_ID);

plotter = new Plotter(this,"ECG");
plotter = new Plotter(this, "ECG");
plotter.setListener(this);

plot.addSeries(plotter.getSeries(), plotter.getFormatter());
plot.setRangeBoundaries(-3.3,3.3,BoundaryMode.FIXED);
plot.setRangeStep(StepMode.INCREMENT_BY_FIT,0.55);
plot.setDomainBoundaries(0,500, BoundaryMode.GROW);
plot.setRangeBoundaries(-3.3, 3.3, BoundaryMode.FIXED);
plot.setRangeStep(StepMode.INCREMENT_BY_FIT, 0.55);
plot.setDomainBoundaries(0, 500, BoundaryMode.GROW);
plot.setLinesPerRangeLabel(2);
}

Expand All @@ -141,37 +156,40 @@ public void onDestroy() {
api.shutDown();
}

public void streamECG(){
if( ecgDisposable == null ){
ecgDisposable = api.requestEcgSettings(DEVICE_ID).toFlowable().flatMap(new Function<PolarSensorSetting, Publisher<PolarEcgData>>() {
@Override
public Publisher<PolarEcgData> apply(PolarSensorSetting sensorSetting) throws Exception {
return api.startEcgStreaming(DEVICE_ID,sensorSetting.maxSettings());
}
}).observeOn(AndroidSchedulers.mainThread()).subscribe(
new Consumer<PolarEcgData>() {
@Override
public void accept(PolarEcgData polarEcgData) throws Exception {
Log.d(TAG,"ecg update");
for( Integer data : polarEcgData.samples ){
plotter.sendSingleSample((float) ((float)data/1000.0));
public void streamECG() {
if (ecgDisposable == null) {
ecgDisposable =
api.requestEcgSettings(DEVICE_ID).toFlowable().flatMap(new Function<PolarSensorSetting, Publisher<PolarEcgData>>() {
@Override
public Publisher<PolarEcgData> apply(PolarSensorSetting sensorSetting) throws Exception {
return api.startEcgStreaming(DEVICE_ID,
sensorSetting.maxSettings());
}
}
},
new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Log.e(TAG, ""+throwable.getLocalizedMessage());
ecgDisposable = null;
}
},
new Action() {
@Override
public void run() throws Exception {
Log.d(TAG, "complete");
}
}
);
}).observeOn(AndroidSchedulers.mainThread()).subscribe(
new Consumer<PolarEcgData>() {
@Override
public void accept(PolarEcgData polarEcgData) throws Exception {
Log.d(TAG, "ecg update");
for (Integer data : polarEcgData.samples) {
plotter.sendSingleSample((float) ((float) data / 1000.0));
}
}
},
new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Log.e(TAG,
"" + throwable.getLocalizedMessage());
ecgDisposable = null;
}
},
new Action() {
@Override
public void run() throws Exception {
Log.d(TAG, "complete");
}
}
);
} else {
// NOTE stops streaming if it is "running"
ecgDisposable.dispose();
Expand Down
Loading

0 comments on commit 0904de7

Please sign in to comment.