76 lines
2.7 KiB
Kotlin
76 lines
2.7 KiB
Kotlin
package io.gitlab.jfronny.hc05ac.bt
|
|
|
|
import android.annotation.SuppressLint
|
|
import android.bluetooth.BluetoothAdapter
|
|
import android.bluetooth.BluetoothManager
|
|
import android.content.Context
|
|
import android.content.Intent
|
|
import android.os.Build
|
|
import android.os.Bundle
|
|
import android.view.View
|
|
import android.widget.*
|
|
import android.widget.AdapterView.OnItemClickListener
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
import io.gitlab.jfronny.hc05ac.R
|
|
import io.gitlab.jfronny.hc05ac.btAdapter
|
|
|
|
class DeviceList : AppCompatActivity() {
|
|
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.makeText(applicationContext, "Bluetooth device not available", Toast.LENGTH_LONG)
|
|
.show()
|
|
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.makeText(
|
|
applicationContext,
|
|
"No Paired Bluetooth Devices Found.",
|
|
Toast.LENGTH_LONG
|
|
).show()
|
|
}
|
|
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@DeviceList, LedControl::class.java)
|
|
i.putExtra(EXTRA_ADDRESS, address)
|
|
startActivity(i)
|
|
}
|
|
|
|
companion object {
|
|
@JvmField
|
|
var EXTRA_ADDRESS = "device_address"
|
|
}
|
|
} |