master
Zhengyu Peng 2 years ago
parent b786d55c43
commit 6f83022013

@ -3,6 +3,7 @@
package="com.rookiedev.hexapod">
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.INTERNET" />
<application

@ -4,6 +4,7 @@ import android.annotation.SuppressLint
import android.app.AlertDialog
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager
import android.bluetooth.BluetoothSocket
import android.content.Context
import android.content.DialogInterface
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 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.atan2
import kotlin.math.pow
@ -76,15 +81,17 @@ class ControlActivity : AppCompatActivity() {
private var height = 0
private var radius = 0f
private var connectInterface:String = ""
private var connectInterface: String = ""
private var mContext: Context? = null
private var mac:String = ""
// private val mBluetoothAdapter: BluetoothAdapter? = null
private var mac: String = ""
// private val mBluetoothAdapter: BluetoothAdapter? = null
private var bluetoothManager: BluetoothManager? = null
private var deviceAdapter: BluetoothAdapter? = null
private val mChatService: BluetoothService? = null
private var bluetoothAdapter: BluetoothAdapter? = null
private var mChatService: BluetoothService? = null
private var mOutStringBuffer: StringBuffer? = null
private var tcpClient: TCPClient? = null
private var ip: String = ""
@ -115,13 +122,13 @@ class ControlActivity : AppCompatActivity() {
bluetoothManager =
mContext!!.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
bluetoothManager
deviceAdapter = bluetoothManager!!.adapter
bluetoothAdapter = bluetoothManager!!.adapter
connectInterface = myIntent.getStringExtra("interface").toString()
if (connectInterface == "WiFi") {
ip = myIntent.getStringExtra("ip").toString()
port = myIntent.getStringExtra("port").toString().toInt()
}else if (connectInterface == "Bluetooth"){
} else if (connectInterface == "Bluetooth") {
mac = myIntent.getStringExtra("mac").toString()
}
@ -362,11 +369,12 @@ class ControlActivity : AppCompatActivity() {
}
}
@SuppressLint("MissingPermission")
override fun onResume() {
super.onResume()
progressBar.visibility = View.VISIBLE
if (connectInterface=="WiFi") {
if (connectInterface == "WiFi") {
this.tcpClient = TCPClient(ip, port, object : OnMessageReceived {
override fun messageReceived(message: String?) {
if (message == null) {
@ -392,8 +400,83 @@ class ControlActivity : AppCompatActivity() {
}
)
this.tcpClient!!.start()
}else if(connectInterface=="Bluetooth"){
} else if (connectInterface == "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"

@ -25,6 +25,7 @@ import com.google.android.material.textfield.TextInputLayout
class MainActivity : AppCompatActivity() {
companion object {
private const val BLUETOOTH_PERMISSION_CODE = 100
private const val BLUETOOTH_SCAN_CODE = 101
}
private val REQUEST_CONNECT_DEVICE_SECURE = 1
@ -94,6 +95,7 @@ class MainActivity : AppCompatActivity() {
selectedDevice.setOnClickListener {
checkPermission("android.permission.BLUETOOTH_CONNECT", BLUETOOTH_PERMISSION_CODE)
checkPermission("android.permission.BLUETOOTH_SCAN", BLUETOOTH_SCAN_CODE)
val serverIntent = Intent(this, DeviceListActivity::class.java)
resultLauncher.launch(serverIntent)
// startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE_INSECURE)

@ -17,12 +17,12 @@ import java.util.*
* incoming connections, a thread for connecting with a device, and a
* thread for performing data transmissions when connected.
*/
class BluetoothService(context: Context?, handler: Handler) {
class BluetoothService(context: Context?) {
// Member fields
private var bluetoothManager: BluetoothManager? = null
private var mContext: Context? = null
// private val mAdapter: BluetoothAdapter
private val mHandler: Handler
// private val mHandler: Handler
private var mSecureAcceptThread: AcceptThread? = null
private var mInsecureAcceptThread: AcceptThread? = null
private var mConnectThread: ConnectThread? = null
@ -46,7 +46,7 @@ class BluetoothService(context: Context?, handler: Handler) {
mNewState = mState
// 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()
// 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()
bundle.putString(Constants.DEVICE_NAME, device.name)
msg.data = bundle
mHandler.sendMessage(msg)
// msg.data = bundle
// mHandler.sendMessage(msg)
// Update UI title
updateUserInterfaceTitle()
}
@ -210,11 +210,11 @@ class BluetoothService(context: Context?, handler: Handler) {
*/
private fun connectionFailed() {
// 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()
bundle.putString(Constants.TOAST, "Unable to connect device")
msg.data = bundle
mHandler.sendMessage(msg)
// msg.data = bundle
// mHandler.sendMessage(msg)
mState = STATE_NONE
// Update UI title
updateUserInterfaceTitle()
@ -228,11 +228,11 @@ class BluetoothService(context: Context?, handler: Handler) {
*/
private fun connectionLost() {
// 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()
bundle.putString(Constants.TOAST, "Device connection was lost")
msg.data = bundle
mHandler.sendMessage(msg)
// msg.data = bundle
// mHandler.sendMessage(msg)
mState = STATE_NONE
// Update UI title
updateUserInterfaceTitle()
@ -434,8 +434,8 @@ class BluetoothService(context: Context?, handler: Handler) {
bytes = mmInStream!!.read(buffer)
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget()
// mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
// .sendToTarget()
} catch (e: IOException) {
// Log.e(TAG, "disconnected", e)
connectionLost()
@ -454,8 +454,8 @@ class BluetoothService(context: Context?, handler: Handler) {
mmOutStream!!.write(buffer)
// Share the sent message back to the UI Activity
mHandler.obtainMessage(Constants.MESSAGE_WRITE, -1, -1, buffer)
.sendToTarget()
// mHandler.obtainMessage(Constants.MESSAGE_WRITE, -1, -1, buffer)
// .sendToTarget()
} catch (e: IOException) {
// Log.e(TAG, "Exception during write", e)
}
@ -520,6 +520,6 @@ class BluetoothService(context: Context?, handler: Handler) {
bluetoothManager!!.adapter
mState = STATE_NONE
mNewState = mState
mHandler = handler
// mHandler = handler
}
}
Loading…
Cancel
Save