feat: Reconnect button (#6)
This commit is contained in:
parent
eddcf47205
commit
dbfb08412d
3 changed files with 58 additions and 20 deletions
|
@ -48,6 +48,14 @@ class Garmin(
|
||||||
locations.start()
|
locations.start()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun reconnect() {
|
||||||
|
Log.d(TAG, "reconnect pressed")
|
||||||
|
if (!connection.isConnecting.get()) {
|
||||||
|
Log.d(TAG, "reconnection enqueued")
|
||||||
|
context.runAsync { connection.tryConnect() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun close() {
|
fun close() {
|
||||||
connection.close()
|
connection.close()
|
||||||
|
|
||||||
|
@ -185,6 +193,7 @@ class Garmin(
|
||||||
|
|
||||||
private inner class ConnectThread : Thread() {
|
private inner class ConnectThread : Thread() {
|
||||||
|
|
||||||
|
val isConnecting = AtomicBoolean(false)
|
||||||
private val queue: SynchronousQueue<IntArray> = SynchronousQueue()
|
private val queue: SynchronousQueue<IntArray> = SynchronousQueue()
|
||||||
private val state = AtomicBoolean(false)
|
private val state = AtomicBoolean(false)
|
||||||
private var current: IntArray = intArrayOf()
|
private var current: IntArray = intArrayOf()
|
||||||
|
@ -195,24 +204,30 @@ class Garmin(
|
||||||
}
|
}
|
||||||
|
|
||||||
fun tryConnect() {
|
fun tryConnect() {
|
||||||
context.checkBt()
|
Log.d(TAG, "Connection requested")
|
||||||
// Cancel discovery because it otherwise slows down the connection.
|
if (!isConnecting.get()) {
|
||||||
adapter.cancelDiscovery()
|
Log.d(TAG, "Trying to connect...")
|
||||||
try {
|
isConnecting.set(true)
|
||||||
socket?.connect()
|
context.checkBt()
|
||||||
context.toastConnectionStatus(true)
|
// Cancel discovery because it otherwise slows down the connection.
|
||||||
sleep(3000)
|
adapter.cancelDiscovery()
|
||||||
readAll()
|
try {
|
||||||
send(intArrayOf(0x07, 0x01)) // Set GPS to true
|
socket?.connect()
|
||||||
send(intArrayOf(0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)) // Clear screen
|
context.toastConnectionStatus(true)
|
||||||
state.set(true)
|
sleep(3000)
|
||||||
} catch (e: IOException) {
|
readAll()
|
||||||
Log.d(TAG, "Not connected", e)
|
send(intArrayOf(0x07, 0x01)) // Set GPS to true
|
||||||
context.toastConnectionStatus(false)
|
send(intArrayOf(0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)) // Clear screen
|
||||||
state.set(false)
|
state.set(true)
|
||||||
|
} catch (e: IOException) {
|
||||||
|
Log.d(TAG, "Not connected", e)
|
||||||
|
context.toastConnectionStatus(false)
|
||||||
|
state.set(false)
|
||||||
|
}
|
||||||
|
queue.clear()
|
||||||
|
current = intArrayOf()
|
||||||
|
isConnecting.set(false)
|
||||||
}
|
}
|
||||||
queue.clear()
|
|
||||||
current = intArrayOf()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun run() {
|
override fun run() {
|
||||||
|
@ -262,9 +277,13 @@ class Garmin(
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun sendRaw(buff: IntArray) {
|
private fun sendRaw(buff: IntArray) {
|
||||||
buff.forEach { socket!!.outputStream.write(it) }
|
try {
|
||||||
socket!!.outputStream.flush()
|
buff.forEach { socket!!.outputStream.write(it) }
|
||||||
readAll()
|
socket!!.outputStream.flush()
|
||||||
|
readAll()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.d(TAG, "sendRaw: ${e.message}")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@ import androidx.activity.result.contract.ActivityResultContracts.StartActivityFo
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.core.app.ActivityCompat
|
import androidx.core.app.ActivityCompat
|
||||||
import androidx.lifecycle.DefaultLifecycleObserver
|
import androidx.lifecycle.DefaultLifecycleObserver
|
||||||
|
import androidx.lifecycle.lifecycleScope
|
||||||
import com.mapbox.navigation.base.options.NavigationOptions
|
import com.mapbox.navigation.base.options.NavigationOptions
|
||||||
import com.mapbox.navigation.core.MapboxNavigation
|
import com.mapbox.navigation.core.MapboxNavigation
|
||||||
import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp
|
import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp
|
||||||
|
@ -25,6 +26,9 @@ import com.mapbox.navigation.core.lifecycle.requireMapboxNavigation
|
||||||
import eu.ztsh.garmin.databinding.ActivityMainBinding
|
import eu.ztsh.garmin.databinding.ActivityMainBinding
|
||||||
import eu.ztsh.garmin.mapbox.MapControl
|
import eu.ztsh.garmin.mapbox.MapControl
|
||||||
import eu.ztsh.garmin.util.PermissionsHelper
|
import eu.ztsh.garmin.util.PermissionsHelper
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
import java.lang.ref.WeakReference
|
import java.lang.ref.WeakReference
|
||||||
|
|
||||||
|
|
||||||
|
@ -124,6 +128,14 @@ class MainActivity : AppCompatActivity() {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun runAsync(action: () -> Unit) {
|
||||||
|
lifecycleScope.launch {
|
||||||
|
withContext(Dispatchers.IO) {
|
||||||
|
action()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun toastConnectionStatus(success: Boolean) {
|
fun toastConnectionStatus(success: Boolean) {
|
||||||
this.runOnUiThread {
|
this.runOnUiThread {
|
||||||
if (success) {
|
if (success) {
|
||||||
|
|
|
@ -22,6 +22,7 @@ import com.mapbox.navigation.ui.maps.camera.data.MapboxNavigationViewportDataSou
|
||||||
import com.mapbox.navigation.ui.maps.camera.lifecycle.NavigationBasicGesturesHandler
|
import com.mapbox.navigation.ui.maps.camera.lifecycle.NavigationBasicGesturesHandler
|
||||||
import com.mapbox.navigation.ui.maps.camera.state.NavigationCameraState
|
import com.mapbox.navigation.ui.maps.camera.state.NavigationCameraState
|
||||||
import com.mapbox.navigation.ui.maps.location.NavigationLocationProvider
|
import com.mapbox.navigation.ui.maps.location.NavigationLocationProvider
|
||||||
|
import eu.ztsh.garmin.Garmin
|
||||||
import eu.ztsh.garmin.MainActivity
|
import eu.ztsh.garmin.MainActivity
|
||||||
import eu.ztsh.garmin.UI
|
import eu.ztsh.garmin.UI
|
||||||
import eu.ztsh.garmin.mock.ReplayResources
|
import eu.ztsh.garmin.mock.ReplayResources
|
||||||
|
@ -105,6 +106,12 @@ class MapControl(
|
||||||
locationObserver = LocationObserver(this)
|
locationObserver = LocationObserver(this)
|
||||||
routeProgressObserver = routeControl.routeProgressObserver
|
routeProgressObserver = routeControl.routeProgressObserver
|
||||||
voiceInstructionsObserver = voiceControl.voiceInstructionsObserver
|
voiceInstructionsObserver = voiceControl.voiceInstructionsObserver
|
||||||
|
|
||||||
|
|
||||||
|
ui.reconnect.setOnClickListener {
|
||||||
|
Garmin.instance.reconnect()
|
||||||
|
ui.reconnect.showTextAndExtend(UI.BUTTON_ANIMATION_DURATION)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun routeToPoint(point: Point) {
|
fun routeToPoint(point: Point) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue