Bluetooth module | #8

DeviceListFragment.kt

import android.app.Activity
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothManager
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.snackbar.Snackbar
import com.neco_dev.bt_def.databinding.FragmentListBinding

class DeviceListFragment : Fragment(), ItemAdapter.Listener {
private lateinit var itemAdapter: ItemAdapter
private var bAdapter: BluetoothAdapter? = null
private lateinit var binding: FragmentListBinding
private lateinit var btLauncher: ActivityResultLauncher<Intent>

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentListBinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.imBluetoothOn.setOnClickListener {
btLauncher.launch(Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE))
}
initRcViews()
registerBtLauncher()
initBtAdapter()
bluetoothState()
}

private fun initRcViews() = with(binding){
rcViewPaired.layoutManager = LinearLayoutManager(requireContext())
itemAdapter = ItemAdapter(this@DeviceListFragment)
rcViewPaired.adapter = itemAdapter
}

private fun getPairedDevices(){
try {
val list = ArrayList<ListItem>()
val deviceList = bAdapter?.bondedDevices as Set<BluetoothDevice>
deviceList.forEach{
list.add(
ListItem(
it.name,
it.address,
false
)
)
}
binding.tvEmptyPaired.visibility = if(list.isEmpty()) View.VISIBLE else View.GONE
itemAdapter.submitList(list)
} catch (e: SecurityException){

}

}

private fun initBtAdapter(){
val bManager = activity?.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
bAdapter = bManager.adapter
}

private fun bluetoothState(){
if (bAdapter?.isEnabled == true){
changeButtonColor(binding.imBluetoothOn, Color.GREEN)
getPairedDevices()
}
}

private fun registerBtLauncher(){
btLauncher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
){
if (it.resultCode == Activity.RESULT_OK){
changeButtonColor(binding.imBluetoothOn, Color.GREEN)
getPairedDevices()
Snackbar.make(binding.root, "Блютуз включен!", Snackbar.LENGTH_LONG).show()
} else {
Snackbar.make(binding.root, "Блютуз выключен!", Snackbar.LENGTH_LONG).show()
}
}
}

override fun onClick(device: ListItem) {

}

}

ItemAdapter.kt

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.neco_dev.bt_def.databinding.ListItemBinding

class ItemAdapter(private val listener: Listener) :
ListAdapter<ListItem, ItemAdapter.MyHolder>(Comparator()) {
private var oldCheckBox: CheckBox? = null

class MyHolder(
view: View, private val adapter: ItemAdapter,
private val listener: Listener
) : RecyclerView.ViewHolder(view) {
private val b = ListItemBinding.bind(view)
private var device: ListItem? = null

init {
b.checkBox.setOnClickListener {
device?.let { it1 -> listener.onClick(it1) }
adapter.selectCheckBox(it as CheckBox)
}
itemView.setOnClickListener {
device?.let { it1 -> listener.onClick(it1) }
adapter.selectCheckBox(b.checkBox)
}
}

fun bind(item: ListItem) = with(b) {
device = item
name.text = item.name
mac.text = item.mac
}
}

class Comparator : DiffUtil.ItemCallback<ListItem>() {
override fun areItemsTheSame(oldItem: ListItem, newItem: ListItem): Boolean {
return oldItem == newItem
}

override fun areContentsTheSame(oldItem: ListItem, newItem: ListItem): Boolean {
return oldItem == newItem
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.list_item, parent, false)
return MyHolder(view, this, listener)
}

override fun onBindViewHolder(holder: MyHolder, position: Int) {
holder.bind(getItem(position))
}

fun selectCheckBox(checkBox: CheckBox) {
oldCheckBox?.isChecked = false
oldCheckBox = checkBox
oldCheckBox?.isChecked = true
}

interface Listener {
fun onClick(device: ListItem)
}
}

ListItem.kt

data class ListItem(
val name: String,
val mac: String,
val isChecked: Boolean
)

BaseActivity.kt

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class BaseActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_base)
// initRcView()
supportFragmentManager.beginTransaction()
.replace(R.id.placeHolder, DeviceListFragment()).commit()
}
}

Добавить комментарий

Ваш адрес email не будет опубликован.