Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.gymappcompanion
- import android.os.Bundle
- import android.os.Handler
- import android.widget.Button
- import androidx.appcompat.app.AppCompatActivity
- import com.garmin.android.connectiq.ConnectIQ
- import com.garmin.android.connectiq.IQApp
- import com.garmin.android.connectiq.IQDevice
- import kotlinx.coroutines.*
- import timber.log.Timber
- class GarminConnectActivity : AppCompatActivity() {
- companion object {
- private const val GARMIN_WATCH_APP_ID = "11111111-d9eb-4b72-8f21-27b131d32276"
- // private const val GARMIN_WATCH_APP_ID = "11111111d9eb4b728f21-27b131d32276"
- }
- private val parentJob = Job()
- private val coroutineScope = CoroutineScope(Dispatchers.Main + parentJob)
- private lateinit var connectIQ: ConnectIQ
- private var iqApp: IQApp? = null
- private var device: IQDevice? = null
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_garmin_connect)
- connectIQ = ConnectIQ.getInstance(this, ConnectIQ.IQConnectType.WIRELESS);
- // connectIQ = ConnectIQ.getInstance(this, ConnectIQ.IQConnectType.TETHERED);
- connectIQ.initialize(this, true, initializationListener)
- // fixme viewBinding
- findViewById<Button>(R.id.btn_test).setOnClickListener {
- sendMessage(5000)
- }
- }
- val map = HashMap<Long, Long>()
- private fun sendMessage(delay: Long) {
- Timber.i("sending massage with delay $delay")
- Handler().postDelayed({
- if (delay > 100) {
- sendMessage(delay - 10)
- }
- }, delay)
- coroutineScope.launch(Dispatchers.Main) {
- sendTestMessage(delay)
- }
- }
- private suspend fun sendTestMessage(delay: Long) {
- withContext(Dispatchers.IO) {
- val message = listOf("messageKeyFromPhone", "test2");
- // connectIQ.sendMessage(device, iqApp, message, sendMessageListener)
- map[delay] = System.currentTimeMillis()
- connectIQ.sendMessage(device, iqApp, message, SendMessageListener(delay))
- }
- }
- private fun getConnectedDevice(): IQDevice? {
- val connectedDevices = connectIQ.connectedDevices
- return if (connectedDevices != null && connectedDevices.size > 0) {
- device = connectedDevices[0]
- connectedDevices[0]
- } else {
- null
- }
- }
- override fun onDestroy() {
- super.onDestroy()
- val connectedDevice = getConnectedDevice()
- if (connectedDevice != null) {
- connectIQ.unregisterForEvents(connectedDevice)
- }
- }
- private fun verifyAppVersion(connectedDevice: IQDevice) {
- val value = object : ConnectIQ.IQApplicationInfoListener {
- override fun onApplicationInfoReceived(app: IQApp?) {
- iqApp = app
- Timber.i("onApplicationInfoReceived $app")
- if (app != null) {
- Timber.i("app.status = ${app.status}")
- Timber.i("app.version = ${app.version()}")
- connectIQ.registerForAppEvents(connectedDevice, app, appEventsListener)
- } else {
- // fixme? check if even possible to get null here
- }
- }
- override fun onApplicationNotInstalled(appId: String) {
- Timber.e("onApplicationNotInstalled")
- }
- }
- connectIQ.getApplicationInfo(
- GARMIN_WATCH_APP_ID,
- connectedDevice,
- value
- )
- }
- private inner class SendMessageListener(val sentDelay: Long): ConnectIQ.IQSendMessageListener {
- override fun onMessageStatus(p0: IQDevice?, p1: IQApp?, status: ConnectIQ.IQMessageStatus?) {
- val timeInMillisMsgWasSent = map[sentDelay]!!
- val timeDiff = System.currentTimeMillis() - timeInMillisMsgWasSent
- Timber.i("sendMessageListener status = $status, timeDiff for delay $sentDelay is" +
- "$timeDiff")
- }
- }
- private val sendMessageListener = ConnectIQ.IQSendMessageListener { device, app, status ->
- Timber.i("sendMessageListener status = $status")
- }
- private val deviceEventListener = ConnectIQ.IQDeviceEventListener { device, deviceStatus ->
- Timber.i("device = $device, deviceStatus = $deviceStatus")
- // todo show communication that watch is conntected / disconnected
- }
- private val initializationListener = object : ConnectIQ.ConnectIQListener {
- override fun onSdkReady() {
- Timber.i("onSdkReady")
- // fixme improve connecting process by handling all cases. never connected, connection in past but not now, connected
- connect()
- }
- private fun connect() {
- val connectedDevice = getConnectedDevice()
- if (connectedDevice != null) {
- connectIQ.registerForDeviceEvents(connectedDevice, deviceEventListener)
- verifyAppVersion(connectedDevice)
- } else {
- Timber.e("no connected device")
- // todo show communication that watch is disconnected
- // fixme temporary fix for connecting to watch simulator over adb
- Handler().postDelayed({ connect() }, 5000)
- }
- }
- override fun onInitializeError(errorStatus: ConnectIQ.IQSdkErrorStatus?) {
- Timber.e("onInitializeError $errorStatus")
- }
- override fun onSdkShutDown() {
- Timber.i("onSdkShutDown")
- }
- }
- private val appEventsListener =
- ConnectIQ.IQApplicationEventListener { device, app, message, status ->
- Timber.i("appEventsListener message received, status = $status, message = $message")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement