From f66554b603f99bb1c9b8ba09cfea4aec6f4cf49d Mon Sep 17 00:00:00 2001 From: Zhengyu Peng Date: Mon, 21 Feb 2022 14:51:07 -0500 Subject: [PATCH] update --- .../rookiedev/hexapod/DeviceListActivity.kt | 184 ++++++++---------- .../app/src/main/res/layout/device_list.xml | 40 ++++ .../src/main/res/layout/selected_device.xml | 4 +- 3 files changed, 123 insertions(+), 105 deletions(-) create mode 100644 software/android/app/src/main/res/layout/device_list.xml 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 5f917c7..3d2fa67 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 @@ -2,49 +2,74 @@ package com.rookiedev.hexapod import android.annotation.SuppressLint import android.app.Activity -import android.bluetooth.BluetoothAdapter import android.bluetooth.BluetoothDevice import android.bluetooth.BluetoothManager -import android.content.BroadcastReceiver import android.content.Context import android.content.Intent -import android.content.IntentFilter import android.os.Bundle +import android.view.LayoutInflater import android.view.View -import android.view.Window +import android.view.ViewGroup import android.widget.AdapterView.OnItemClickListener import android.widget.ArrayAdapter -import android.widget.Button +import android.widget.ImageView import android.widget.ListView import android.widget.TextView -/** - * This Activity appears as a dialog. It lists any paired devices and - * devices detected in the area after discovery. When a device is chosen - * by the user, the MAC address of the device is sent back to the parent - * Activity in the result Intent. - */ +class BluetoothAdapter(mContext: Context?, private val devices: ArrayList) : + ArrayAdapter(mContext!!, 0, devices as List) { + @SuppressLint("MissingPermission") + override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { + // Get the data item for this position + var cView = convertView + val device: BluetoothDevice? = getItem(position) + // Check if an existing view is being reused, otherwise inflate the view + if (cView == null) { + cView = LayoutInflater.from(context).inflate(R.layout.device_list, parent, false) + } + // Lookup view for data population +// cView!!.isClickable = true + + val textDeviceName = cView!!.findViewById(R.id.list_device_name) + val textDeviceAddress = cView.findViewById(R.id.list_device_address) + val bluetoothIcon = cView.findViewById(R.id.bluetooth_icon) + + bluetoothIcon.setImageResource(R.drawable.ic_baseline_bluetooth_24) + + // Populate the data into the template view using the data object + textDeviceName.text = device!!.name + textDeviceAddress.text = device.address + // Return the completed view to render on screen + return cView + } + + override fun getCount(): Int { + return devices.size + } + + override fun getItem(arg0: Int): BluetoothDevice { + return devices[arg0] + } + + override fun getItemId(arg0: Int): Long { + return arg0.toLong() + } +} + + class DeviceListActivity : Activity() { /** * Member fields */ - private var mBtAdapter: BluetoothAdapter? = null - private var mContext: Context? = null private var bluetoothManager: BluetoothManager? = null - /** - * Newly discovered devices - */ - private var mNewDevicesArrayAdapter: ArrayAdapter? = null - @SuppressLint("MissingPermission") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Setup the window -// requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS) setContentView(R.layout.activity_device_list) mContext = applicationContext @@ -54,26 +79,11 @@ class DeviceListActivity : Activity() { // Initialize array adapters. One for already paired devices and // one for newly discovered devices - val pairedDevicesArrayAdapter = ArrayAdapter(this, R.layout.device_name) - mNewDevicesArrayAdapter = ArrayAdapter(this, R.layout.device_name) + val pairedDevicesArrayAdapter = ArrayAdapter(this, R.layout.device_list) +// val bluetoothAdapter = BluetoothAdapter(this, R.layout.device_list, ) - // Find and set up the ListView for paired devices - val pairedListView: ListView = findViewById(R.id.paired_devices) - pairedListView.adapter = pairedDevicesArrayAdapter - pairedListView.onItemClickListener = mDeviceClickListener - - // Find and set up the ListView for newly discovered devices -// val newDevicesListView: ListView = findViewById(R.id.new_devices) -// newDevicesListView.adapter = mNewDevicesArrayAdapter -// newDevicesListView.onItemClickListener = mDeviceClickListener - // Register for broadcasts when a device is discovered -// var filter = IntentFilter(BluetoothDevice.ACTION_FOUND) -// this.registerReceiver(mReceiver, filter) - // Register for broadcasts when discovery has finished -// filter = IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED) -// this.registerReceiver(mReceiver, filter) // Get the local Bluetooth adapter bluetoothManager = @@ -83,35 +93,30 @@ class DeviceListActivity : Activity() { // Get a set of currently paired devices val pairedDevices: Set = bluetoothManager!!.adapter.bondedDevices - // If there are paired devices, add each one to the ArrayAdapter - if (pairedDevices.isNotEmpty()) { - findViewById(R.id.title_paired_devices).visibility = View.VISIBLE - for (device in pairedDevices) { - pairedDevicesArrayAdapter.add( - """ - ${device.name} - ${device.address} - """.trimIndent() - ) - } - } else { - val noDevices = "No device" - pairedDevicesArrayAdapter.add(noDevices) - } - } - - @SuppressLint("MissingPermission") - override fun onDestroy() { - super.onDestroy() - - // Make sure we're not doing discovery anymore -// if (mBtAdapter != null) { -// mBtAdapter!!.cancelDiscovery() + val deviceList: ArrayList = ArrayList(pairedDevices) + + val bluetoothAdapter = BluetoothAdapter(this, deviceList) + +// // If there are paired devices, add each one to the ArrayAdapter +// if (pairedDevices.isNotEmpty()) { +// for (device in pairedDevices) { +// +// pairedDevicesArrayAdapter.add( +// """ +// ${device.name} +// ${device.address} +// """.trimIndent() +// ) +// } +// } else { +// val noDevices = "No device" +// pairedDevicesArrayAdapter.add(noDevices) // } -// bluetoothManager!!.adapter.cancelDiscovery() - // Unregister broadcast listeners -// unregisterReceiver(mReceiver) + // Find and set up the ListView for paired devices + val pairedListView: ListView = findViewById(R.id.paired_devices) + pairedListView.adapter = bluetoothAdapter + pairedListView.onItemClickListener = mDeviceClickListener } /** @@ -119,56 +124,28 @@ class DeviceListActivity : Activity() { */ @SuppressLint("MissingPermission") private val mDeviceClickListener = - OnItemClickListener { av, v, arg2, arg3 -> // Cancel discovery because it's costly and we're about to connect -// bluetoothManager!!.adapter.cancelDiscovery() + OnItemClickListener { av, v, arg2, arg3 -> - // 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) + + 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, address) + intent.putExtra(EXTRA_DEVICE_ADDRESS, device.address) + intent.putExtra(EXTRA_DEVICE_NAME, device.name) // Set result and finish this Activity setResult(RESULT_OK, intent) + + println("selected") finish() } - /** - * The BroadcastReceiver that listens for discovered devices and changes the title when - * discovery is finished - */ - private val mReceiver: BroadcastReceiver = object : BroadcastReceiver() { - @SuppressLint("MissingPermission") - override fun onReceive(context: Context?, intent: Intent) { - val action = intent.action - - // When discovery finds a device - if (BluetoothDevice.ACTION_FOUND == action) { - // Get the BluetoothDevice object from the Intent - val device = - intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) - // If it's already paired, skip it, because it's been listed already - if (device != null && device.bondState != BluetoothDevice.BOND_BONDED) { - mNewDevicesArrayAdapter!!.add( - """ - ${device.name} - ${device.address} - """.trimIndent() - ) - } - // When discovery is finished, change the Activity title -// } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED == action) { -// setProgressBarIndeterminateVisibility(false) -// setTitle(R.string.select_device) -// if (mNewDevicesArrayAdapter!!.count == 0) { -// val noDevices = resources.getText(R.string.none_found).toString() -// mNewDevicesArrayAdapter!!.add(noDevices) -// } - } - } - } companion object { /** @@ -180,5 +157,6 @@ class DeviceListActivity : Activity() { * Return Intent extra */ var EXTRA_DEVICE_ADDRESS = "device_address" + var EXTRA_DEVICE_NAME = "device_name" } } \ No newline at end of file diff --git a/software/android/app/src/main/res/layout/device_list.xml b/software/android/app/src/main/res/layout/device_list.xml new file mode 100644 index 0000000..afc6212 --- /dev/null +++ b/software/android/app/src/main/res/layout/device_list.xml @@ -0,0 +1,40 @@ + + + + + + + + + + \ No newline at end of file diff --git a/software/android/app/src/main/res/layout/selected_device.xml b/software/android/app/src/main/res/layout/selected_device.xml index 084f77e..7a302cb 100644 --- a/software/android/app/src/main/res/layout/selected_device.xml +++ b/software/android/app/src/main/res/layout/selected_device.xml @@ -23,7 +23,7 @@ android:id="@+id/textView_device_name" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:textAppearance="?attr/textAppearanceHeadline6" + android:textAppearance="?attr/textAppearanceBody1" android:text="TextView" app:layout_constraintBottom_toTopOf="@+id/textView_device_address" app:layout_constraintStart_toEndOf="@+id/imageView" @@ -34,7 +34,7 @@ android:id="@+id/textView_device_address" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:textAppearance="?attr/textAppearanceBody2" + android:textAppearance="?attr/textAppearanceCaption" android:text="TextView" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toEndOf="@+id/imageView"