From 6f830220137d1d21eb26f6e001ec75a7b562f74e Mon Sep 17 00:00:00 2001 From: Zhengyu Peng Date: Tue, 22 Feb 2022 16:50:41 -0500 Subject: [PATCH] update --- .../android/app/src/main/AndroidManifest.xml | 1 + .../com/rookiedev/hexapod/ControlActivity.kt | 101 ++++++++++++++++-- .../com/rookiedev/hexapod/MainActivity.kt | 2 + .../hexapod/network/BluetoothService.kt | 34 +++--- 4 files changed, 112 insertions(+), 26 deletions(-) diff --git a/software/android/app/src/main/AndroidManifest.xml b/software/android/app/src/main/AndroidManifest.xml index 3517895..f1367b6 100644 --- a/software/android/app/src/main/AndroidManifest.xml +++ b/software/android/app/src/main/AndroidManifest.xml @@ -3,6 +3,7 @@ package="com.rookiedev.hexapod"> + ?>( + 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?>( + 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" diff --git a/software/android/app/src/main/java/com/rookiedev/hexapod/MainActivity.kt b/software/android/app/src/main/java/com/rookiedev/hexapod/MainActivity.kt index 1cc70d7..94412f1 100644 --- a/software/android/app/src/main/java/com/rookiedev/hexapod/MainActivity.kt +++ b/software/android/app/src/main/java/com/rookiedev/hexapod/MainActivity.kt @@ -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) diff --git a/software/android/app/src/main/java/com/rookiedev/hexapod/network/BluetoothService.kt b/software/android/app/src/main/java/com/rookiedev/hexapod/network/BluetoothService.kt index 9d48af0..30792a0 100644 --- a/software/android/app/src/main/java/com/rookiedev/hexapod/network/BluetoothService.kt +++ b/software/android/app/src/main/java/com/rookiedev/hexapod/network/BluetoothService.kt @@ -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 } } \ No newline at end of file