跳到主要内容

蓝牙通信

BlinkLife 通过 BLE 蓝牙与智能指环/按键外设通信,实现运动中的无感打点。通信分两层:Flutter 层(扫描/连接)+ 原生层(GATT 按键监听 + KeepAlive 保活)。

架构

Flutter 层 原生层 (Kotlin)
┌──────────────────┐ ┌──────────────────┐
│ BluetoothService │ │ MainActivity.kt │
│ (扫描/连接 UI) │ │ (GATT/保活) │
└────────┬─────────┘ └────────┬─────────┘
│ flutter_blue_plus │
└────────────┬───────────────────┘
│ MethodChannel
NativeBluetoothService
(Flutter ↔ 原生桥接)

MethodChannel 接口

通道名:blinklife/bluetooth

方向方法说明
Flutter→原生startKeyListening(deviceId)启动 GATT 按键监听
Flutter→原生stopKeyListening停止监听
Flutter→原生setRecordingState(bool)通知录制状态
Flutter→原生setGestureExclusionRects(bool)屏蔽左边缘返回手势
原生→FlutteronBluetoothStatus(status)连接状态回调
原生→FlutteronKeyPressed(keyCode)按键事件回调

保活策略

参数录制中非录制
KeepAlive 间隔3 秒20 秒
重连上限无限10 次
退避策略指数 1s→32s指数 1s→32s
KeepAlive 方式readBatteryLevel / readRemoteRssi同左

按键映射

keyCode方向默认动作(足球)
0x10up射门
0x11down传球
0x12left犯规
0x13right任意球
0x14center精彩
unknown_*center (兜底)精彩

蓝牙事件与生命周期冲突

蓝牙连接/断连事件会误触发 AppLifecycleState.paused。录制页使用 5 秒冷却期机制:

if (_bleEventCooldown) return; // 跳过 lifecycle autoSave
_bleEventCooldown = true;
Timer(Duration(seconds: 5), () => _bleEventCooldown = false);

相关文档