update
This commit is contained in:
parent
9ef2705543
commit
5d4d762b9b
@ -3,6 +3,7 @@
|
|||||||
package="com.rookiedev.hexapod">
|
package="com.rookiedev.hexapod">
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
|
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
|
||||||
|
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
|
@ -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"
|
||||||
|
@ -25,6 +25,7 @@ import com.google.android.material.textfield.TextInputLayout
|
|||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity() {
|
||||||
companion object {
|
companion object {
|
||||||
private const val BLUETOOTH_PERMISSION_CODE = 100
|
private const val BLUETOOTH_PERMISSION_CODE = 100
|
||||||
|
private const val BLUETOOTH_SCAN_CODE = 101
|
||||||
}
|
}
|
||||||
|
|
||||||
private val REQUEST_CONNECT_DEVICE_SECURE = 1
|
private val REQUEST_CONNECT_DEVICE_SECURE = 1
|
||||||
@ -94,6 +95,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
selectedDevice.setOnClickListener {
|
selectedDevice.setOnClickListener {
|
||||||
checkPermission("android.permission.BLUETOOTH_CONNECT", BLUETOOTH_PERMISSION_CODE)
|
checkPermission("android.permission.BLUETOOTH_CONNECT", BLUETOOTH_PERMISSION_CODE)
|
||||||
|
checkPermission("android.permission.BLUETOOTH_SCAN", BLUETOOTH_SCAN_CODE)
|
||||||
val serverIntent = Intent(this, DeviceListActivity::class.java)
|
val serverIntent = Intent(this, DeviceListActivity::class.java)
|
||||||
resultLauncher.launch(serverIntent)
|
resultLauncher.launch(serverIntent)
|
||||||
// startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE_INSECURE)
|
// startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE_INSECURE)
|
||||||
|
@ -17,12 +17,12 @@ import java.util.*
|
|||||||
* incoming connections, a thread for connecting with a device, and a
|
* incoming connections, a thread for connecting with a device, and a
|
||||||
* thread for performing data transmissions when connected.
|
* thread for performing data transmissions when connected.
|
||||||
*/
|
*/
|
||||||
class BluetoothService(context: Context?, handler: Handler) {
|
class BluetoothService(context: Context?) {
|
||||||
// Member fields
|
// Member fields
|
||||||
private var bluetoothManager: BluetoothManager? = null
|
private var bluetoothManager: BluetoothManager? = null
|
||||||
private var mContext: Context? = null
|
private var mContext: Context? = null
|
||||||
// private val mAdapter: BluetoothAdapter
|
// private val mAdapter: BluetoothAdapter
|
||||||
private val mHandler: Handler
|
// private val mHandler: Handler
|
||||||
private var mSecureAcceptThread: AcceptThread? = null
|
private var mSecureAcceptThread: AcceptThread? = null
|
||||||
private var mInsecureAcceptThread: AcceptThread? = null
|
private var mInsecureAcceptThread: AcceptThread? = null
|
||||||
private var mConnectThread: ConnectThread? = null
|
private var mConnectThread: ConnectThread? = null
|
||||||
@ -46,7 +46,7 @@ class BluetoothService(context: Context?, handler: Handler) {
|
|||||||
mNewState = mState
|
mNewState = mState
|
||||||
|
|
||||||
// Give the new state to the Handler so the UI Activity can update
|
// Give the new state to the Handler so the UI Activity can update
|
||||||
mHandler.obtainMessage(Constants.MESSAGE_STATE_CHANGE, mNewState, -1).sendToTarget()
|
// mHandler.obtainMessage(Constants.MESSAGE_STATE_CHANGE, mNewState, -1).sendToTarget()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -151,11 +151,11 @@ class BluetoothService(context: Context?, handler: Handler) {
|
|||||||
mConnectedThread!!.start()
|
mConnectedThread!!.start()
|
||||||
|
|
||||||
// Send the name of the connected device back to the UI Activity
|
// Send the name of the connected device back to the UI Activity
|
||||||
val msg = mHandler.obtainMessage(Constants.MESSAGE_DEVICE_NAME)
|
// val msg = mHandler.obtainMessage(Constants.MESSAGE_DEVICE_NAME)
|
||||||
val bundle = Bundle()
|
val bundle = Bundle()
|
||||||
bundle.putString(Constants.DEVICE_NAME, device.name)
|
bundle.putString(Constants.DEVICE_NAME, device.name)
|
||||||
msg.data = bundle
|
// msg.data = bundle
|
||||||
mHandler.sendMessage(msg)
|
// mHandler.sendMessage(msg)
|
||||||
// Update UI title
|
// Update UI title
|
||||||
updateUserInterfaceTitle()
|
updateUserInterfaceTitle()
|
||||||
}
|
}
|
||||||
@ -210,11 +210,11 @@ class BluetoothService(context: Context?, handler: Handler) {
|
|||||||
*/
|
*/
|
||||||
private fun connectionFailed() {
|
private fun connectionFailed() {
|
||||||
// Send a failure message back to the Activity
|
// Send a failure message back to the Activity
|
||||||
val msg = mHandler.obtainMessage(Constants.MESSAGE_TOAST)
|
// val msg = mHandler.obtainMessage(Constants.MESSAGE_TOAST)
|
||||||
val bundle = Bundle()
|
val bundle = Bundle()
|
||||||
bundle.putString(Constants.TOAST, "Unable to connect device")
|
bundle.putString(Constants.TOAST, "Unable to connect device")
|
||||||
msg.data = bundle
|
// msg.data = bundle
|
||||||
mHandler.sendMessage(msg)
|
// mHandler.sendMessage(msg)
|
||||||
mState = STATE_NONE
|
mState = STATE_NONE
|
||||||
// Update UI title
|
// Update UI title
|
||||||
updateUserInterfaceTitle()
|
updateUserInterfaceTitle()
|
||||||
@ -228,11 +228,11 @@ class BluetoothService(context: Context?, handler: Handler) {
|
|||||||
*/
|
*/
|
||||||
private fun connectionLost() {
|
private fun connectionLost() {
|
||||||
// Send a failure message back to the Activity
|
// Send a failure message back to the Activity
|
||||||
val msg = mHandler.obtainMessage(Constants.MESSAGE_TOAST)
|
// val msg = mHandler.obtainMessage(Constants.MESSAGE_TOAST)
|
||||||
val bundle = Bundle()
|
val bundle = Bundle()
|
||||||
bundle.putString(Constants.TOAST, "Device connection was lost")
|
bundle.putString(Constants.TOAST, "Device connection was lost")
|
||||||
msg.data = bundle
|
// msg.data = bundle
|
||||||
mHandler.sendMessage(msg)
|
// mHandler.sendMessage(msg)
|
||||||
mState = STATE_NONE
|
mState = STATE_NONE
|
||||||
// Update UI title
|
// Update UI title
|
||||||
updateUserInterfaceTitle()
|
updateUserInterfaceTitle()
|
||||||
@ -434,8 +434,8 @@ class BluetoothService(context: Context?, handler: Handler) {
|
|||||||
bytes = mmInStream!!.read(buffer)
|
bytes = mmInStream!!.read(buffer)
|
||||||
|
|
||||||
// Send the obtained bytes to the UI Activity
|
// Send the obtained bytes to the UI Activity
|
||||||
mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
|
// mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
|
||||||
.sendToTarget()
|
// .sendToTarget()
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
// Log.e(TAG, "disconnected", e)
|
// Log.e(TAG, "disconnected", e)
|
||||||
connectionLost()
|
connectionLost()
|
||||||
@ -454,8 +454,8 @@ class BluetoothService(context: Context?, handler: Handler) {
|
|||||||
mmOutStream!!.write(buffer)
|
mmOutStream!!.write(buffer)
|
||||||
|
|
||||||
// Share the sent message back to the UI Activity
|
// Share the sent message back to the UI Activity
|
||||||
mHandler.obtainMessage(Constants.MESSAGE_WRITE, -1, -1, buffer)
|
// mHandler.obtainMessage(Constants.MESSAGE_WRITE, -1, -1, buffer)
|
||||||
.sendToTarget()
|
// .sendToTarget()
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
// Log.e(TAG, "Exception during write", e)
|
// Log.e(TAG, "Exception during write", e)
|
||||||
}
|
}
|
||||||
@ -520,6 +520,6 @@ class BluetoothService(context: Context?, handler: Handler) {
|
|||||||
bluetoothManager!!.adapter
|
bluetoothManager!!.adapter
|
||||||
mState = STATE_NONE
|
mState = STATE_NONE
|
||||||
mNewState = mState
|
mNewState = mState
|
||||||
mHandler = handler
|
// mHandler = handler
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user