68 lines
2.4 KiB
Kotlin
68 lines
2.4 KiB
Kotlin
package io.gitlab.jfronny.hc05ac.bt
|
|
|
|
import android.annotation.SuppressLint
|
|
import android.bluetooth.BluetoothAdapter
|
|
import android.content.Intent
|
|
import android.os.Bundle
|
|
import android.view.View
|
|
import android.widget.*
|
|
import android.widget.AdapterView.OnItemClickListener
|
|
import io.gitlab.jfronny.hc05ac.R
|
|
import io.gitlab.jfronny.hc05ac.util.BaseActivity
|
|
import io.gitlab.jfronny.hc05ac.util.btAdapter
|
|
import io.gitlab.jfronny.hc05ac.util.toast
|
|
|
|
class DeviceListActivity : BaseActivity() {
|
|
private var deviceList: ListView? = null
|
|
private var bluetoothAdapter: BluetoothAdapter? = null
|
|
@SuppressLint("MissingPermission")
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
setContentView(R.layout.activity_device_list)
|
|
val btnPaired: Button = findViewById(R.id.button)
|
|
deviceList = findViewById(R.id.listView)
|
|
bluetoothAdapter = btAdapter
|
|
if (bluetoothAdapter == null) {
|
|
toast("Bluetooth device not available")
|
|
finish()
|
|
} else if (!bluetoothAdapter!!.isEnabled) {
|
|
val turnBTon = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
|
|
startActivityForResult(turnBTon, 1)
|
|
}
|
|
btnPaired.setOnClickListener { pairedDevicesList() }
|
|
}
|
|
|
|
@SuppressLint("MissingPermission")
|
|
private fun pairedDevicesList() {
|
|
val pairedDevices = bluetoothAdapter!!.bondedDevices
|
|
val list: MutableList<String> = ArrayList()
|
|
if (pairedDevices.size > 0) {
|
|
for (bt in pairedDevices) {
|
|
list.add(
|
|
"""
|
|
${bt.name}
|
|
${bt.address}
|
|
""".trimIndent()
|
|
)
|
|
}
|
|
} else {
|
|
toast("No Paired Bluetooth Devices Found.")
|
|
}
|
|
val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, list)
|
|
deviceList!!.adapter = adapter
|
|
deviceList!!.onItemClickListener = myListClickListener
|
|
}
|
|
|
|
private val myListClickListener =
|
|
OnItemClickListener { _: AdapterView<*>?, view: View, _: Int, _: Long ->
|
|
val info = (view as TextView).text.toString()
|
|
val address = info.substring(info.length - 17)
|
|
val i = Intent(this@DeviceListActivity, BtInputActivity::class.java)
|
|
i.putExtra(EXTRA_ADDRESS, address)
|
|
startActivity(i)
|
|
}
|
|
|
|
companion object {
|
|
var EXTRA_ADDRESS = "device_address"
|
|
}
|
|
} |