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