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

@ -1,13 +1,19 @@
package com.rookiedev.hexapod
import android.annotation.SuppressLint
import android.app.AlertDialog
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.*
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.rookiedev.hexapod.network.TCPClient
import com.rookiedev.hexapod.network.TCPClient.OnConnectEstablished
import com.rookiedev.hexapod.network.TCPClient.OnMessageReceived
import com.rookiedev.hexapod.network.TCPClient.OnDisconnected
import kotlinx.coroutines.*
import kotlin.math.PI
import kotlin.math.atan2
@ -196,9 +202,17 @@ class ControlActivity : AppCompatActivity() {
// udpClient.start()
println("connected")
}
}, object : OnDisconnected{
override fun onDisconnected() {
Handler(Looper.getMainLooper()).post {
alertDialog(0)
}
}
}
)
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?,
port: Int,
messagelistener: OnMessageReceived?,
onconnected: OnConnectEstablished?
onconnected: OnConnectEstablished?,
ondisconnect: OnDisconnected?
) :
Thread() {
private val controller: ControlActivity = c
@ -28,6 +29,7 @@ class TCPClient(
private var TCPMessage: String? = null
private var mMessageListener: OnMessageReceived? = null
private var onConnected: OnConnectEstablished? = null
private var onDisconnected: OnDisconnected?=null
private var isConnected = false
private var pause = false // if the thread is paused by system
private var isNewData = false
@ -61,6 +63,8 @@ class TCPClient(
} catch (e: Exception) {
// controller.cancelProgressDialog(java.lang.ModuleLayer.Controller.SERVERALERT)
println("unable to connect")
// controller.alertDialog(0)
onDisconnected!!.onDisconnected()
}
}
@ -102,10 +106,15 @@ class TCPClient(
fun onConnected()
}
interface OnDisconnected {
fun onDisconnected()
}
init {
SERVERPORT = port
mMessageListener = messagelistener
onConnected = onconnected
onDisconnected = ondisconnect
try {
SERVERIP = InetAddress.getByName(ip)
serverAddr = InetSocketAddress(SERVERIP, SERVERPORT)

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