master
Zhengyu Peng 3 years ago
parent 428a505740
commit 6d25b649ab

@ -1,13 +1,19 @@
package com.rookiedev.hexapod package com.rookiedev.hexapod
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.AlertDialog
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.* import android.view.*
import android.widget.ImageView import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import com.rookiedev.hexapod.network.TCPClient import com.rookiedev.hexapod.network.TCPClient
import com.rookiedev.hexapod.network.TCPClient.OnConnectEstablished import com.rookiedev.hexapod.network.TCPClient.OnConnectEstablished
import com.rookiedev.hexapod.network.TCPClient.OnMessageReceived import com.rookiedev.hexapod.network.TCPClient.OnMessageReceived
import com.rookiedev.hexapod.network.TCPClient.OnDisconnected
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlin.math.PI import kotlin.math.PI
import kotlin.math.atan2 import kotlin.math.atan2
@ -196,9 +202,17 @@ class ControlActivity : AppCompatActivity() {
// udpClient.start() // udpClient.start()
println("connected") println("connected")
} }
}, object : OnDisconnected{
override fun onDisconnected() {
Handler(Looper.getMainLooper()).post {
alertDialog(0)
}
}
} }
) )
this.tcpClient!!.start() this.tcpClient!!.start()
// alertDialog(0)
} }
@ -226,6 +240,23 @@ class ControlActivity : AppCompatActivity() {
} }
} }
} }
fun alertDialog(type: Int) {
val alert: AlertDialog = AlertDialog.Builder(this).create()
when (type) {
0 -> {
alert.setTitle("Failed to connect")
alert.setMessage("Failed to connect to the Hexapod"
)
alert.setOnCancelListener(DialogInterface.OnCancelListener { finish() })
alert.setButton(AlertDialog.BUTTON_POSITIVE,
"OK",
DialogInterface.OnClickListener { dialog, which -> finish() })
}
}
alert.show()
}
} }

@ -15,7 +15,8 @@ class TCPClient(
ip: String?, ip: String?,
port: Int, port: Int,
messagelistener: OnMessageReceived?, messagelistener: OnMessageReceived?,
onconnected: OnConnectEstablished? onconnected: OnConnectEstablished?,
ondisconnect: OnDisconnected?
) : ) :
Thread() { Thread() {
private val controller: ControlActivity = c private val controller: ControlActivity = c
@ -28,6 +29,7 @@ class TCPClient(
private var TCPMessage: String? = null private var TCPMessage: String? = null
private var mMessageListener: OnMessageReceived? = null private var mMessageListener: OnMessageReceived? = null
private var onConnected: OnConnectEstablished? = null private var onConnected: OnConnectEstablished? = null
private var onDisconnected: OnDisconnected?=null
private var isConnected = false private var isConnected = false
private var pause = false // if the thread is paused by system private var pause = false // if the thread is paused by system
private var isNewData = false private var isNewData = false
@ -61,6 +63,8 @@ class TCPClient(
} catch (e: Exception) { } catch (e: Exception) {
// controller.cancelProgressDialog(java.lang.ModuleLayer.Controller.SERVERALERT) // controller.cancelProgressDialog(java.lang.ModuleLayer.Controller.SERVERALERT)
println("unable to connect") println("unable to connect")
// controller.alertDialog(0)
onDisconnected!!.onDisconnected()
} }
} }
@ -102,10 +106,15 @@ class TCPClient(
fun onConnected() fun onConnected()
} }
interface OnDisconnected {
fun onDisconnected()
}
init { init {
SERVERPORT = port SERVERPORT = port
mMessageListener = messagelistener mMessageListener = messagelistener
onConnected = onconnected onConnected = onconnected
onDisconnected = ondisconnect
try { try {
SERVERIP = InetAddress.getByName(ip) SERVERIP = InetAddress.getByName(ip)
serverAddr = InetSocketAddress(SERVERIP, SERVERPORT) serverAddr = InetSocketAddress(SERVERIP, SERVERPORT)

@ -117,4 +117,5 @@
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_control_circle" /> app:srcCompat="@drawable/ic_control_circle" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout> </LinearLayout>
Loading…
Cancel
Save