feat: 接入真实信号强度显示(之前写死满格)

- AppEvent 新增 SignalChanged 事件
- SystemStateMonitor 注册 PhoneStateListener 监听信号强度
- HomeFragment 接收事件更新 StatusBar 信号条(0-4级)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
dongliang
2026-05-07 13:32:25 +09:30
parent a5453d1013
commit 6f11784b8a
3 changed files with 28 additions and 0 deletions

View File

@@ -23,6 +23,8 @@ sealed class AppEvent {
data class BluetoothStateChanged(val isOn: Boolean) : AppEvent() data class BluetoothStateChanged(val isOn: Boolean) : AppEvent()
data class BluetoothDeviceConnected(val deviceName: String) : AppEvent() data class BluetoothDeviceConnected(val deviceName: String) : AppEvent()
data class BluetoothDeviceDisconnected(val deviceName: String) : AppEvent() data class BluetoothDeviceDisconnected(val deviceName: String) : AppEvent()
/** 信号强度变化 (level: 0-4) */
data class SignalChanged(val level: Int) : AppEvent()
// 消息通知 // 消息通知
/** 新任务到达(携带任务 ID 列表,横幅+红点用) */ /** 新任务到达(携带任务 ID 列表,横幅+红点用) */

View File

@@ -7,6 +7,9 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.os.BatteryManager import android.os.BatteryManager
import android.telephony.PhoneStateListener
import android.telephony.SignalStrength
import android.telephony.TelephonyManager
import com.xiaoqu.watch.event.AppEvent import com.xiaoqu.watch.event.AppEvent
import com.xiaoqu.watch.event.EventBus import com.xiaoqu.watch.event.EventBus
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
@@ -37,6 +40,16 @@ class SystemStateMonitor @Inject constructor(
/** 是否已注册 */ /** 是否已注册 */
private var registered = false private var registered = false
/** 信号强度监听器 */
private val phoneStateListener = object : PhoneStateListener() {
@Suppress("DEPRECATION")
override fun onSignalStrengthsChanged(signalStrength: SignalStrength) {
// 将信号强度转换为 0-4 级
val level = signalStrength.level // API 23+ 返回 0-4
emitEvent(AppEvent.SignalChanged(level))
}
}
/** 广播接收器 */ /** 广播接收器 */
private val receiver = object : BroadcastReceiver() { private val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
@@ -64,6 +77,11 @@ class SystemStateMonitor @Inject constructor(
addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED) addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED)
} }
context.registerReceiver(receiver, filter) context.registerReceiver(receiver, filter)
// 注册信号强度监听
val telephony = context.getSystemService(Context.TELEPHONY_SERVICE) as? TelephonyManager
telephony?.listen(phoneStateListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS)
registered = true registered = true
Timber.d("系统状态监听: 已注册") Timber.d("系统状态监听: 已注册")
} }
@@ -77,6 +95,10 @@ class SystemStateMonitor @Inject constructor(
} catch (e: Exception) { } catch (e: Exception) {
Timber.w(e, "系统状态监听: 取消注册异常") Timber.w(e, "系统状态监听: 取消注册异常")
} }
// 取消信号监听
val telephony = context.getSystemService(Context.TELEPHONY_SERVICE) as? TelephonyManager
telephony?.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE)
registered = false registered = false
Timber.d("系统状态监听: 已取消注册") Timber.d("系统状态监听: 已取消注册")
} }

View File

@@ -533,6 +533,10 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>() {
is AppEvent.BluetoothStateChanged -> { is AppEvent.BluetoothStateChanged -> {
statusBar.updateBluetooth(event.isOn) statusBar.updateBluetooth(event.isOn)
} }
// 信号强度变化
is AppEvent.SignalChanged -> {
statusBar.updateSignal(event.level)
}
// 蓝牙设备连接/断开 → 更新设置页 // 蓝牙设备连接/断开 → 更新设置页
is AppEvent.BluetoothDeviceConnected -> { is AppEvent.BluetoothDeviceConnected -> {
updateConfigBluetooth(true) updateConfigBluetooth(true)