|
|
@ -4,6 +4,7 @@ import android.annotation.SuppressLint
|
|
|
|
import android.app.AlertDialog
|
|
|
|
import android.app.AlertDialog
|
|
|
|
import android.bluetooth.BluetoothAdapter
|
|
|
|
import android.bluetooth.BluetoothAdapter
|
|
|
|
import android.bluetooth.BluetoothManager
|
|
|
|
import android.bluetooth.BluetoothManager
|
|
|
|
|
|
|
|
import android.bluetooth.BluetoothSocket
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.DialogInterface
|
|
|
|
import android.content.DialogInterface
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.os.Bundle
|
|
|
@ -18,6 +19,10 @@ import com.rookiedev.hexapod.network.BluetoothService
|
|
|
|
import com.rookiedev.hexapod.network.TCPClient
|
|
|
|
import com.rookiedev.hexapod.network.TCPClient
|
|
|
|
import com.rookiedev.hexapod.network.TCPClient.*
|
|
|
|
import com.rookiedev.hexapod.network.TCPClient.*
|
|
|
|
import kotlinx.coroutines.*
|
|
|
|
import kotlinx.coroutines.*
|
|
|
|
|
|
|
|
import java.io.*
|
|
|
|
|
|
|
|
import java.lang.reflect.InvocationTargetException
|
|
|
|
|
|
|
|
import java.lang.reflect.Method
|
|
|
|
|
|
|
|
import java.util.*
|
|
|
|
import kotlin.math.PI
|
|
|
|
import kotlin.math.PI
|
|
|
|
import kotlin.math.atan2
|
|
|
|
import kotlin.math.atan2
|
|
|
|
import kotlin.math.pow
|
|
|
|
import kotlin.math.pow
|
|
|
@ -76,15 +81,17 @@ class ControlActivity : AppCompatActivity() {
|
|
|
|
private var height = 0
|
|
|
|
private var height = 0
|
|
|
|
private var radius = 0f
|
|
|
|
private var radius = 0f
|
|
|
|
|
|
|
|
|
|
|
|
private var connectInterface:String = ""
|
|
|
|
private var connectInterface: String = ""
|
|
|
|
|
|
|
|
|
|
|
|
private var mContext: Context? = null
|
|
|
|
private var mContext: Context? = null
|
|
|
|
|
|
|
|
|
|
|
|
private var mac:String = ""
|
|
|
|
private var mac: String = ""
|
|
|
|
// private val mBluetoothAdapter: BluetoothAdapter? = null
|
|
|
|
|
|
|
|
|
|
|
|
// private val mBluetoothAdapter: BluetoothAdapter? = null
|
|
|
|
private var bluetoothManager: BluetoothManager? = null
|
|
|
|
private var bluetoothManager: BluetoothManager? = null
|
|
|
|
private var deviceAdapter: BluetoothAdapter? = null
|
|
|
|
private var bluetoothAdapter: BluetoothAdapter? = null
|
|
|
|
private val mChatService: BluetoothService? = null
|
|
|
|
private var mChatService: BluetoothService? = null
|
|
|
|
|
|
|
|
private var mOutStringBuffer: StringBuffer? = null
|
|
|
|
|
|
|
|
|
|
|
|
private var tcpClient: TCPClient? = null
|
|
|
|
private var tcpClient: TCPClient? = null
|
|
|
|
private var ip: String = ""
|
|
|
|
private var ip: String = ""
|
|
|
@ -115,13 +122,13 @@ class ControlActivity : AppCompatActivity() {
|
|
|
|
bluetoothManager =
|
|
|
|
bluetoothManager =
|
|
|
|
mContext!!.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
|
|
|
|
mContext!!.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
|
|
|
|
bluetoothManager
|
|
|
|
bluetoothManager
|
|
|
|
deviceAdapter = bluetoothManager!!.adapter
|
|
|
|
bluetoothAdapter = bluetoothManager!!.adapter
|
|
|
|
|
|
|
|
|
|
|
|
connectInterface = myIntent.getStringExtra("interface").toString()
|
|
|
|
connectInterface = myIntent.getStringExtra("interface").toString()
|
|
|
|
if (connectInterface == "WiFi") {
|
|
|
|
if (connectInterface == "WiFi") {
|
|
|
|
ip = myIntent.getStringExtra("ip").toString()
|
|
|
|
ip = myIntent.getStringExtra("ip").toString()
|
|
|
|
port = myIntent.getStringExtra("port").toString().toInt()
|
|
|
|
port = myIntent.getStringExtra("port").toString().toInt()
|
|
|
|
}else if (connectInterface == "Bluetooth"){
|
|
|
|
} else if (connectInterface == "Bluetooth") {
|
|
|
|
mac = myIntent.getStringExtra("mac").toString()
|
|
|
|
mac = myIntent.getStringExtra("mac").toString()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -362,11 +369,12 @@ class ControlActivity : AppCompatActivity() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressLint("MissingPermission")
|
|
|
|
override fun onResume() {
|
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
|
|
|
super.onResume()
|
|
|
|
progressBar.visibility = View.VISIBLE
|
|
|
|
progressBar.visibility = View.VISIBLE
|
|
|
|
|
|
|
|
|
|
|
|
if (connectInterface=="WiFi") {
|
|
|
|
if (connectInterface == "WiFi") {
|
|
|
|
this.tcpClient = TCPClient(ip, port, object : OnMessageReceived {
|
|
|
|
this.tcpClient = TCPClient(ip, port, object : OnMessageReceived {
|
|
|
|
override fun messageReceived(message: String?) {
|
|
|
|
override fun messageReceived(message: String?) {
|
|
|
|
if (message == null) {
|
|
|
|
if (message == null) {
|
|
|
@ -392,8 +400,83 @@ class ControlActivity : AppCompatActivity() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
this.tcpClient!!.start()
|
|
|
|
this.tcpClient!!.start()
|
|
|
|
}else if(connectInterface=="Bluetooth"){
|
|
|
|
} else if (connectInterface == "Bluetooth") {
|
|
|
|
println("Bluetooth")
|
|
|
|
println("Bluetooth")
|
|
|
|
|
|
|
|
if (bluetoothAdapter!!.isEnabled) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val SERIAL_UUID: UUID =
|
|
|
|
|
|
|
|
UUID.fromString("00001101-0000-1000-8000-00805F9B34FB") //UUID for serial connection
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var device =
|
|
|
|
|
|
|
|
bluetoothAdapter!!.getRemoteDevice(mac) //get remote device by mac, we assume these two devices are already paired
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // Get a BluetoothSocket to connect with the given BluetoothDevice
|
|
|
|
|
|
|
|
var socket: BluetoothSocket? = null
|
|
|
|
|
|
|
|
var out: PrintWriter? = null
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
|
|
|
// socket = device.createRfcommSocketToServiceRecord(SERIAL_UUID)
|
|
|
|
|
|
|
|
// } catch (e: IOException) {
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val secure = false
|
|
|
|
|
|
|
|
val port = 10
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
if (secure) {
|
|
|
|
|
|
|
|
if (port == 0) {
|
|
|
|
|
|
|
|
socket = device.createRfcommSocketToServiceRecord(SERIAL_UUID)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
val createRfcommSocket: Method = device.javaClass.getMethod(
|
|
|
|
|
|
|
|
"createRfcommSocket", *arrayOf<Class<*>?>(
|
|
|
|
|
|
|
|
Int::class.javaPrimitiveType
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
socket = createRfcommSocket.invoke(device, port) as BluetoothSocket
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (port == 0) {
|
|
|
|
|
|
|
|
socket = device.createInsecureRfcommSocketToServiceRecord(
|
|
|
|
|
|
|
|
SERIAL_UUID
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
val createInsecureRfcommSocket: Method =
|
|
|
|
|
|
|
|
device.javaClass.getMethod(
|
|
|
|
|
|
|
|
"createInsecureRfcommSocket", *arrayOf<Class<*>?>(
|
|
|
|
|
|
|
|
Int::class.javaPrimitiveType
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
socket = createInsecureRfcommSocket.invoke(
|
|
|
|
|
|
|
|
device,
|
|
|
|
|
|
|
|
port
|
|
|
|
|
|
|
|
) as BluetoothSocket
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (e: IOException) {
|
|
|
|
|
|
|
|
println(e)
|
|
|
|
|
|
|
|
// Log.e(TAG, "Socket Type: " + mSocketType.toString() + "create() failed", e)
|
|
|
|
|
|
|
|
} catch (e: NoSuchMethodException) {
|
|
|
|
|
|
|
|
println(e)
|
|
|
|
|
|
|
|
// Log.e(TAG, "Socket Type: " + mSocketType.toString() + "create() failed", e)
|
|
|
|
|
|
|
|
} catch (e: InvocationTargetException) {
|
|
|
|
|
|
|
|
println(e)
|
|
|
|
|
|
|
|
// Log.e(TAG, "Socket Type: " + mSocketType.toString() + "create() failed", e)
|
|
|
|
|
|
|
|
} catch (e: IllegalAccessException) {
|
|
|
|
|
|
|
|
println(e)
|
|
|
|
|
|
|
|
// Log.e(TAG, "Socket Type: " + mSocketType.toString() + "create() failed", e)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
println("attempt to connect")
|
|
|
|
|
|
|
|
socket!!.connect()
|
|
|
|
|
|
|
|
out = PrintWriter(
|
|
|
|
|
|
|
|
BufferedWriter(OutputStreamWriter(socket.outputStream)),
|
|
|
|
|
|
|
|
true
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
out.println("testest")
|
|
|
|
|
|
|
|
//now you can use out to send output via out.write
|
|
|
|
|
|
|
|
} catch (e: IOException) {
|
|
|
|
|
|
|
|
println(e)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
currentState = "standby"
|
|
|
|
currentState = "standby"
|
|
|
|