[M] Move collection out of activity scan

This commit is contained in:
Azalea Gui
2023-01-23 16:35:16 -05:00
parent ed6c8403da
commit b0e8fc0c6f
3 changed files with 93 additions and 99 deletions
@@ -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())
)
}
}
}
}
}
@@ -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())
)
}
}
}
}
}
-12
View File
@@ -6,18 +6,6 @@
android:layout_height="match_parent"
tools:context=".ActivityScan">
<TextView
android:id="@+id/mainText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Waiting for measurement..."
android:textAlignment="center"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvPaired"
android:layout_width="wrap_content"