Skip to content

Refactor: Localization singleton, Gen3 BLE handshake, error handling & docs#1

Open
dallascyclist wants to merge 8 commits into
GZguanshiwei:mainfrom
dallascyclist:feature/localization-refactor-and-gen3-handshake
Open

Refactor: Localization singleton, Gen3 BLE handshake, error handling & docs#1
dallascyclist wants to merge 8 commits into
GZguanshiwei:mainfrom
dallascyclist:feature/localization-refactor-and-gen3-handshake

Conversation

@dallascyclist

@dallascyclist dallascyclist commented Feb 9, 2026

Copy link
Copy Markdown

Summary / 摘要

This PR refactors the QIUI-API demo app to improve developer experience, fix bugs found during live device testing, and add comprehensive documentation.

本PR重构了QIUI-API示例应用,改善了开发者体验,修复了真机测试中发现的Bug,并添加了完整的文档。


Changes / 变更内容

1. Centralized Localization / 统一本地化

Before / 之前: Three separate inline LocalizedMessages() dictionaries scattered across HKBabyBluetoothManager.m, APIBuletoothListViewController.m, and APIHomeViewController.m.

After / 之后: Single QIUILocalization singleton (QIUILocalization.h/.m) with L() macro. 47 bilingual keys (zh-Hans + en) organized by prefix (ble.*, scan.*, api.*, step.*, cmd.*, hud.*). Language detected once at startup. Fallback chain: detected language → zh-Hans → raw key.

之前: 三个独立的 LocalizedMessages() 字典分散在 HKBabyBluetoothManager.mAPIBuletoothListViewController.mAPIHomeViewController.m 中。

之后: 统一的 QIUILocalization 单例类,使用 L() 宏。47个中英双语键值,按前缀分类。启动时检测语言,回退链:检测语言 → zh-Hans → 原始键名。

2. Gen3 BLE Handshake Protocol / 三代BLE握手协议

Implemented the complete challenge-response handshake required by Gen3 (Cag Ink III / typeId 10) devices:

  • Fetch token command from buildCellMatePro4GTokenCmd API
  • Write hex bytes to BLE characteristic 36F5
  • Read device response from 36F6 notification
  • Validate session via decryBluetoothCommand API
  • State machine (BLEPendingOperation enum) routes BLE responses correctly

实现了三代设备(Cag Ink III / typeId 10)所需的完整挑战-响应握手:

  • buildCellMatePro4GTokenCmd API获取Token命令
  • 将十六进制字节写入BLE特征值 36F5
  • 36F6 通知读取设备响应
  • 通过 decryBluetoothCommand API验证会话
  • 状态机(BLEPendingOperation 枚举)正确路由BLE响应

3. Error Handling Fixes / 错误处理修复

  • Swallowed network errors: Added if (error) guards to 3 AFNetworking completion handlers that were silently failing (Btn1/Btn3/Btn4).

  • API error key mismatch: API returns "msg" on errors but code only checked "message". Now checks both: json[@"message"] ?: json[@"msg"].

  • User-facing alerts: Prerequisite validation (buttons pressed out of order) now shows UIAlertController alerts instead of only logging to console.

  • 网络错误被吞没: 为3个AFNetworking回调添加了 if (error) 错误守卫(按钮1/3/4)。

  • API错误键不匹配: API错误返回 "msg" 但代码只检查 "message",现在两个都检查。

  • 用户提示: 前置步骤验证现在显示 UIAlertController 提示,而不是仅打印日志。

4. Documentation / 文档

  • ARCHITECTURE.md: Complete technical reference with API endpoints, BLE protocol, and data flow diagrams.

  • Bilingual button labels: XIB updated with English subtitles on all 8 workflow buttons.

  • ARCHITECTURE.md: 完整技术参考,包含API端点、BLE协议、数据流图。

  • 双语按钮标签: XIB已更新,8个工作流按钮添加了英文副标题。

