diff --git a/software/android/app/src/main/java/com/rookiedev/hexapod/ControlActivity.kt b/software/android/app/src/main/java/com/rookiedev/hexapod/ControlActivity.kt index 2f7bb7a..0c140b4 100644 --- a/software/android/app/src/main/java/com/rookiedev/hexapod/ControlActivity.kt +++ b/software/android/app/src/main/java/com/rookiedev/hexapod/ControlActivity.kt @@ -2,6 +2,9 @@ package com.rookiedev.hexapod import android.annotation.SuppressLint import android.app.AlertDialog +import android.bluetooth.BluetoothAdapter +import android.bluetooth.BluetoothManager +import android.content.Context import android.content.DialogInterface import android.os.Bundle import android.os.Handler @@ -11,6 +14,7 @@ import android.widget.Button import android.widget.ImageView import androidx.appcompat.app.AppCompatActivity import androidx.constraintlayout.widget.ConstraintLayout +import com.rookiedev.hexapod.network.BluetoothService import com.rookiedev.hexapod.network.TCPClient import com.rookiedev.hexapod.network.TCPClient.* import kotlinx.coroutines.* @@ -72,6 +76,16 @@ class ControlActivity : AppCompatActivity() { private var height = 0 private var radius = 0f + private var connectInterface:String = "" + + private var mContext: Context? = 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 tcpClient: TCPClient? = null private var ip: String = "" private var port = 0 @@ -97,8 +111,19 @@ class ControlActivity : AppCompatActivity() { val myIntent = intent // gets the previously created intent - ip = myIntent.getStringExtra("ip").toString() - port = myIntent.getStringExtra("port").toString().toInt() + mContext = applicationContext + bluetoothManager = + mContext!!.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager + bluetoothManager + deviceAdapter = 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"){ + mac = myIntent.getStringExtra("mac").toString() + } controlWindowInsets(true) @@ -341,31 +366,35 @@ class ControlActivity : AppCompatActivity() { super.onResume() progressBar.visibility = View.VISIBLE - this.tcpClient = TCPClient(ip, port, object : OnMessageReceived { - override fun messageReceived(message: String?) { - if (message == null) { + if (connectInterface=="WiFi") { + this.tcpClient = TCPClient(ip, port, object : OnMessageReceived { + override fun messageReceived(message: String?) { + if (message == null) { // alertDialog(DISCONNECTED) - println("no message") + println("no message") + } } - } - }, object : OnConnectEstablished { - override fun onConnected() { + }, object : OnConnectEstablished { + override fun onConnected() { // udpClient.start() - println("connected") - Handler(Looper.getMainLooper()).post { - progressBar.visibility = View.GONE + println("connected") + Handler(Looper.getMainLooper()).post { + progressBar.visibility = View.GONE + } } - } - }, object : OnDisconnected { - override fun onDisconnected() { - Handler(Looper.getMainLooper()).post { - progressBar.visibility = View.GONE - alertDialog(0) + }, object : OnDisconnected { + override fun onDisconnected() { + Handler(Looper.getMainLooper()).post { + progressBar.visibility = View.GONE + alertDialog(0) + } } } + ) + this.tcpClient!!.start() + }else if(connectInterface=="Bluetooth"){ + println("Bluetooth") } - ) - this.tcpClient!!.start() currentState = "standby" controlImage!!.setImageResource(R.drawable.ic_control_circle_standby) diff --git a/software/android/app/src/main/java/com/rookiedev/hexapod/DeviceListActivity.kt b/software/android/app/src/main/java/com/rookiedev/hexapod/DeviceListActivity.kt index c9f7a07..ec1284c 100644 --- a/software/android/app/src/main/java/com/rookiedev/hexapod/DeviceListActivity.kt +++ b/software/android/app/src/main/java/com/rookiedev/hexapod/DeviceListActivity.kt @@ -17,7 +17,7 @@ import android.widget.ListView import android.widget.TextView -class BluetoothAdapter(mContext: Context?, private val devices: ArrayList) : +class DeviceAdapter(mContext: Context?, private val devices: ArrayList) : ArrayAdapter(mContext!!, 0, devices as List) { @SuppressLint("MissingPermission") override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { @@ -86,7 +86,7 @@ class DeviceListActivity : Activity() { val deviceList: ArrayList = ArrayList(pairedDevices) - val bluetoothAdapter = BluetoothAdapter(this, deviceList) + val bluetoothAdapter = DeviceAdapter(this, deviceList) // Find and set up the ListView for paired devices val pairedListView: ListView = findViewById(R.id.paired_devices) 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 370c598..9d48af0 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 @@ -1,10 +1,7 @@ package com.rookiedev.hexapod.network import android.annotation.SuppressLint -import android.bluetooth.BluetoothAdapter -import android.bluetooth.BluetoothDevice -import android.bluetooth.BluetoothServerSocket -import android.bluetooth.BluetoothSocket +import android.bluetooth.* import android.content.Context import android.os.Bundle import android.os.Handler @@ -22,7 +19,9 @@ import java.util.* */ class BluetoothService(context: Context?, handler: Handler) { // Member fields - private val mAdapter: BluetoothAdapter + private var bluetoothManager: BluetoothManager? = null + private var mContext: Context? = null +// private val mAdapter: BluetoothAdapter private val mHandler: Handler private var mSecureAcceptThread: AcceptThread? = null private var mInsecureAcceptThread: AcceptThread? = null @@ -316,12 +315,12 @@ class BluetoothService(context: Context?, handler: Handler) { // Create a new listening server socket try { tmp = if (secure) { - mAdapter.listenUsingRfcommWithServiceRecord( + bluetoothManager!!.adapter.listenUsingRfcommWithServiceRecord( NAME_SECURE, MY_UUID_SECURE ) } else { - mAdapter.listenUsingInsecureRfcommWithServiceRecord( + bluetoothManager!!.adapter.listenUsingInsecureRfcommWithServiceRecord( NAME_INSECURE, MY_UUID_INSECURE ) } @@ -351,7 +350,7 @@ class BluetoothService(context: Context?, handler: Handler) { name = "ConnectThread$mSocketType" // Always cancel discovery because it will slow down a connection - mAdapter.cancelDiscovery() + bluetoothManager!!.adapter.cancelDiscovery() // Make a connection to the BluetoothSocket try { @@ -420,7 +419,7 @@ class BluetoothService(context: Context?, handler: Handler) { */ private inner class ConnectedThread(socket: BluetoothSocket?, socketType: String) : Thread() { - private val mmSocket: BluetoothSocket? + private val mmSocket: BluetoothSocket? = socket private val mmInStream: InputStream? private val mmOutStream: OutputStream? override fun run() { @@ -472,7 +471,6 @@ class BluetoothService(context: Context?, handler: Handler) { init { // Log.d(TAG, "create ConnectedThread: $socketType") - mmSocket = socket var tmpIn: InputStream? = null var tmpOut: OutputStream? = null @@ -515,7 +513,11 @@ class BluetoothService(context: Context?, handler: Handler) { * @param handler A Handler to send messages back to the UI Activity */ init { - mAdapter = BluetoothAdapter.getDefaultAdapter() + mContext = context +// mAdapter = BluetoothAdapter.getDefaultAdapter() + bluetoothManager = + mContext!!.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager + bluetoothManager!!.adapter mState = STATE_NONE mNewState = mState mHandler = handler