Bluetooth module | #10

DeviceListFragment

import android.Manifest
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.content.SharedPreferences
import android.graphics.Color
import android.os.Build
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 var preferences: SharedPreferences? = null
private lateinit var itemAdapter: 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))
}
checkPermissions()
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,
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(device: ListItem) {
saveMac(device.mac)
}

}

Extensions.kt

import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import android.widget.ImageButton
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.DrawableCompat
import androidx.fragment.app.Fragment

fun Fragment.changeButtonColor(button: ImageButton, color: Int){
val drawable = button.drawable
DrawableCompat.setTint(drawable, color)
button.setImageDrawable(drawable)
}

fun Fragment.checkBtPermissions(): Boolean{
return if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S){
ContextCompat.checkSelfPermission(
requireContext(),
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(
requireContext(),
Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED
} else {
ContextCompat.checkSelfPermission(
requireContext(),
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
}
}

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

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