5. Minor Fixes / 其他修复

  • Fixed 5 missing localization keys that logged (null) during BLE scanning.

  • Switched from KeyPod API endpoints to CellMate Pro 4G endpoints for Gen3 devices.

  • Added .gitignore for xcuserdata/.

  • Added Xcode shared scheme for CI builds.

  • 修复了BLE扫描时打印 (null) 的5个缺失本地化键。

  • 将KeyPod API端点切换为CellMate Pro 4G端点以支持三代设备。

  • 添加 .gitignore 排除 xcuserdata/

  • 添加Xcode共享Scheme支持CI构建。


Files Changed / 修改的文件

File Change
QIUILocalization.h/.m New — Localization singleton / 新增本地化单例
APIHomeViewController.m Gen3 handshake, error handling, localization / 三代握手、错误处理、本地化
APIHomeViewController.xib Bilingual button labels / 双语按钮标签
APIBuletoothListViewController.m Localization migration / 本地化迁移
HKBabyBluetoothManager.m Localization migration + code cleanup / 本地化迁移+代码清理
project.pbxproj Added QIUILocalization to build / 添加QIUILocalization到构建
ARCHITECTURE.md New — Technical reference / 新增技术参考
.gitignore New — Exclude xcuserdata / 新增排除用户数据

Test Plan / 测试计划

  • Build succeeds (xcodebuild clean build)
  • Deployed and tested on physical iPhone 16 Pro Max
  • BLE scanning discovers real devices
  • Gen3 handshake completes successfully
  • Lock/unlock commands work end-to-end
  • Out-of-order button presses show helpful alerts
  • API errors display actual error messages (not generic fallback)
  • Chinese and English log output works correctly

Add project guidance file (CLAUDE.md) with architecture docs,
build instructions, and git identity config. Include 18 API
documentation PDFs in Docs/. Refactored BLE manager, home
controller, and Bluetooth list controller with bilingual
logging and cleaner structure.
Ignore xcuserdata/ directories. Document SSH remote alias
and key configuration in CLAUDE.md.
Comprehensive architecture reference covering all 18 QIUI API
endpoints, BLE protocol details, app workflow sequence diagrams,
source file function maps with line numbers, and 9 known issues
identified during analysis.
- Add complete Gen3 BLE challenge-response handshake protocol:
  write device token to BLE, read device response, validate via
  decryBluetoothCommand API before lock/unlock commands
- Fix CellMate API endpoints (was using KeyPod URLs)
- Fix BLE UUIDs for Gen3: FEE7/36F5/36F6 (was FFF0/FFF1/FFF2)
- Add hex-to-data conversion for proper BLE byte transmission
- Add prerequisite validation with alert feedback on all buttons
- Fix API error extraction: check both 'message' and 'msg' keys
- Fix typeId/expiresTime NSNumber-to-NSString conversion
- Add progress HUD with status updates during BLE handshake
- Number buttons 1-8 with bilingual Chinese/English labels in XIB
- Add showAlert helper to reduce UIAlertController duplication
- Add detailed debug logging for lock/unlock request/response
Document the challenge-response handshake flow, CellMate endpoint
table, BLE UUID differences between Gen2/Gen3, error response format
inconsistency, XIB outlet mismatch warning, and developer portal link.
Consolidate three per-file localization dictionaries into a single
QIUILocalization singleton with 47 keys organized by prefix (ble.*,
scan.*, api.*, step.*, cmd.*, hud.*). Language detection runs once
at launch; fallback chain: detected → zh-Hans → raw key.

- Fix 3 swallowed network errors in selectorBtn1/3/4 (missing if-error guards)
- Fix 5 missing scan keys that logged (null) during device testing
- Replace ~30 hardcoded English alert/HUD strings with L() calls
- Remove inline LocalizedMessages() from all three source files
Add Localization section covering singleton usage, L() macro,
key prefix conventions, fallback chain, and how to add strings
or new languages.
CLAUDE.md contains local development config (git identity, SSH aliases)
not relevant to the upstream project.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant