Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @@ -0,0 +1,99 @@
- package com.bm.fasty.dashboard
- import android.Manifest
- import android.bluetooth.BluetoothAdapter
- import android.bluetooth.BluetoothDevice
- import android.bluetooth.BluetoothManager
- import android.content.Context
- import android.content.pm.PackageManager
- import android.os.Build
- import androidx.core.content.ContextCompat
- import androidx.fragment.app.DialogFragment
- import com.bm.fasty.dashboard.vo.showToast
- import java.lang.reflect.Method
- abstract class BaseDialogFragment : DialogFragment() {
- fun baseCheckBluetoothPermission(
- context: Context,
- bluetoothStatus: (Boolean?) -> Unit,
- requestPermissionBelow31: (BluetoothAdapter) -> Unit,
- requestPermissionAbove31: (BluetoothAdapter) -> Unit
- ): BluetoothAdapter? {
- // Check to see if the Bluetooth classic feature is available.
- val bluetoothAvailable =
- context.packageManager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)
- // Check to see if the BLE feature is available.
- val bluetoothLEAvailable =
- context.packageManager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)
- val bluetoothManager: BluetoothManager =
- context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
- val bluetoothAdapter: BluetoothAdapter? = bluetoothManager.adapter
- if (bluetoothAvailable || bluetoothLEAvailable) {
- if (bluetoothAdapter == null) {
- bluetoothStatus(false)
- showToast(context, "Bluetooth tidak tersedia pada device Anda")
- } else {
- if (!bluetoothAdapter.isEnabled) {
- showToast(context, "Mohon nyalakan bluetooth Anda")
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
- if (ContextCompat.checkSelfPermission(
- context,
- Manifest.permission.BLUETOOTH_CONNECT
- ) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(
- context,
- Manifest.permission.BLUETOOTH_SCAN
- ) == PackageManager.PERMISSION_GRANTED
- ) {
- // Bluetooth Connect permission is granted, proceed with Bluetooth activation
- requestPermissionBelow31(bluetoothAdapter)
- } else {
- // Bluetooth Connect permission is not granted, request it
- requestPermissionAbove31(bluetoothAdapter)
- }
- } else {
- requestPermissionBelow31(bluetoothAdapter)
- }
- } else {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
- if (ContextCompat.checkSelfPermission(
- context,
- Manifest.permission.BLUETOOTH_CONNECT
- ) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(
- context,
- Manifest.permission.BLUETOOTH_SCAN
- ) == PackageManager.PERMISSION_GRANTED
- ) {
- // Bluetooth Connect permission is granted, proceed with Bluetooth activation
- requestPermissionBelow31(bluetoothAdapter)
- } else {
- // Bluetooth Connect permission is not granted, request it
- requestPermissionAbove31(bluetoothAdapter)
- }
- } else {
- requestPermissionBelow31(bluetoothAdapter)
- }
- }
- }
- } else {
- bluetoothStatus(false)
- showToast(context, "Bluetooth tidak tersedia pada device Anda")
- }
- return bluetoothAdapter
- }
- fun isConnected(device: BluetoothDevice): Boolean {
- return try {
- val m: Method = device.javaClass.getMethod("isConnected")
- m.invoke(device) as Boolean
- } catch (e: Exception) {
- throw IllegalStateException(e)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement