cloudroam
2025-02-14 2fce91b8c0faf1290d8a35ee022dab3cdbc28a54
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.example.firstapp.utils
 
import android.bluetooth.BluetoothAdapter
import android.content.Context
import android.content.pm.PackageManager
import androidx.core.content.ContextCompat
 
@Suppress("DEPRECATION", "MemberVisibilityCanBePrivate")
object BluetoothUtils {
 
    /**
     * 检查应用是否具有蓝牙权限
     */
    fun hasBluetoothPermission(context: Context): Boolean {
        return ContextCompat.checkSelfPermission(context, android.Manifest.permission.BLUETOOTH) == PackageManager.PERMISSION_GRANTED
                && ContextCompat.checkSelfPermission(context, android.Manifest.permission.BLUETOOTH_ADMIN) == PackageManager.PERMISSION_GRANTED
    }
 
    /**
     * 检查蓝牙是否已启用
     */
    fun isBluetoothEnabled(): Boolean {
        val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
        return bluetoothAdapter != null && bluetoothAdapter.isEnabled
    }
 
    /**
     * 检查设备是否支持蓝牙功能
     */
    fun hasBluetoothCapability(context: Context): Boolean {
        if (!hasBluetoothPermission(context)) {
            Log.e("BluetoothUtils", "hasBluetoothCapability: no bluetooth permission")
            return false
        }
 
        return context.packageManager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)
    }
}