feat: modernize bluetooth apis
This commit is contained in:
@@ -12,8 +12,8 @@ android {
|
||||
applicationId = "org.noxylva.lbjconsole"
|
||||
minSdk = 29
|
||||
targetSdk = 35
|
||||
versionCode = 1
|
||||
versionName = "0.0.1"
|
||||
versionCode = 2
|
||||
versionName = "0.0.2"
|
||||
|
||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
|
||||
@@ -90,12 +90,13 @@ class BLEClient(private val context: Context) : BluetoothGattCallback() {
|
||||
try {
|
||||
scanCallback = callback
|
||||
this.targetDeviceName = targetDeviceName
|
||||
val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter() ?: run {
|
||||
val bluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
|
||||
val bluetoothAdapter = bluetoothManager.adapter ?: run {
|
||||
Log.e(TAG, "Bluetooth adapter unavailable")
|
||||
return
|
||||
}
|
||||
|
||||
if (!bluetoothAdapter.isEnabled) {
|
||||
if (bluetoothAdapter.isEnabled != true) {
|
||||
Log.e(TAG, "Bluetooth adapter disabled")
|
||||
return
|
||||
}
|
||||
@@ -150,13 +151,14 @@ class BLEClient(private val context: Context) : BluetoothGattCallback() {
|
||||
}
|
||||
|
||||
try {
|
||||
val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter() ?: run {
|
||||
val bluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
|
||||
val bluetoothAdapter = bluetoothManager.adapter ?: run {
|
||||
Log.e(TAG, "Bluetooth adapter unavailable")
|
||||
handler.post { onConnectionStateChange?.invoke(false) }
|
||||
return false
|
||||
}
|
||||
|
||||
if (!bluetoothAdapter.isEnabled) {
|
||||
if (bluetoothAdapter.isEnabled != true) {
|
||||
Log.e(TAG, "Bluetooth adapter is disabled")
|
||||
handler.post { onConnectionStateChange?.invoke(false) }
|
||||
return false
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.noxylva.lbjconsole
|
||||
import android.Manifest
|
||||
import android.bluetooth.BluetoothAdapter
|
||||
import android.bluetooth.BluetoothDevice
|
||||
import android.bluetooth.BluetoothManager
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import java.io.File
|
||||
@@ -77,7 +78,17 @@ class MainActivity : ComponentActivity() {
|
||||
private var targetDeviceName = "LBJReceiver"
|
||||
|
||||
|
||||
private val settingsPrefs by lazy { getSharedPreferences("app_settings", Context.MODE_PRIVATE) }
|
||||
private val settingsPrefs by lazy { getSharedPreferences("app_settings", Context.MODE_PRIVATE) }
|
||||
|
||||
private fun getAppVersion(): String {
|
||||
return try {
|
||||
val packageInfo = packageManager.getPackageInfo(packageName, 0)
|
||||
packageInfo.versionName ?: "Unknown"
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Failed to get app version", e)
|
||||
"Unknown"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private val requestPermissions = registerForActivityResult(
|
||||
@@ -279,6 +290,7 @@ class MainActivity : ComponentActivity() {
|
||||
Toast.makeText(this, "设备名称 '${settingsDeviceName}' 已保存,下次连接时生效", Toast.LENGTH_LONG).show()
|
||||
Log.d(TAG, "Applied settings deviceName=${settingsDeviceName}")
|
||||
},
|
||||
appVersion = getAppVersion(),
|
||||
locoInfoUtil = locoInfoUtil
|
||||
)
|
||||
|
||||
@@ -315,8 +327,9 @@ class MainActivity : ComponentActivity() {
|
||||
Log.d(TAG, "Connecting to device name=${device.name ?: "Unknown"} address=${device.address}")
|
||||
|
||||
// 检查蓝牙适配器状态
|
||||
val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
|
||||
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled) {
|
||||
val bluetoothManager = getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
|
||||
val bluetoothAdapter = bluetoothManager.adapter
|
||||
if (bluetoothAdapter == null || bluetoothAdapter.isEnabled != true) {
|
||||
deviceStatus = "蓝牙未启用"
|
||||
Log.e(TAG, "Bluetooth adapter unavailable or disabled")
|
||||
return
|
||||
@@ -422,7 +435,8 @@ class MainActivity : ComponentActivity() {
|
||||
|
||||
|
||||
private fun startScan() {
|
||||
val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
|
||||
val bluetoothManager = getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
|
||||
val bluetoothAdapter = bluetoothManager.adapter
|
||||
if (bluetoothAdapter == null) {
|
||||
Log.e(TAG, "Bluetooth adapter unavailable")
|
||||
deviceStatus = "设备不支持蓝牙"
|
||||
@@ -521,6 +535,7 @@ fun MainContent(
|
||||
deviceName: String,
|
||||
onDeviceNameChange: (String) -> Unit,
|
||||
onApplySettings: () -> Unit,
|
||||
appVersion: String,
|
||||
|
||||
|
||||
locoInfoUtil: LocoInfoUtil
|
||||
@@ -627,6 +642,7 @@ fun MainContent(
|
||||
deviceName = deviceName,
|
||||
onDeviceNameChange = onDeviceNameChange,
|
||||
onApplySettings = onApplySettings,
|
||||
appVersion = appVersion
|
||||
)
|
||||
3 -> MapScreen(
|
||||
records = if (allRecords.isNotEmpty()) allRecords else recentRecords,
|
||||
|
||||
@@ -82,7 +82,7 @@ fun TrainDetailDialog(
|
||||
DetailItem("机车类型", recordMap["loco_type"] ?: "--")
|
||||
DetailItem("列车类型", recordMap["lbj_class"] ?: "--")
|
||||
|
||||
Divider(modifier = Modifier.padding(vertical = 8.dp))
|
||||
HorizontalDivider(modifier = Modifier.padding(vertical = 8.dp))
|
||||
|
||||
|
||||
DetailItem("路线", recordMap["route"] ?: "--")
|
||||
|
||||
@@ -14,7 +14,8 @@ import androidx.compose.ui.unit.dp
|
||||
fun SettingsScreen(
|
||||
deviceName: String,
|
||||
onDeviceNameChange: (String) -> Unit,
|
||||
onApplySettings: () -> Unit
|
||||
onApplySettings: () -> Unit,
|
||||
appVersion: String = "Unknown"
|
||||
) {
|
||||
val uriHandler = LocalUriHandler.current
|
||||
|
||||
@@ -46,7 +47,7 @@ fun SettingsScreen(
|
||||
Spacer(modifier = Modifier.weight(1f))
|
||||
|
||||
Text(
|
||||
text = "LBJ Console v0.0.1 by undef-i",
|
||||
text = "LBJ Console v$appVersion by undef-i",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
modifier = Modifier.clickable {
|
||||
|
||||
Reference in New Issue
Block a user