feat: add option for automatically connecting to Bluetooth devices
This commit is contained in:
@@ -117,6 +117,7 @@ class MainActivity : ComponentActivity() {
|
||||
private var specifiedDeviceAddress by mutableStateOf<String?>(null)
|
||||
private var searchOrderList by mutableStateOf(listOf<String>())
|
||||
private var showDisconnectButton by mutableStateOf(false)
|
||||
private var autoConnectEnabled by mutableStateOf(true)
|
||||
|
||||
|
||||
private val settingsPrefs by lazy { getSharedPreferences("app_settings", Context.MODE_PRIVATE) }
|
||||
@@ -313,6 +314,12 @@ class MainActivity : ComponentActivity() {
|
||||
saveSettings()
|
||||
Log.d(TAG, "Set specified device address: $address")
|
||||
},
|
||||
autoConnectEnabled = autoConnectEnabled,
|
||||
onAutoConnectEnabledChange = { enabled ->
|
||||
autoConnectEnabled = enabled
|
||||
saveSettings()
|
||||
Log.d(TAG, "Auto connect enabled: $enabled")
|
||||
},
|
||||
|
||||
|
||||
latestRecord = latestRecord,
|
||||
@@ -615,6 +622,11 @@ class MainActivity : ComponentActivity() {
|
||||
|
||||
|
||||
private fun startAutoScanAndConnect() {
|
||||
if (!autoConnectEnabled) {
|
||||
Log.d(TAG, "Auto connect disabled, skipping auto scan")
|
||||
return
|
||||
}
|
||||
|
||||
Log.d(TAG, "Starting auto scan and connect")
|
||||
|
||||
if (!hasBluetoothPermissions()) {
|
||||
@@ -774,9 +786,11 @@ class MainActivity : ComponentActivity() {
|
||||
searchOrderStr.split(",").filter { it.isNotBlank() }
|
||||
}
|
||||
|
||||
autoConnectEnabled = settingsPrefs.getBoolean("auto_connect_enabled", true)
|
||||
|
||||
bleClient.setSpecifiedDeviceAddress(specifiedDeviceAddress)
|
||||
|
||||
Log.d(TAG, "Loaded settings deviceName=${settingsDeviceName} tab=${currentTab} specifiedDevice=${specifiedDeviceAddress} searchOrder=${searchOrderList.size}")
|
||||
Log.d(TAG, "Loaded settings deviceName=${settingsDeviceName} tab=${currentTab} specifiedDevice=${specifiedDeviceAddress} searchOrder=${searchOrderList.size} autoConnect=${autoConnectEnabled}")
|
||||
}
|
||||
|
||||
|
||||
@@ -794,6 +808,7 @@ class MainActivity : ComponentActivity() {
|
||||
.putBoolean("map_railway_visible", mapRailwayLayerVisible)
|
||||
.putString("specified_device_address", specifiedDeviceAddress)
|
||||
.putString("search_order_list", searchOrderList.joinToString(","))
|
||||
.putBoolean("auto_connect_enabled", autoConnectEnabled)
|
||||
|
||||
mapCenterPosition?.let { (lat, lon) ->
|
||||
editor.putFloat("map_center_lat", lat.toFloat())
|
||||
@@ -810,7 +825,7 @@ class MainActivity : ComponentActivity() {
|
||||
|
||||
bleClient.setHighFrequencyReconnect(true)
|
||||
|
||||
if (hasBluetoothPermissions() && !bleClient.isConnected()) {
|
||||
if (hasBluetoothPermissions() && !bleClient.isConnected() && autoConnectEnabled) {
|
||||
Log.d(TAG, "App resumed and not connected, starting auto scan")
|
||||
startAutoScanAndConnect()
|
||||
} else if (bleClient.isConnected()) {
|
||||
@@ -847,6 +862,8 @@ fun MainContent(
|
||||
specifiedDeviceAddress: String?,
|
||||
searchOrderList: List<String>,
|
||||
onSpecifiedDeviceSelected: (String?) -> Unit,
|
||||
autoConnectEnabled: Boolean,
|
||||
onAutoConnectEnabledChange: (Boolean) -> Unit,
|
||||
|
||||
|
||||
latestRecord: TrainRecord?,
|
||||
@@ -1118,7 +1135,9 @@ fun MainContent(
|
||||
onScrollPositionChange = onSettingsScrollPositionChange,
|
||||
specifiedDeviceAddress = specifiedDeviceAddress,
|
||||
searchOrderList = searchOrderList,
|
||||
onSpecifiedDeviceSelected = onSpecifiedDeviceSelected
|
||||
onSpecifiedDeviceSelected = onSpecifiedDeviceSelected,
|
||||
autoConnectEnabled = autoConnectEnabled,
|
||||
onAutoConnectEnabledChange = onAutoConnectEnabledChange
|
||||
)
|
||||
3 -> MapScreen(
|
||||
records = if (allRecords.isNotEmpty()) {
|
||||
|
||||
@@ -38,7 +38,9 @@ fun SettingsScreen(
|
||||
onScrollPositionChange: (Int) -> Unit = {},
|
||||
specifiedDeviceAddress: String? = null,
|
||||
searchOrderList: List<String> = emptyList(),
|
||||
onSpecifiedDeviceSelected: (String?) -> Unit = {}
|
||||
onSpecifiedDeviceSelected: (String?) -> Unit = {},
|
||||
autoConnectEnabled: Boolean = true,
|
||||
onAutoConnectEnabledChange: (Boolean) -> Unit = {}
|
||||
) {
|
||||
val uriHandler = LocalUriHandler.current
|
||||
val scrollState = rememberScrollState()
|
||||
@@ -262,6 +264,29 @@ fun SettingsScreen(
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Column {
|
||||
Text(
|
||||
"自动连接",
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
fontWeight = FontWeight.Medium
|
||||
)
|
||||
Text(
|
||||
"自动连接蓝牙设备",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
}
|
||||
Switch(
|
||||
checked = autoConnectEnabled,
|
||||
onCheckedChange = onAutoConnectEnabledChange
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user