aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/com/arslaancodes/zwznfreefit/DeviceActivity.kt
blob: 38862bde0d950489f6b619a1acc37a6c965b395e (plain)
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
package com.arslaancodes.zwznfreefit

import android.bluetooth.BluetoothAdapter
import android.os.Bundle
import android.widget.TextView
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity

class DeviceActivity : AppCompatActivity() {
	private lateinit var statusText: TextView
	private val handler = android.os.Handler(android.os.Looper.getMainLooper())
	private val syncRunnable = object : Runnable {
		override fun run() {
			BleManager.instance.syncTime()
			handler.postDelayed(this, 5 * 60 * 1000)
		}
	}

	override fun onResume() {
		super.onResume()
		handler.post(syncRunnable)
	}

	override fun onPause() {
		super.onPause()
		handler.removeCallbacks(syncRunnable)
	}

	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		setContentView(R.layout.activity_device)
		window.decorView.systemUiVisibility = (
			android.view.View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
			or android.view.View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
		)

		val address = intent.getStringExtra("device_address")
		val device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(address)

		statusText = findViewById(R.id.deviceStatusText)
		statusText.text = "Connected to: ${device.name ?: device.address}"
		BleManager.instance.syncTime();
		findViewById<Button>(R.id.syncTimeButton).setOnClickListener {
			BleManager.instance.syncTime()
		}

		findViewById<Button>(R.id.findBandButton).setOnClickListener {
			BleManager.instance.switchFindBand(0x01)
		}

		findViewById<Button>(R.id.stopFindBandButton).setOnClickListener {
			BleManager.instance.switchFindBand(0x00)
		}

		findViewById<Button>(R.id.configRealTimeMeasureHeartRateButton).setOnClickListener {
			BleManager.instance.configRealTimeMeasure(0x00, true, { data ->
				runOnUiThread {
					if (data[0] == 0x94.toByte()) {
						val heartRate = data[1].toInt() and 0xFF
						statusText.text = "Got generic measurement ${heartRate}"
					}
					if (data[0] == 0xE1.toByte()) {
						val rate1 = data[1].toInt() and 0xFF
						val rate2 = data[5].toInt() and 0xFF
						val rate3 = data[6].toInt() and 0xFF
						val rate4 = data[7].toInt() and 0xFF
						statusText.text = "Got aggregate rates ${rate1}, ${rate2}, ${rate3}, ${rate4}"
					}
				}
			})
		}
	}
}