From b0e8fc0c6fe2e75059ca9abe5b7c90abc43ad81e Mon Sep 17 00:00:00 2001 From: Azalea Gui Date: Mon, 23 Jan 2023 16:35:16 -0500 Subject: [PATCH] [M] Move collection out of activity scan --- .../java/org/hydev/wearsync/ActivityScan.kt | 87 ----------------- .../java/org/hydev/wearsync/MainActivity.kt | 93 +++++++++++++++++++ app/src/main/res/layout/activity_scan.xml | 12 --- 3 files changed, 93 insertions(+), 99 deletions(-) diff --git a/app/src/main/java/org/hydev/wearsync/ActivityScan.kt b/app/src/main/java/org/hydev/wearsync/ActivityScan.kt index 474f6b9..c25abb8 100644 --- a/app/src/main/java/org/hydev/wearsync/ActivityScan.kt +++ b/app/src/main/java/org/hydev/wearsync/ActivityScan.kt @@ -8,21 +8,13 @@ import android.widget.ArrayAdapter import androidx.appcompat.app.AppCompatActivity import com.welie.blessed.BluetoothPeripheral import kotlinx.coroutines.* -import kotlinx.coroutines.flow.consumeAsFlow -import org.hydev.wearsync.bles.BluetoothHandler import org.hydev.wearsync.bles.BluetoothHandler.Companion.ble -import org.hydev.wearsync.bles.ObservationUnit import org.hydev.wearsync.databinding.ActivityScanBinding -import java.text.DateFormat -import java.text.SimpleDateFormat import java.util.* class ActivityScan : AppCompatActivity() { lateinit var binding: ActivityScanBinding - private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO) - private val dateFormat: DateFormat = SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.ENGLISH) - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityScanBinding.inflate(layoutInflater) @@ -80,84 +72,5 @@ class ActivityScan : AppCompatActivity() { view.snack("✅ Connected.") } } - - collectHeartRate(ble) - collectPulseOxContinuous(ble) - collectPulseOxSpot(ble) - collectTemperature(ble) - collectWeight(ble) - } - - private fun collectHeartRate(bluetoothHandler: BluetoothHandler) { - scope.launch { - bluetoothHandler.heartRateChannel.consumeAsFlow().collect { - withContext(Dispatchers.Main) { - binding.mainText.text = String.format(Locale.ENGLISH, "%d bpm", it.pulse) - } - } - } - } - - private fun collectPulseOxContinuous(bluetoothHandler: BluetoothHandler) { - scope.launch { - bluetoothHandler.pulseOxContinuousChannel.consumeAsFlow().collect { - withContext(Dispatchers.Main) { - binding.mainText.text = String.format( - Locale.ENGLISH, - "SpO2 %d%%, Pulse %d bpm\n%s\n\nfrom %s", - it.spO2, - it.pulseRate, - dateFormat.format(Calendar.getInstance()) - ) - } - } - } - } - - private fun collectPulseOxSpot(bluetoothHandler: BluetoothHandler) { - scope.launch { - bluetoothHandler.pulseOxSpotChannel.consumeAsFlow().collect { - withContext(Dispatchers.Main) { - binding.mainText.text = String.format( - Locale.ENGLISH, - "SpO2 %d%%, Pulse %d bpm\n", - it.spO2, - it.pulseRate - ) - } - } - } - } - - private fun collectTemperature(bluetoothHandler: BluetoothHandler) { - scope.launch { - bluetoothHandler.temperatureChannel.consumeAsFlow().collect { - withContext(Dispatchers.Main) { - binding.mainText.text = String.format( - Locale.ENGLISH, - "%.1f %s (%s)\n%s\n", - it.temperatureValue, - if (it.unit == ObservationUnit.Celsius) "celsius" else "fahrenheit", - it.type, - dateFormat.format(it.timestamp ?: Calendar.getInstance()) - ) - } - } - } - } - - private fun collectWeight(bluetoothHandler: BluetoothHandler) { - scope.launch { - bluetoothHandler.weightChannel.consumeAsFlow().collect { - withContext(Dispatchers.Main) { - binding.mainText.text = String.format( - Locale.ENGLISH, - "%.1f %s\n%s\n", - it.weight, it.unit.toString(), - dateFormat.format(it.timestamp ?: Calendar.getInstance()) - ) - } - } - } } } \ No newline at end of file diff --git a/app/src/main/java/org/hydev/wearsync/MainActivity.kt b/app/src/main/java/org/hydev/wearsync/MainActivity.kt index c904a0e..e896fd7 100644 --- a/app/src/main/java/org/hydev/wearsync/MainActivity.kt +++ b/app/src/main/java/org/hydev/wearsync/MainActivity.kt @@ -10,8 +10,16 @@ import android.view.MenuItem import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AppCompatActivity import com.google.android.material.snackbar.Snackbar +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.consumeAsFlow import org.hydev.wearsync.ActivityPermissions.Companion.hasPermissions +import org.hydev.wearsync.bles.BluetoothHandler +import org.hydev.wearsync.bles.BluetoothHandler.Companion.ble +import org.hydev.wearsync.bles.ObservationUnit import org.hydev.wearsync.databinding.ActivityMainBinding +import java.text.DateFormat +import java.text.SimpleDateFormat +import java.util.* class MainActivity : AppCompatActivity() { @@ -85,4 +93,89 @@ class MainActivity : AppCompatActivity() else -> super.onOptionsItemSelected(item) } } + + private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO) + private val dateFormat: DateFormat = SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.ENGLISH) + + fun startCollect() + { + collectHeartRate(ble) + collectPulseOxContinuous(ble) + collectPulseOxSpot(ble) + collectTemperature(ble) + collectWeight(ble) + } + + private fun collectHeartRate(bluetoothHandler: BluetoothHandler) { + scope.launch { + bluetoothHandler.heartRateChannel.consumeAsFlow().collect { + withContext(Dispatchers.Main) { + binding.content.tvValue.text = String.format(Locale.ENGLISH, "%d bpm", it.pulse) + } + } + } + } + + private fun collectPulseOxContinuous(bluetoothHandler: BluetoothHandler) { + scope.launch { + bluetoothHandler.pulseOxContinuousChannel.consumeAsFlow().collect { + withContext(Dispatchers.Main) { + binding.content.tvValue.text = String.format( + Locale.ENGLISH, + "SpO2 %d%%, Pulse %d bpm\n%s\n\nfrom %s", + it.spO2, + it.pulseRate, + dateFormat.format(Calendar.getInstance()) + ) + } + } + } + } + + private fun collectPulseOxSpot(bluetoothHandler: BluetoothHandler) { + scope.launch { + bluetoothHandler.pulseOxSpotChannel.consumeAsFlow().collect { + withContext(Dispatchers.Main) { + binding.content.tvValue.text = String.format( + Locale.ENGLISH, + "SpO2 %d%%, Pulse %d bpm\n", + it.spO2, + it.pulseRate + ) + } + } + } + } + + private fun collectTemperature(bluetoothHandler: BluetoothHandler) { + scope.launch { + bluetoothHandler.temperatureChannel.consumeAsFlow().collect { + withContext(Dispatchers.Main) { + binding.content.tvValue.text = String.format( + Locale.ENGLISH, + "%.1f %s (%s)\n%s\n", + it.temperatureValue, + if (it.unit == ObservationUnit.Celsius) "celsius" else "fahrenheit", + it.type, + dateFormat.format(it.timestamp ?: Calendar.getInstance()) + ) + } + } + } + } + + private fun collectWeight(bluetoothHandler: BluetoothHandler) { + scope.launch { + bluetoothHandler.weightChannel.consumeAsFlow().collect { + withContext(Dispatchers.Main) { + binding.content.tvValue.text = String.format( + Locale.ENGLISH, + "%.1f %s\n%s\n", + it.weight, it.unit.toString(), + dateFormat.format(it.timestamp ?: Calendar.getInstance()) + ) + } + } + } + } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_scan.xml b/app/src/main/res/layout/activity_scan.xml index df38a03..c3112b3 100644 --- a/app/src/main/res/layout/activity_scan.xml +++ b/app/src/main/res/layout/activity_scan.xml @@ -6,18 +6,6 @@ android:layout_height="match_parent" tools:context=".ActivityScan"> - -