feat: 蓝牙扫描与iBeacon模块

核心功能:
- BleScanner: BLE扫描封装,SCAN_MODE_LOW_POWER低功耗
- IBeaconParser: 从ScanRecord解析UUID/Major/Minor/TxPower
- BluetoothScanManager: 状态机管理常规/特殊双模式
  - 常规模式:周期扫描→上报API→检查新任务
  - 特殊模式:RSSI采样→均值判定→任务匹配→唤醒屏幕
- 设备过滤:名称含RFstar + 功率0<p<=100

集成:
- HomeFragment: MQTT type=4更新蓝牙参数,type=5控制启停
- 解绑时停止扫描
- 电量同步给扫描管理器

新增6文件,修改2文件。

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
dongliang
2026-04-29 12:06:59 +09:30
parent 866063b21c
commit e7fa7b3b1d
8 changed files with 630 additions and 2 deletions

View File

@@ -0,0 +1,57 @@
package com.xiaoqu.watch.data.bluetooth
/**
* 蓝牙扫描参数(服务端通过 MQTT type=4 下发,本地有默认值兜底)
* 对应旧版 bluetoothStore.js 的 dictParams
*/
data class BleScanParams(
/** 常规模式扫描轮数 */
val bluetoothRoutineCycle: Int = 1,
/** 常规模式每轮扫描时间(秒) */
val bluetoothRoutineCycleTime: Int = 20,
/** 常规模式采样次数 */
val bluetoothRoutineCycleTimes: Int = 1,
/** 特殊模式扫描轮数 */
val bluetoothSpecialCycle: Int = 5,
/** 特殊模式采样次数(也是信标移除阈值) */
val bluetoothSpecialCycleTimes: Int = 5,
/** RSSI 信号强度阈值(平均值 >= 此值判定为在范围内) */
val bluetoothSign: Int = 1,
/** 特殊模式→常规模式超时(秒) */
val specialToRoutineTime: Int = 3
) {
/** 常规模式扫描时长(毫秒) */
val routineScanMs: Long
get() = ((bluetoothRoutineCycle - bluetoothRoutineCycleTime).coerceAtLeast(1)) * 1000L
/** 常规模式休息时长(毫秒) */
val routineRestMs: Long
get() = bluetoothRoutineCycleTime * 1000L
/** 特殊模式扫描时长(毫秒) */
val specialScanMs: Long
get() = ((bluetoothSpecialCycle - bluetoothSpecialCycleTimes).coerceAtLeast(1)) * 1000L
/** 特殊模式休息时长(毫秒) */
val specialRestMs: Long
get() = bluetoothSpecialCycleTimes * 1000L
/** 特殊→常规超时(毫秒) */
val specialTimeoutMs: Long
get() = specialToRoutineTime * 1000L
companion object {
/** 从 MQTT JSON 解析参数,缺失字段用默认值 */
fun fromJson(json: org.json.JSONObject): BleScanParams {
return BleScanParams(
bluetoothRoutineCycle = json.optInt("bluetoothRoutineCycle", 1),
bluetoothRoutineCycleTime = json.optInt("bluetoothRoutineCycleTime", 20),
bluetoothRoutineCycleTimes = json.optInt("bluetoothRoutineCycleTimes", 1),
bluetoothSpecialCycle = json.optInt("bluetoothSpecialCycle", 5),
bluetoothSpecialCycleTimes = json.optInt("bluetoothSpecialCycleTimes", 5),
bluetoothSign = json.optInt("bluetoothSign", 1),
specialToRoutineTime = json.optInt("specialToRoutineTime", 3)
)
}
}
}

View File

@@ -0,0 +1,22 @@
package com.xiaoqu.watch.data.bluetooth
/**
* iBeacon 设备BLE 扫描结果解析后)
* 用于上报服务端和特殊模式 RSSI 追踪
*/
data class IBeaconDevice(
/** 蓝牙 MAC 地址 */
val mac: String,
/** 信号强度 (dBm, 负值, 如 -65) */
val rssi: Int,
/** iBeacon UUID */
val uuid: String,
/** iBeacon Major */
val major: Int,
/** iBeacon Minor */
val minor: Int,
/** 发射功率 (dBm) */
val txPower: Int,
/** 设备名称 */
val name: String
)