蓝牙检测,需要用到以下权限:
检测工具类:
import android.bluetooth.BluetoothAdapter
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.os.Environment
import android.os.StatFs
import android.util.DisplayMetrics
import android.util.Log
import android.view.WindowManager
import java.io.BufferedReader
import java.io.FileReader
import kotlin.math.ceil
import kotlin.math.sqrt
class DeviceChecker(private val context: Context) {
// CPU 最低要求 1.5 GHz
private val requiredCpuMinGHz = 1.5
// RAM 最低要求 4 GB
private val requiredRamMinGB = 4.0
// ROM 最低要求 32 GB
private val requiredRomMinGB = 32.0
// 蓝牙最低要求 4.0
private val requiredBluetoothMinVersion = "4.0"
// 屏幕尺寸最小要求 10.1 英寸
private val requiredScreenSizeInches = 10.1
// 屏幕分辨率要求 1920x1080
private val requiredResolution = Pair(1080, 1920)
// Android 版本最低要求 10 (Q)
private val requiredAndroidSdk = Build.VERSION_CODES.Q
/**
* 检查设备是否满足所有要求
*/
fun isDeviceCompatible(): Boolean {
return isCpuCompatible() &&
isRamCompatible() &&
isRomCompatible() &&
// isBluetoothCompatible() &&
// isBluetooth4Supported() &&
isBluetooth4SupportedAndEnabled() &&
isScreenSizeCompatible() &&
isScreenResolutionCompatible() &&
isAndroidVersionCompatible()
}
/**
* CPU 频率是否满足 1.5 GHz
*/
fun isCpuCompatible(): Boolean {
val maxCpuGHz = getMaxCpuFrequency()
return maxCpuGHz >= requiredCpuMinGHz
}
private fun getMaxCpuFrequency(): Double {
try {
val maxFreqPath = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"
val reader = BufferedReader(FileReader(maxFreqPath))
val maxFreqStr = reader.readLine().trim()
reader.close()
val maxFreqHz = maxFreqStr.toDoubleOrNull() ?: 0.0
return maxFreqHz / 1_000_000.0 // 转换为 GHz
} catch (e: Exception) {
e.printStackTrace()
}
return 0.0
}
/**
* RAM 空间是否满足 4 GB
*/
fun isRamCompatible(): Boolean {
val totalRamGb = getTotalRamGb()
Log.d("caowj", "Total RAM: $totalRamGb GB")
return ceil(totalRamGb) >= requiredRamMinGB
}
private fun getTotalRamGb(): Double {
val activityManager =
context.getSystemService(Context.ACTIVITY_SERVICE) as android.app.ActivityManager
val memoryInfo = android.app.ActivityManager.MemoryInfo()
activityManager.getMemoryInfo(memoryInfo)
// totalMem 在 Android 8.0 及以上需要权限,可能无法获取
// 使用 Runtime 获取应用内存限制,不代表设备总内存
// 因此,这里尝试读取 /proc/meminfo
return try {
val reader = BufferedReader(FileReader("/proc/meminfo"))
var line: String?
var totalRamKb = 0L
while (reader.readLine().also { line = it } != null) {
if (line?.startsWith("MemTotal") == true) {
val parts = line?.split("\\s+".toRegex())
if (parts!!.size >= 2) {
totalRamKb = parts[1].toLongOrNull() ?: 0L
}
break
}
}
reader.close()
totalRamKb / (1024.0 * 1024.0) // 转换为 GB
} catch (e: Exception) {
e.printStackTrace()
0.0
}
}
/**
* ROM 空间是否满足 32 GB
*/
fun isRomCompatible(): Boolean {
val totalRomGb = getTotalInternalStorageGb()
Log.d("caowj", "Total ROM: $totalRomGb GB")
return totalRomGb >= requiredRomMinGB
}
private fun getTotalInternalStorageGb(): Double {
val path = Environment.getDataDirectory()
val stat = StatFs(path.path)
val blockSize = stat.blockSizeLong
val totalBlocks = stat.blockCountLong
val totalBytes = totalBlocks * blockSize
return totalBytes / (1024.0 * 1024.0 * 1024.0) // 转换为 GB
}
fun isBluetooth4SupportedAndEnabled(): Boolean {
// 1. 检查设备是否支持 BLE
if (!context.packageManager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
return false
}
// 2. 获取 BluetoothAdapter
val bluetoothManager =
context.getSystemService(Context.BLUETOOTH_SERVICE) as android.bluetooth.BluetoothManager?
val bluetoothAdapter = bluetoothManager?.adapter
// 3. 检查 BluetoothAdapter 是否可用且已启用
return bluetoothAdapter != null && bluetoothAdapter.isEnabled
}
/**
* 蓝牙版本是否满足 4.0
*/
fun isBluetooth4Supported(): Boolean {
return context.packageManager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)
}
/**
* 是否支持蓝牙 4.0 或更高版本
*/
fun isBluetoothCompatible(): Boolean {
return isBluetoothSupported()
// 获取蓝牙版本较为复杂,这里假设支持蓝牙即满足最低要求
// 实际项目中可能需要更复杂的逻辑或依赖设备信息
}
/**
* 检查设备是否支持蓝牙
* @return true 如果设备支持蓝牙,否则返回 false
*/
private fun isBluetoothSupported(): Boolean {
val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
return bluetoothAdapter != null
}
/**
* 屏幕尺寸是否满足 10.1 英寸
*/
fun isScreenSizeCompatible(): Boolean {
val screenSizeInches = getScreenSizeInches()
Log.d("caowj", "屏幕尺寸: $screenSizeInches inches")
return screenSizeInches >= requiredScreenSizeInches
}
private fun getScreenSizeInches(): Double {
val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val display = windowManager.defaultDisplay
val displayMetrics = DisplayMetrics()
display.getRealMetrics(displayMetrics)
val widthPixels = displayMetrics.widthPixels
val heightPixels = displayMetrics.heightPixels
val xdpi = displayMetrics.xdpi
val ydpi = displayMetrics.ydpi
Log.d("DeviceInfo", "Width Pixels: $widthPixels")
Log.d("DeviceInfo", "Height Pixels: $heightPixels")
Log.d("DeviceInfo", "XDPI: $xdpi")
Log.d("DeviceInfo", "YDPI: $ydpi")
// 计算宽度和高度(英寸)
val widthInches = widthPixels / xdpi
val heightInches = heightPixels / ydpi
Log.d("DeviceInfo", "Width Inches: $widthInches")
Log.d("DeviceInfo", "Height Inches: $heightInches")
// 计算对角线尺寸(英寸)
val sizeInches =
sqrt(((widthInches * widthInches + heightInches * heightInches).toDouble()))
Log.d("DeviceInfo", "Screen Size Inches: $sizeInches")
return sizeInches
}
/**
*屏幕分辨率是否满足 1920x1080
*/
fun isScreenResolutionCompatible(): Boolean {
val (width, height) = getScreenResolution()
Log.d("caowj", "Screen Resolution: $width x $height")
return width >= requiredResolution.first && height >= requiredResolution.second
}
private fun getScreenResolution(): Pair {
val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getRealMetrics(displayMetrics)
return Pair(displayMetrics.widthPixels, displayMetrics.heightPixels)
}
/**
* Android 版本是否满足 10 (Q)
*/
fun isAndroidVersionCompatible(): Boolean {
return Build.VERSION.SDK_INT >= requiredAndroidSdk
}
}
调用示例:
private fun showCompatibilityDetails() {
val deviceChecker = DeviceChecker(this)
if (deviceChecker.isDeviceCompatible()) {
Toast.makeText(this, "设备满足所有要求", Toast.LENGTH_LONG).show()
} else {
Toast.makeText(this, "设备不满足要求,请检查硬件和软件配置", Toast.LENGTH_LONG).show()
// 可选:显示具体哪些不满足
val details = StringBuilder()
details.append("设备兼容性详情:\n")
details.append("CPU: ${if (deviceChecker.isCpuCompatible()) "满足" else "不满足"}\n")
details.append("RAM: ${if (deviceChecker.isRamCompatible()) "满足" else "不满足"}\n")
details.append("ROM: ${if (deviceChecker.isRomCompatible()) "满足" else "不满足"}\n")
details.append("蓝牙: ${if (deviceChecker.isBluetooth4Supported()) "满足" else "不满足"}\n")
details.append("屏幕尺寸: ${if (deviceChecker.isScreenSizeCompatible()) "满足" else "不满足"}\n")
details.append("屏幕分辨率: ${if (deviceChecker.isScreenResolutionCompatible()) "满足" else "不满足"}\n")
details.append("Android 版本: ${if (deviceChecker.isAndroidVersionCompatible()) "满足" else "不满足"}\n")
// 显示详情(例如通过对话框)
// 这里简单使用 Toast,实际项目中建议使用 AlertDialog 或自定义布局
Log.w("caowj", "判断结果:$details")
}
}