master
Zhengyu Peng 2 years ago
parent eda367239a
commit f66554b603

@ -2,49 +2,74 @@ package com.rookiedev.hexapod
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.Activity import android.app.Activity
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothManager import android.bluetooth.BluetoothManager
import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.Window import android.view.ViewGroup
import android.widget.AdapterView.OnItemClickListener import android.widget.AdapterView.OnItemClickListener
import android.widget.ArrayAdapter import android.widget.ArrayAdapter
import android.widget.Button import android.widget.ImageView
import android.widget.ListView import android.widget.ListView
import android.widget.TextView import android.widget.TextView
/** class BluetoothAdapter(mContext: Context?, private val devices: ArrayList<BluetoothDevice>) :
* This Activity appears as a dialog. It lists any paired devices and ArrayAdapter<BluetoothDevice?>(mContext!!, 0, devices as List<BluetoothDevice?>) {
* devices detected in the area after discovery. When a device is chosen @SuppressLint("MissingPermission")
* by the user, the MAC address of the device is sent back to the parent override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
* Activity in the result Intent. // 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<TextView>(R.id.list_device_name)
val textDeviceAddress = cView.findViewById<TextView>(R.id.list_device_address)
val bluetoothIcon = cView.findViewById<ImageView>(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() { class DeviceListActivity : Activity() {
/** /**
* Member fields * Member fields
*/ */
private var mBtAdapter: BluetoothAdapter? = null
private var mContext: Context? = null private var mContext: Context? = null
private var bluetoothManager: BluetoothManager? = null private var bluetoothManager: BluetoothManager? = null
/**
* Newly discovered devices
*/
private var mNewDevicesArrayAdapter: ArrayAdapter<String>? = null
@SuppressLint("MissingPermission") @SuppressLint("MissingPermission")
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
// Setup the window // Setup the window
// requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS)
setContentView(R.layout.activity_device_list) setContentView(R.layout.activity_device_list)
mContext = applicationContext mContext = applicationContext
@ -54,26 +79,11 @@ class DeviceListActivity : Activity() {
// Initialize array adapters. One for already paired devices and // Initialize array adapters. One for already paired devices and
// one for newly discovered devices // one for newly discovered devices
val pairedDevicesArrayAdapter = ArrayAdapter<String>(this, R.layout.device_name) val pairedDevicesArrayAdapter = ArrayAdapter<String>(this, R.layout.device_list)
mNewDevicesArrayAdapter = ArrayAdapter(this, R.layout.device_name) // val bluetoothAdapter = BluetoothAdapter(this, R.layout.device_list, )
// Find and set up the ListView for paired devices
val pairedListView: ListView = findViewById<ListView>(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 // Get the local Bluetooth adapter
bluetoothManager = bluetoothManager =
@ -83,35 +93,30 @@ class DeviceListActivity : Activity() {
// Get a set of currently paired devices // Get a set of currently paired devices
val pairedDevices: Set<BluetoothDevice> = bluetoothManager!!.adapter.bondedDevices val pairedDevices: Set<BluetoothDevice> = bluetoothManager!!.adapter.bondedDevices
// If there are paired devices, add each one to the ArrayAdapter val deviceList: ArrayList<BluetoothDevice> = ArrayList(pairedDevices)
if (pairedDevices.isNotEmpty()) {
findViewById<View>(R.id.title_paired_devices).visibility = View.VISIBLE val bluetoothAdapter = BluetoothAdapter(this, deviceList)
for (device in pairedDevices) {
pairedDevicesArrayAdapter.add( // // If there are paired devices, add each one to the ArrayAdapter
""" // if (pairedDevices.isNotEmpty()) {
${device.name} // for (device in pairedDevices) {
${device.address} //
""".trimIndent() // pairedDevicesArrayAdapter.add(
) // """
} // ${device.name}
} else { // ${device.address}
val noDevices = "No device" // """.trimIndent()
pairedDevicesArrayAdapter.add(noDevices) // )
} // }
} // } else {
// val noDevices = "No device"
@SuppressLint("MissingPermission") // pairedDevicesArrayAdapter.add(noDevices)
override fun onDestroy() {
super.onDestroy()
// Make sure we're not doing discovery anymore
// if (mBtAdapter != null) {
// mBtAdapter!!.cancelDiscovery()
// } // }
// bluetoothManager!!.adapter.cancelDiscovery()
// Unregister broadcast listeners // Find and set up the ListView for paired devices
// unregisterReceiver(mReceiver) val pairedListView: ListView = findViewById<ListView>(R.id.paired_devices)
pairedListView.adapter = bluetoothAdapter
pairedListView.onItemClickListener = mDeviceClickListener
} }
/** /**
@ -119,56 +124,28 @@ class DeviceListActivity : Activity() {
*/ */
@SuppressLint("MissingPermission") @SuppressLint("MissingPermission")
private val mDeviceClickListener = private val mDeviceClickListener =
OnItemClickListener { av, v, arg2, arg3 -> // Cancel discovery because it's costly and we're about to connect OnItemClickListener { av, v, arg2, arg3 ->
// bluetoothManager!!.adapter.cancelDiscovery()
// Get the device MAC address, which is the last 17 chars in the View
val info = (v as TextView).text.toString() val device: BluetoothDevice = av.getItemAtPosition(arg2) as BluetoothDevice
val address = info.substring(info.length - 17)
println(device.name)
println(device.address)
// Create the result Intent and include the MAC address // Create the result Intent and include the MAC address
val intent = Intent() 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 // Set result and finish this Activity
setResult(RESULT_OK, intent) setResult(RESULT_OK, intent)
println("selected")
finish() 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>(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 { companion object {
/** /**
@ -180,5 +157,6 @@ class DeviceListActivity : Activity() {
* Return Intent extra * Return Intent extra
*/ */
var EXTRA_DEVICE_ADDRESS = "device_address" var EXTRA_DEVICE_ADDRESS = "device_address"
var EXTRA_DEVICE_NAME = "device_name"
} }
} }

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground">
<ImageView
android:id="@+id/bluetooth_icon"
android:layout_width="64dp"
android:layout_height="64dp"
android:padding="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="@+id/list_device_name"
app:srcCompat="@drawable/ic_baseline_bluetooth_24" />
<TextView
android:id="@+id/list_device_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?attr/textAppearanceBody1"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/list_device_address"
app:layout_constraintStart_toEndOf="@+id/bluetooth_icon"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<TextView
android:id="@+id/list_device_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?attr/textAppearanceCaption"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/bluetooth_icon"
app:layout_constraintTop_toBottomOf="@+id/list_device_name" />
</androidx.constraintlayout.widget.ConstraintLayout>

@ -23,7 +23,7 @@
android:id="@+id/textView_device_name" android:id="@+id/textView_device_name"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?attr/textAppearanceHeadline6" android:textAppearance="?attr/textAppearanceBody1"
android:text="TextView" android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/textView_device_address" app:layout_constraintBottom_toTopOf="@+id/textView_device_address"
app:layout_constraintStart_toEndOf="@+id/imageView" app:layout_constraintStart_toEndOf="@+id/imageView"
@ -34,7 +34,7 @@
android:id="@+id/textView_device_address" android:id="@+id/textView_device_address"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?attr/textAppearanceBody2" android:textAppearance="?attr/textAppearanceCaption"
android:text="TextView" android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView" app:layout_constraintStart_toEndOf="@+id/imageView"

Loading…
Cancel
Save