-
Notifications
You must be signed in to change notification settings - Fork 153
/
MainActivity.kt
720 lines (667 loc) · 31.1 KB
/
MainActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
package com.polar.androidblesdk
import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.Toast
import androidx.annotation.StringRes
import androidx.appcompat.app.AppCompatActivity
import androidx.core.graphics.drawable.DrawableCompat
import androidx.core.util.Pair
import com.polar.sdk.api.PolarBleApi
import com.polar.sdk.api.PolarBleApi.DeviceStreamingFeature
import com.polar.sdk.api.PolarBleApiCallback
import com.polar.sdk.api.PolarBleApiDefaultImpl
import com.polar.sdk.api.errors.PolarInvalidArgument
import com.polar.sdk.api.model.*
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Flowable
import io.reactivex.rxjava3.core.Single
import io.reactivex.rxjava3.disposables.Disposable
import io.reactivex.rxjava3.functions.Function
import java.util.*
class MainActivity : AppCompatActivity() {
companion object {
private const val TAG = "MainActivity"
private const val API_LOGGER_TAG = "API LOGGER"
private const val PERMISSION_REQUEST_CODE = 1
}
// ATTENTION! Replace with the device ID from your device.
private var deviceId = "8C4CAD2D"
private val api: PolarBleApi by lazy {
// Notice PolarBleApi.ALL_FEATURES are enabled
PolarBleApiDefaultImpl.defaultImplementation(applicationContext, PolarBleApi.ALL_FEATURES)
}
private lateinit var broadcastDisposable: Disposable
private var scanDisposable: Disposable? = null
private var autoConnectDisposable: Disposable? = null
private var ecgDisposable: Disposable? = null
private var accDisposable: Disposable? = null
private var gyrDisposable: Disposable? = null
private var magDisposable: Disposable? = null
private var ppgDisposable: Disposable? = null
private var ppiDisposable: Disposable? = null
private var sdkModeEnableDisposable: Disposable? = null
private var sdkModeEnabledStatus = false
private var deviceConnected = false
private var bluetoothEnabled = false
private var exerciseEntry: PolarExerciseEntry? = null
private lateinit var broadcastButton: Button
private lateinit var connectButton: Button
private lateinit var autoConnectButton: Button
private lateinit var scanButton: Button
private lateinit var ecgButton: Button
private lateinit var accButton: Button
private lateinit var gyrButton: Button
private lateinit var magButton: Button
private lateinit var ppgButton: Button
private lateinit var ppiButton: Button
private lateinit var listExercisesButton: Button
private lateinit var readExerciseButton: Button
private lateinit var removeExerciseButton: Button
private lateinit var startH10RecordingButton: Button
private lateinit var stopH10RecordingButton: Button
private lateinit var readH10RecordingStatusButton: Button
private lateinit var setTimeButton: Button
private lateinit var toggleSdkModeButton: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Log.d(TAG, "version: " + PolarBleApiDefaultImpl.versionInfo())
broadcastButton = findViewById(R.id.broadcast_button)
connectButton = findViewById(R.id.connect_button)
autoConnectButton = findViewById(R.id.auto_connect_button)
scanButton = findViewById(R.id.scan_button)
ecgButton = findViewById(R.id.ecg_button)
accButton = findViewById(R.id.acc_button)
gyrButton = findViewById(R.id.gyr_button)
magButton = findViewById(R.id.mag_button)
ppgButton = findViewById(R.id.ohr_ppg_button)
ppiButton = findViewById(R.id.ohr_ppi_button)
listExercisesButton = findViewById(R.id.list_exercises)
readExerciseButton = findViewById(R.id.read_exercise)
removeExerciseButton = findViewById(R.id.remove_exercise)
startH10RecordingButton = findViewById(R.id.start_h10_recording)
stopH10RecordingButton = findViewById(R.id.stop_h10_recording)
readH10RecordingStatusButton = findViewById(R.id.h10_recording_status)
setTimeButton = findViewById(R.id.set_time)
toggleSdkModeButton = findViewById(R.id.toggle_SDK_mode)
api.setPolarFilter(false)
api.setApiLogger { s: String -> Log.d(API_LOGGER_TAG, s) }
api.setApiCallback(object : PolarBleApiCallback() {
override fun blePowerStateChanged(powered: Boolean) {
Log.d(TAG, "BLE power: $powered")
bluetoothEnabled = powered
if (powered) {
enableAllButtons()
showToast("Phone Bluetooth on")
} else {
disableAllButtons()
showToast("Phone Bluetooth off")
}
}
override fun deviceConnected(polarDeviceInfo: PolarDeviceInfo) {
Log.d(TAG, "CONNECTED: " + polarDeviceInfo.deviceId)
deviceId = polarDeviceInfo.deviceId
deviceConnected = true
val buttonText = getString(R.string.disconnect_from_device, deviceId)
toggleButtonDown(connectButton, buttonText)
}
override fun deviceConnecting(polarDeviceInfo: PolarDeviceInfo) {
Log.d(TAG, "CONNECTING: " + polarDeviceInfo.deviceId)
}
override fun deviceDisconnected(polarDeviceInfo: PolarDeviceInfo) {
Log.d(TAG, "DISCONNECTED: " + polarDeviceInfo.deviceId)
deviceConnected = false
val buttonText = getString(R.string.connect_to_device, deviceId)
toggleButtonUp(connectButton, buttonText)
toggleButtonUp(toggleSdkModeButton, R.string.enable_sdk_mode)
}
override fun streamingFeaturesReady(
identifier: String, features: Set<DeviceStreamingFeature>
) {
for (feature in features) {
Log.d(TAG, "Streaming feature $feature is ready")
}
}
override fun hrFeatureReady(identifier: String) {
Log.d(TAG, "HR READY: $identifier")
// hr notifications are about to start
}
override fun disInformationReceived(identifier: String, uuid: UUID, value: String) {
Log.d(TAG, "uuid: $uuid value: $value")
}
override fun batteryLevelReceived(identifier: String, level: Int) {
Log.d(TAG, "BATTERY LEVEL: $level")
}
override fun hrNotificationReceived(identifier: String, data: PolarHrData) {
Log.d(TAG, "HR value: ${data.hr} rrsMs: ${data.rrsMs} rr: ${data.rrs} contact: ${data.contactStatus} , ${data.contactStatusSupported}")
}
override fun polarFtpFeatureReady(s: String) {
Log.d(TAG, "FTP ready")
}
})
broadcastButton.setOnClickListener {
if (!this::broadcastDisposable.isInitialized || broadcastDisposable.isDisposed) {
toggleButtonDown(broadcastButton, R.string.listening_broadcast)
broadcastDisposable = api.startListenForPolarHrBroadcasts(null)
.subscribe(
{ polarBroadcastData: PolarHrBroadcastData ->
Log.d(TAG, "HR BROADCAST ${polarBroadcastData.polarDeviceInfo.deviceId} HR: ${polarBroadcastData.hr} batt: ${polarBroadcastData.batteryStatus}")
},
{ error: Throwable ->
toggleButtonUp(broadcastButton, R.string.listen_broadcast)
Log.e(TAG, "Broadcast listener failed. Reason $error")
},
{ Log.d(TAG, "complete") }
)
} else {
toggleButtonUp(broadcastButton, R.string.listen_broadcast)
broadcastDisposable.dispose()
}
}
connectButton.text = getString(R.string.connect_to_device, deviceId)
connectButton.setOnClickListener {
try {
if (deviceConnected) {
api.disconnectFromDevice(deviceId)
} else {
api.connectToDevice(deviceId)
}
} catch (polarInvalidArgument: PolarInvalidArgument) {
val attempt = if (deviceConnected) {
"disconnect"
} else {
"connect"
}
Log.e(TAG, "Failed to $attempt. Reason $polarInvalidArgument ")
}
}
autoConnectButton.setOnClickListener {
if (autoConnectDisposable != null) {
autoConnectDisposable?.dispose()
}
autoConnectDisposable = api.autoConnectToDevice(-50, "180D", null)
.subscribe(
{ Log.d(TAG, "auto connect search complete") },
{ throwable: Throwable -> Log.e(TAG, "" + throwable.toString()) }
)
}
scanButton.setOnClickListener {
val isDisposed = scanDisposable?.isDisposed ?: true
if (isDisposed) {
toggleButtonDown(scanButton, R.string.scanning_devices)
scanDisposable = api.searchForDevice()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ polarDeviceInfo: PolarDeviceInfo ->
Log.d(TAG, "polar device found id: " + polarDeviceInfo.deviceId + " address: " + polarDeviceInfo.address + " rssi: " + polarDeviceInfo.rssi + " name: " + polarDeviceInfo.name + " isConnectable: " + polarDeviceInfo.isConnectable)
},
{ error: Throwable ->
toggleButtonUp(scanButton, "Scan devices")
Log.e(TAG, "Device scan failed. Reason $error")
},
{ Log.d(TAG, "complete") }
)
} else {
toggleButtonUp(scanButton, "Scan devices")
scanDisposable?.dispose()
}
}
ecgButton.setOnClickListener {
val isDisposed = ecgDisposable?.isDisposed ?: true
if (isDisposed) {
toggleButtonDown(ecgButton, R.string.stop_ecg_stream)
ecgDisposable = requestStreamSettings(deviceId, DeviceStreamingFeature.ECG)
.flatMap { settings: PolarSensorSetting ->
api.startEcgStreaming(deviceId, settings)
}
.subscribe(
{ polarEcgData: PolarEcgData ->
for (microVolts in polarEcgData.samples) {
Log.d(TAG, " yV: $microVolts")
}
},
{ error: Throwable ->
toggleButtonUp(ecgButton, R.string.start_ecg_stream)
Log.e(TAG, "ECG stream failed. Reason $error")
},
{ Log.d(TAG, "ECG stream complete") }
)
} else {
toggleButtonUp(ecgButton, R.string.start_ecg_stream)
// NOTE stops streaming if it is "running"
ecgDisposable?.dispose()
}
}
accButton.setOnClickListener {
val isDisposed = accDisposable?.isDisposed ?: true
if (isDisposed) {
toggleButtonDown(accButton, R.string.stop_acc_stream)
accDisposable = requestStreamSettings(deviceId, DeviceStreamingFeature.ACC)
.flatMap { settings: PolarSensorSetting ->
api.startAccStreaming(deviceId, settings)
}
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ polarAccelerometerData: PolarAccelerometerData ->
for (data in polarAccelerometerData.samples) {
Log.d(TAG, "ACC x: ${data.x} y: ${data.y} z: ${data.z}")
}
},
{ error: Throwable ->
toggleButtonUp(accButton, R.string.start_acc_stream)
Log.e(TAG, "ACC stream failed. Reason $error")
},
{
showToast("ACC stream complete")
Log.d(TAG, "ACC stream complete")
}
)
} else {
toggleButtonUp(accButton, R.string.start_acc_stream)
// NOTE dispose will stop streaming if it is "running"
accDisposable?.dispose()
}
}
gyrButton.setOnClickListener {
val isDisposed = gyrDisposable?.isDisposed ?: true
if (isDisposed) {
toggleButtonDown(gyrButton, R.string.stop_gyro_stream)
gyrDisposable =
requestStreamSettings(deviceId, DeviceStreamingFeature.GYRO)
.flatMap { settings: PolarSensorSetting ->
api.startGyroStreaming(deviceId, settings)
}
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ polarGyroData: PolarGyroData ->
for (data in polarGyroData.samples) {
Log.d(TAG, "GYR x: ${data.x} y: ${data.y} z: ${data.z}")
}
},
{ error: Throwable ->
toggleButtonUp(gyrButton, R.string.start_gyro_stream)
Log.e(TAG, "GYR stream failed. Reason $error")
},
{ Log.d(TAG, "GYR stream complete") }
)
} else {
toggleButtonUp(gyrButton, R.string.start_gyro_stream)
// NOTE dispose will stop streaming if it is "running"
gyrDisposable?.dispose()
}
}
magButton.setOnClickListener {
val isDisposed = magDisposable?.isDisposed ?: true
if (isDisposed) {
toggleButtonDown(magButton, R.string.stop_mag_stream)
magDisposable =
requestStreamSettings(deviceId, DeviceStreamingFeature.MAGNETOMETER)
.flatMap { settings: PolarSensorSetting ->
api.startMagnetometerStreaming(deviceId, settings)
}
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ polarMagData: PolarMagnetometerData ->
for (data in polarMagData.samples) {
Log.d(TAG, "MAG x: ${data.x} y: ${data.y} z: ${data.z}")
}
},
{ error: Throwable ->
toggleButtonUp(magButton, R.string.start_mag_stream)
Log.e(TAG, "MAGNETOMETER stream failed. Reason $error")
},
{ Log.d(TAG, "MAGNETOMETER stream complete") }
)
} else {
toggleButtonUp(magButton, R.string.start_mag_stream)
// NOTE dispose will stop streaming if it is "running"
magDisposable!!.dispose()
}
}
ppgButton.setOnClickListener {
val isDisposed = ppgDisposable?.isDisposed ?: true
if (isDisposed) {
toggleButtonDown(ppgButton, R.string.stop_ppg_stream)
ppgDisposable =
requestStreamSettings(deviceId, DeviceStreamingFeature.PPG)
.flatMap { settings: PolarSensorSetting ->
api.startOhrStreaming(deviceId, settings)
}
.subscribe(
{ polarOhrPPGData: PolarOhrData ->
if (polarOhrPPGData.type == PolarOhrData.OHR_DATA_TYPE.PPG3_AMBIENT1) {
for (data in polarOhrPPGData.samples) {
Log.d(TAG, "PPG ppg0: ${data.channelSamples[0]} ppg1: ${data.channelSamples[1]} ppg2: ${data.channelSamples[2]} ambient: ${data.channelSamples[3]}")
}
}
},
{ error: Throwable ->
toggleButtonUp(ppgButton, R.string.start_ppg_stream)
Log.e(TAG, "PPG stream failed. Reason $error")
},
{ Log.d(TAG, "PPG stream complete") }
)
} else {
toggleButtonUp(ppgButton, R.string.start_ppg_stream)
// NOTE dispose will stop streaming if it is "running"
ppgDisposable?.dispose()
}
}
ppiButton.setOnClickListener {
val isDisposed = ppiDisposable?.isDisposed ?: true
if (isDisposed) {
toggleButtonDown(ppiButton, R.string.stop_ppi_stream)
ppiDisposable = api.startOhrPPIStreaming(deviceId)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ ppiData: PolarOhrPPIData ->
for (sample in ppiData.samples) {
Log.d(TAG, "PPI ppi: ${sample.ppi} blocker: ${sample.blockerBit} errorEstimate: ${sample.errorEstimate}")
}
},
{ error: Throwable ->
toggleButtonUp(ppiButton, R.string.start_ppi_stream)
Log.e(TAG, "PPI stream failed. Reason $error")
},
{ Log.d(TAG, "PPI stream complete") }
)
} else {
toggleButtonUp(ppiButton, R.string.start_ppi_stream)
// NOTE dispose will stop streaming if it is "running"
ppiDisposable?.dispose()
}
}
listExercisesButton.setOnClickListener {
api.listExercises(deviceId)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ polarExerciseEntry: PolarExerciseEntry ->
Log.d(TAG, "next: ${polarExerciseEntry.date} path: ${polarExerciseEntry.path} id: ${polarExerciseEntry.identifier}")
exerciseEntry = polarExerciseEntry
},
{ error: Throwable -> Log.e(TAG, "Failed to list exercises: $error") },
{ Log.d(TAG, "list exercises complete") }
)
}
readExerciseButton.setOnClickListener {
exerciseEntry?.let { entry ->
api.fetchExercise(deviceId, entry)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ polarExerciseData: PolarExerciseData ->
Log.d(TAG, "exercise data count: ${polarExerciseData.hrSamples.size} samples: ${polarExerciseData.hrSamples}")
},
{ error: Throwable ->
val errorDescription = "Failed to read exercise. Reason: $error"
Log.e(TAG, errorDescription)
showToast(errorDescription)
}
)
} ?: run {
val help = "No exercise to read, please list the exercises first"
showToast(help)
Log.e(TAG, help)
}
}
removeExerciseButton.setOnClickListener {
exerciseEntry?.let { entry ->
api.removeExercise(deviceId, entry)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{
exerciseEntry = null
Log.d(TAG, "ex removed ok")
},
{ error: Throwable ->
Log.d(TAG, "ex remove failed: $error")
}
)
} ?: run {
val help = "No exercise to remove, please list the exercises first"
showToast(help)
Log.e(TAG, help)
}
}
startH10RecordingButton.setOnClickListener {
api.startRecording(
deviceId,
"TEST_APP_ID",
PolarBleApi.RecordingInterval.INTERVAL_1S,
PolarBleApi.SampleType.HR
)
.subscribe(
{ Log.d(TAG, "recording started") },
{ error: Throwable ->
Log.e(TAG, "recording start failed: $error")
})
}
stopH10RecordingButton.setOnClickListener {
api.stopRecording(deviceId)
.subscribe(
{ Log.d(TAG, "recording stopped") },
{ error: Throwable -> Log.e(TAG, "recording stop failed: $error") }
)
}
readH10RecordingStatusButton.setOnClickListener {
api.requestRecordingStatus(deviceId)
.subscribe(
{ pair: Pair<Boolean, String> ->
Log.d(TAG, "recording on: ${pair.first} ID: ${pair.second}")
},
{ error: Throwable -> Log.e(TAG, "recording status failed: $error") }
)
}
setTimeButton.setOnClickListener {
val calendar = Calendar.getInstance()
calendar.time = Date()
api.setLocalTime(deviceId, calendar)
.subscribe(
{ Log.d(TAG, "time ${calendar.time} set to device") },
{ error: Throwable -> Log.d(TAG, "set time failed: $error") }
)
}
toggleSdkModeButton.setOnClickListener {
toggleSdkModeButton.isEnabled = false
if (!sdkModeEnabledStatus) {
sdkModeEnableDisposable = api.enableSDKMode(deviceId)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{
Log.d(TAG, "SDK mode enabled")
// at this point dispose all existing streams. SDK mode enable command
// stops all the streams but client is not informed. This is workaround
// for the bug.
disposeAllStreams()
toggleSdkModeButton.isEnabled = true
sdkModeEnabledStatus = true
toggleButtonDown(toggleSdkModeButton, R.string.disable_sdk_mode)
},
{ error ->
toggleSdkModeButton.isEnabled = true
val errorString = "SDK mode enable failed: $error"
showToast(errorString)
Log.e(TAG, errorString)
}
)
} else {
sdkModeEnableDisposable = api.disableSDKMode(deviceId)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{
Log.d(TAG, "SDK mode disabled")
toggleSdkModeButton.isEnabled = true
sdkModeEnabledStatus = false
toggleButtonUp(toggleSdkModeButton, R.string.enable_sdk_mode)
},
{ error ->
toggleSdkModeButton.isEnabled = true
val errorString = "SDK mode disable failed: $error"
showToast(errorString)
Log.e(TAG, errorString)
}
)
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
requestPermissions(arrayOf(Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT), PERMISSION_REQUEST_CODE)
} else {
requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), PERMISSION_REQUEST_CODE)
}
}
requestPermissions(arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION), PERMISSION_REQUEST_CODE)
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == PERMISSION_REQUEST_CODE) {
for (index in 0..grantResults.lastIndex) {
if (grantResults[index] == PackageManager.PERMISSION_DENIED) {
disableAllButtons()
Log.w(TAG, "No sufficient permissions")
showToast("No sufficient permissions")
return
}
}
Log.d(TAG, "Needed permissions are granted")
enableAllButtons()
}
}
public override fun onPause() {
super.onPause()
api.backgroundEntered()
}
public override fun onResume() {
super.onResume()
api.foregroundEntered()
}
public override fun onDestroy() {
super.onDestroy()
api.shutDown()
}
private fun toggleButtonDown(button: Button, text: String? = null) {
toggleButton(button, true, text)
}
private fun toggleButtonDown(button: Button, @StringRes resourceId: Int) {
toggleButton(button, true, getString(resourceId))
}
private fun toggleButtonUp(button: Button, text: String? = null) {
toggleButton(button, false, text)
}
private fun toggleButtonUp(button: Button, @StringRes resourceId: Int) {
toggleButton(button, false, getString(resourceId))
}
private fun toggleButton(button: Button, isDown: Boolean, text: String? = null) {
if (text != null) button.text = text
var buttonDrawable = button.background
buttonDrawable = DrawableCompat.wrap(buttonDrawable!!)
if (isDown) {
DrawableCompat.setTint(buttonDrawable, resources.getColor(R.color.primaryDarkColor))
} else {
DrawableCompat.setTint(buttonDrawable, resources.getColor(R.color.primaryColor))
}
button.background = buttonDrawable
}
private fun requestStreamSettings(
identifier: String,
feature: DeviceStreamingFeature
): Flowable<PolarSensorSetting> {
val availableSettings = api.requestStreamSettings(identifier, feature)
.observeOn(AndroidSchedulers.mainThread())
.onErrorReturn { error: Throwable ->
val errorString = "Settings are not available for feature $feature. REASON: $error"
Log.w(TAG, errorString)
showToast(errorString)
PolarSensorSetting(emptyMap())
}
val allSettings = api.requestFullStreamSettings(identifier, feature)
.onErrorReturn { error: Throwable ->
Log.w(
TAG,
"Full stream settings are not available for feature $feature. REASON: $error"
)
PolarSensorSetting(emptyMap())
}
return Single.zip(
availableSettings,
allSettings,
{ available: PolarSensorSetting, all: PolarSensorSetting ->
if (available.settings.isEmpty()) {
throw Throwable("Settings are not available")
} else {
Log.d(TAG, "Feature " + feature + " available settings " + available.settings)
Log.d(TAG, "Feature " + feature + " all settings " + all.settings)
return@zip android.util.Pair(available, all)
}
}
)
.observeOn(AndroidSchedulers.mainThread())
.toFlowable()
.flatMap(
Function { sensorSettings: android.util.Pair<PolarSensorSetting, PolarSensorSetting> ->
DialogUtility.showAllSettingsDialog(
this@MainActivity,
sensorSettings.first.settings,
sensorSettings.second.settings
).toFlowable()
} as Function<android.util.Pair<PolarSensorSetting, PolarSensorSetting>, Flowable<PolarSensorSetting>>
)
}
private fun showToast(message: String) {
val toast = Toast.makeText(applicationContext, message, Toast.LENGTH_LONG)
toast.show()
}
private fun disableAllButtons() {
broadcastButton.isEnabled = false
connectButton.isEnabled = false
autoConnectButton.isEnabled = false
scanButton.isEnabled = false
ecgButton.isEnabled = false
accButton.isEnabled = false
gyrButton.isEnabled = false
magButton.isEnabled = false
ppgButton.isEnabled = false
ppiButton.isEnabled = false
listExercisesButton.isEnabled = false
readExerciseButton.isEnabled = false
removeExerciseButton.isEnabled = false
startH10RecordingButton.isEnabled = false
stopH10RecordingButton.isEnabled = false
readH10RecordingStatusButton.isEnabled = false
setTimeButton.isEnabled = false
toggleSdkModeButton.isEnabled = false
}
private fun enableAllButtons() {
broadcastButton.isEnabled = true
connectButton.isEnabled = true
autoConnectButton.isEnabled = true
scanButton.isEnabled = true
ecgButton.isEnabled = true
accButton.isEnabled = true
gyrButton.isEnabled = true
magButton.isEnabled = true
ppgButton.isEnabled = true
ppiButton.isEnabled = true
listExercisesButton.isEnabled = true
readExerciseButton.isEnabled = true
removeExerciseButton.isEnabled = true
startH10RecordingButton.isEnabled = true
stopH10RecordingButton.isEnabled = true
readH10RecordingStatusButton.isEnabled = true
setTimeButton.isEnabled = true
toggleSdkModeButton.isEnabled = true
}
private fun disposeAllStreams() {
ecgDisposable?.dispose()
accDisposable?.dispose()
gyrDisposable?.dispose()
magDisposable?.dispose()
ppgDisposable?.dispose()
ppgDisposable?.dispose()
}
}