From c115d2ccceb0baef2cbe94e0bc6a6e85e198bf39 Mon Sep 17 00:00:00 2001 From: Zhengyu Peng Date: Mon, 21 Feb 2022 15:03:14 -0500 Subject: [PATCH] update --- .../rookiedev/hexapod/DeviceListActivity.kt | 7 -- .../com/rookiedev/hexapod/MainActivity.kt | 76 +++++++++---------- 2 files changed, 38 insertions(+), 45 deletions(-) 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 3d2fa67..154adc7 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 @@ -126,14 +126,8 @@ class DeviceListActivity : Activity() { private val mDeviceClickListener = OnItemClickListener { av, v, arg2, arg3 -> - val device: BluetoothDevice = av.getItemAtPosition(arg2) as BluetoothDevice - - - println(device.name) - println(device.address) - // Create the result Intent and include the MAC address val intent = Intent() intent.putExtra(EXTRA_DEVICE_ADDRESS, device.address) @@ -142,7 +136,6 @@ class DeviceListActivity : Activity() { // Set result and finish this Activity setResult(RESULT_OK, intent) - println("selected") finish() } 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 bf640d0..2d494c1 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 androidx.core.app.ActivityCompat import com.google.android.material.tabs.TabLayout import com.google.android.material.textfield.TextInputEditText import com.google.android.material.textfield.TextInputLayout +import org.w3c.dom.Text class MainActivity : AppCompatActivity() { @@ -41,11 +42,14 @@ class MainActivity : AppCompatActivity() { private val SHAREDPREFSIP = "IP" private val SHAREDPREFSPORT = "PORT" - private var mContext: Context?=null + private var mContext: Context? = null private lateinit var ipInput: TextInputEditText private lateinit var portInput: TextInputEditText + private lateinit var deviceName: TextView + private lateinit var deviceAddress: TextView + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) @@ -61,8 +65,8 @@ class MainActivity : AppCompatActivity() { val deviceList = findViewById(R.id.paired_devices) val selectedDevice = findViewById(R.id.selected) - val deviceName = findViewById(R.id.textView_device_name) - val deviceAddress = findViewById(R.id.textView_device_address) + deviceName = findViewById(R.id.textView_device_name) + deviceAddress = findViewById(R.id.textView_device_address) val sourceLink = findViewById(R.id.textView_github) sourceLink.movementMethod = LinkMovementMethod.getInstance() @@ -94,7 +98,7 @@ class MainActivity : AppCompatActivity() { } ) - selectedDevice.setOnClickListener{ + selectedDevice.setOnClickListener { checkPermission(Manifest.permission.BLUETOOTH_CONNECT, BLUETOOTH_PERMISSION_CODE) val serverIntent = Intent(this, DeviceListActivity::class.java) resultLauncher.launch(serverIntent) @@ -143,66 +147,62 @@ class MainActivity : AppCompatActivity() { }) } - var resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> - if (result.resultCode == Activity.RESULT_OK) { - // There are no request codes - val data: Intent? = result.data -// doSomeOperations() + var resultLauncher = + registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> + if (result.resultCode == Activity.RESULT_OK) { + // There are no request codes + val data: Intent? = result.data + + deviceName.text = data!!.getStringExtra("device_name") + deviceAddress.text = data.getStringExtra("device_address") + + } } - } // Function to check and request permission. private fun checkPermission(permission: String, requestCode: Int) { - if (ActivityCompat.checkSelfPermission(this@MainActivity, permission) == PackageManager.PERMISSION_DENIED) { + if (ActivityCompat.checkSelfPermission( + this@MainActivity, + permission + ) == PackageManager.PERMISSION_DENIED + ) { // Requesting the permission ActivityCompat.requestPermissions(this@MainActivity, arrayOf(permission), requestCode) } else { - Toast.makeText(this@MainActivity, "Permission already granted", Toast.LENGTH_SHORT).show() + Toast.makeText(this@MainActivity, "Permission already granted", Toast.LENGTH_SHORT) + .show() } } // This function is called when the user accepts or decline the permission. // Request Code is used to check which permission called this function. // This request code is provided when the user is prompt for permission. - override fun onRequestPermissionsResult(requestCode: Int, - permissions: Array, - grantResults: IntArray) { + override fun onRequestPermissionsResult( + requestCode: Int, + permissions: Array, + grantResults: IntArray + ) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) if (requestCode == BLUETOOTH_PERMISSION_CODE) { if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { - Toast.makeText(this@MainActivity, "Camera Permission Granted", Toast.LENGTH_SHORT).show() + Toast.makeText(this@MainActivity, "Camera Permission Granted", Toast.LENGTH_SHORT) + .show() } else { - Toast.makeText(this@MainActivity, "Camera Permission Denied", Toast.LENGTH_SHORT).show() + Toast.makeText(this@MainActivity, "Camera Permission Denied", Toast.LENGTH_SHORT) + .show() } } else if (requestCode == INTERNET_PERMISSION_CODE) { if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { - Toast.makeText(this@MainActivity, "Storage Permission Granted", Toast.LENGTH_SHORT).show() + Toast.makeText(this@MainActivity, "Storage Permission Granted", Toast.LENGTH_SHORT) + .show() } else { - Toast.makeText(this@MainActivity, "Storage Permission Denied", Toast.LENGTH_SHORT).show() + Toast.makeText(this@MainActivity, "Storage Permission Denied", Toast.LENGTH_SHORT) + .show() } } } - /** - * The on-click listener for all devices in the ListViews - */ - private val mDeviceClickListener = - OnItemClickListener { av, v, arg2, arg3 -> // Cancel discovery because it's costly and we're about to connect -// mBtAdapter.cancelDiscovery() -// -// // Get the device MAC address, which is the last 17 chars in the View -// val info = (v as TextView).text.toString() -// val address = info.substring(info.length - 17) -// -// // Create the result Intent and include the MAC address -// val intent = Intent() -// intent.putExtra(EXTRA_DEVICE_ADDRESS, address) -// -// // Set result and finish this Activity -// setResult(RESULT_OK, intent) -// finish() - } private fun readSharedPref() { val prefs = getSharedPreferences(