Bluetooth module | #12

DeviceListFragment.kt

import android.Manifest
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.content.SharedPreferences
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.util.Log
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 var preferences: SharedPreferences? = null
private lateinit var itemAdapter: ItemAdapter
private lateinit var discoveryAdapter: ItemAdapter
private var bAdapter: BluetoothAdapter? = null
private lateinit var binding: FragmentListBinding
private lateinit var btLauncher: ActivityResultLauncher<Intent>
private lateinit var pLauncher: ActivityResultLauncher<Array<String>>

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)
preferences = activity?.getSharedPreferences(BluetoothConstants.PREFERENCES, Context.MODE_PRIVATE)
binding.imBluetoothOn.setOnClickListener {
btLauncher.launch(Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE))
}
binding.imBluetoothSearch.setOnClickListener {
try{
bAdapter?.startDiscovery()
} catch (e: SecurityException){
}
}
intentFilters()
checkPermissions()
initRcViews()
registerBtLauncher()
initBtAdapter()
bluetoothState()
}

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

private fun getPairedDevices(){
try {
val list = ArrayList<ListItem>()
val deviceList = bAdapter?.bondedDevices as Set<BluetoothDevice>
deviceList.forEach{
list.add(
ListItem(
it,
preferences?.getString(BluetoothConstants.MAC, "") == it.address
)
)
}
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()
}
}
}

private fun checkPermissions(){
if(!checkBtPermissions()){
registerPermissionListener()
launchBtPermissions()
}
}

private fun launchBtPermissions(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S){
pLauncher.launch(arrayOf(
Manifest.permission.BLUETOOTH_CONNECT,
Manifest.permission.ACCESS_FINE_LOCATION
))
} else {
pLauncher.launch(arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION
))
}
}

private fun registerPermissionListener(){
pLauncher = registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()
){

}
}

private fun saveMac(mac: String){
val editor = preferences?.edit()
editor?.putString(BluetoothConstants.MAC, mac)
editor?.apply()
}

override fun onClick(item: ListItem) {
saveMac(item.device.address)
}

private val bReceiver = object : BroadcastReceiver(){
override fun onReceive(p0: Context?, intent: Intent?) {
if(intent?.action == BluetoothDevice.ACTION_FOUND){
val device = intent.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE)
val list = mutableSetOf<ListItem>()
list.addAll(discoveryAdapter.currentList)
if(device != null) list.add(ListItem(device, false))
discoveryAdapter.submitList(list.toList())
try{
Log.d("MyLog", "Device: ${device?.name}")
} catch (e: SecurityException){

}
} else if(intent?.action == BluetoothDevice.ACTION_BOND_STATE_CHANGED){

} else if(intent?.action == BluetoothAdapter.ACTION_DISCOVERY_FINISHED){

}
}
}

private fun intentFilters(){
val f1 = IntentFilter(BluetoothDevice.ACTION_FOUND)
val f2 = IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED)
val f3 = IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)
activity?.registerReceiver(bReceiver, f1)
activity?.registerReceiver(bReceiver, f2)
activity?.registerReceiver(bReceiver, f3)
}

}

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
try {
name.text = item.device.name
mac.text = item.device.address
} catch (e: SecurityException){}

if (item.isChecked) adapter.selectCheckBox(checkBox)
}
}

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

import android.bluetooth.BluetoothDevice

data class ListItem(
val device: BluetoothDevice,
val isChecked: Boolean
)

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

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