Advertisement
Guest User

Untitled

a guest
Nov 21st, 2022
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.90 KB | None | 0 0
  1. package com.example.gymappcompanion
  2.  
  3. import android.os.Bundle
  4. import android.os.Handler
  5. import android.widget.Button
  6. import androidx.appcompat.app.AppCompatActivity
  7. import com.garmin.android.connectiq.ConnectIQ
  8. import com.garmin.android.connectiq.IQApp
  9. import com.garmin.android.connectiq.IQDevice
  10. import kotlinx.coroutines.*
  11. import timber.log.Timber
  12.  
  13. class GarminConnectActivity : AppCompatActivity() {
  14. companion object {
  15. private const val GARMIN_WATCH_APP_ID = "11111111-d9eb-4b72-8f21-27b131d32276"
  16. // private const val GARMIN_WATCH_APP_ID = "11111111d9eb4b728f21-27b131d32276"
  17. }
  18.  
  19. private val parentJob = Job()
  20. private val coroutineScope = CoroutineScope(Dispatchers.Main + parentJob)
  21.  
  22. private lateinit var connectIQ: ConnectIQ
  23. private var iqApp: IQApp? = null
  24. private var device: IQDevice? = null
  25.  
  26. override fun onCreate(savedInstanceState: Bundle?) {
  27. super.onCreate(savedInstanceState)
  28. setContentView(R.layout.activity_garmin_connect)
  29.  
  30. connectIQ = ConnectIQ.getInstance(this, ConnectIQ.IQConnectType.WIRELESS);
  31. // connectIQ = ConnectIQ.getInstance(this, ConnectIQ.IQConnectType.TETHERED);
  32. connectIQ.initialize(this, true, initializationListener)
  33.  
  34. // fixme viewBinding
  35. findViewById<Button>(R.id.btn_test).setOnClickListener {
  36. sendMessage(5000)
  37. }
  38. }
  39.  
  40. val map = HashMap<Long, Long>()
  41.  
  42. private fun sendMessage(delay: Long) {
  43. Timber.i("sending massage with delay $delay")
  44. Handler().postDelayed({
  45. if (delay > 100) {
  46. sendMessage(delay - 10)
  47. }
  48. }, delay)
  49.  
  50. coroutineScope.launch(Dispatchers.Main) {
  51. sendTestMessage(delay)
  52. }
  53. }
  54.  
  55. private suspend fun sendTestMessage(delay: Long) {
  56. withContext(Dispatchers.IO) {
  57. val message = listOf("messageKeyFromPhone", "test2");
  58.  
  59. // connectIQ.sendMessage(device, iqApp, message, sendMessageListener)
  60. map[delay] = System.currentTimeMillis()
  61. connectIQ.sendMessage(device, iqApp, message, SendMessageListener(delay))
  62. }
  63. }
  64.  
  65. private fun getConnectedDevice(): IQDevice? {
  66. val connectedDevices = connectIQ.connectedDevices
  67.  
  68. return if (connectedDevices != null && connectedDevices.size > 0) {
  69. device = connectedDevices[0]
  70.  
  71. connectedDevices[0]
  72. } else {
  73. null
  74. }
  75. }
  76.  
  77. override fun onDestroy() {
  78. super.onDestroy()
  79.  
  80. val connectedDevice = getConnectedDevice()
  81.  
  82. if (connectedDevice != null) {
  83. connectIQ.unregisterForEvents(connectedDevice)
  84. }
  85. }
  86.  
  87. private fun verifyAppVersion(connectedDevice: IQDevice) {
  88. val value = object : ConnectIQ.IQApplicationInfoListener {
  89. override fun onApplicationInfoReceived(app: IQApp?) {
  90. iqApp = app
  91.  
  92. Timber.i("onApplicationInfoReceived $app")
  93.  
  94. if (app != null) {
  95. Timber.i("app.status = ${app.status}")
  96. Timber.i("app.version = ${app.version()}")
  97.  
  98. connectIQ.registerForAppEvents(connectedDevice, app, appEventsListener)
  99. } else {
  100. // fixme? check if even possible to get null here
  101. }
  102. }
  103.  
  104. override fun onApplicationNotInstalled(appId: String) {
  105. Timber.e("onApplicationNotInstalled")
  106. }
  107. }
  108. connectIQ.getApplicationInfo(
  109. GARMIN_WATCH_APP_ID,
  110. connectedDevice,
  111. value
  112. )
  113. }
  114.  
  115. private inner class SendMessageListener(val sentDelay: Long): ConnectIQ.IQSendMessageListener {
  116. override fun onMessageStatus(p0: IQDevice?, p1: IQApp?, status: ConnectIQ.IQMessageStatus?) {
  117. val timeInMillisMsgWasSent = map[sentDelay]!!
  118. val timeDiff = System.currentTimeMillis() - timeInMillisMsgWasSent
  119. Timber.i("sendMessageListener status = $status, timeDiff for delay $sentDelay is" +
  120. "$timeDiff")
  121. }
  122. }
  123.  
  124. private val sendMessageListener = ConnectIQ.IQSendMessageListener { device, app, status ->
  125. Timber.i("sendMessageListener status = $status")
  126. }
  127.  
  128. private val deviceEventListener = ConnectIQ.IQDeviceEventListener { device, deviceStatus ->
  129. Timber.i("device = $device, deviceStatus = $deviceStatus")
  130.  
  131. // todo show communication that watch is conntected / disconnected
  132. }
  133.  
  134. private val initializationListener = object : ConnectIQ.ConnectIQListener {
  135. override fun onSdkReady() {
  136. Timber.i("onSdkReady")
  137.  
  138. // fixme improve connecting process by handling all cases. never connected, connection in past but not now, connected
  139. connect()
  140. }
  141.  
  142. private fun connect() {
  143. val connectedDevice = getConnectedDevice()
  144.  
  145. if (connectedDevice != null) {
  146. connectIQ.registerForDeviceEvents(connectedDevice, deviceEventListener)
  147. verifyAppVersion(connectedDevice)
  148. } else {
  149. Timber.e("no connected device")
  150. // todo show communication that watch is disconnected
  151.  
  152. // fixme temporary fix for connecting to watch simulator over adb
  153. Handler().postDelayed({ connect() }, 5000)
  154. }
  155. }
  156.  
  157. override fun onInitializeError(errorStatus: ConnectIQ.IQSdkErrorStatus?) {
  158. Timber.e("onInitializeError $errorStatus")
  159. }
  160.  
  161. override fun onSdkShutDown() {
  162. Timber.i("onSdkShutDown")
  163. }
  164. }
  165.  
  166. private val appEventsListener =
  167. ConnectIQ.IQApplicationEventListener { device, app, message, status ->
  168. Timber.i("appEventsListener message received, status = $status, message = $message")
  169. }
  170.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement