CBCharacteristic:是「特征」还是「数据通道」?

目录

  1. 名词困惑:两种中文译法的由来
  2. 官方定义 & 开发者视角
  3. 乐高类比:文件夹与文件
  4. 智能手表实例:Characteristic 长什么样?
  5. iOS 代码实战:读 / 写 / 订阅
  6. 小结 & Best Practice

1. 名词困惑:为什么有两种翻译?

英文常见翻译强调点
Characteristic特征(直译)它是 Service 里的一个“特性”或“属性”
数据通道(意译)它是真正承载 数据流 的最小单位

一句话记忆Characteristic = 一个带权限的键值对 (UUID ➜ Data),你对它 读 / 写 / 监听,数据就在这条“管道”中流动。


2. 官方定义 & 开发者视角

“A value used by a service, plus its metadata and permitted operations.” —— Bluetooth GATT

  • Value:真正的数据 (Data)
  • Metadata:UUID、描述符、属性位图
  • Permitted operations.read .write .notify .indicate …

对 iOS 开发者而言:只有拿到 CBCharacteristic,才能调用

peripheral.readValue(for:)
peripheral.writeValue(_:for:type:)
peripheral.setNotifyValue(true, for:)

Service 只是目录,Characteristic 才能触碰“文件内容”。


3. 乐高类比

BLE 元素类比日常解释
Peripheral路由器硬件本体
ServiceUSB 共享文件夹分类功能
Characteristic文件真正存/取数据
Property 位图文件权限读、写、订阅、签名…

把 “文件” 读 / 写 / 订阅通知,就是在 Characteristic 管道 中收发字节流。


4. 智能手表实例

ServiceCharacteristic典型属性用途
Heart Rate (0x180D)Measurement (0x2A37)Notify实时心率流
Battery (0x180F)Level (0x2A19)Read/Notify电量 %
Device Info (0x180A)Firmware Rev (0x2A26)Read显示版本
OTA 自定义Control Point (FF01)Write升级指令
Data Packet (FF02)Write Without Response升级数据块

一块主流手表大约 15–25 条 Characteristic,所有心率、计步、推送、升级字节都从这些“管道”进出。


5. iOS 代码实战

5.1 发现并订阅心率

// ⚑ 已连接 peripheral
let heartRateService = CBUUID(string: "180D")
let measurementChar  = CBUUID(string: "2A37")

peripheral.discoverServices([heartRateService])

func peripheral(_ p: CBPeripheral, didDiscoverServices error: Error?) {
    guard let service = p.services?.first else { return }
    p.discoverCharacteristics([measurementChar], for: service)
}

func peripheral(_ p: CBPeripheral,
                didDiscoverCharacteristicsFor service: CBService,
                error: Error?) {
    if let ch = service.characteristics?
                 .first(where: { $0.uuid == measurementChar }) {
        p.setNotifyValue(true, for: ch)          // 订阅
    }
}

5.2 解析 Heart Rate Measurement

func peripheral(_ p: CBPeripheral,
                didUpdateValueFor ch: CBCharacteristic,
                error: Error?) {
    guard let data = ch.value else { return }
    let flag = data[0]
    let bpm: Int = flag & 0x01 == 0
        ? Int(data[1])                          // 8-bit
        : Int(UInt16(littleEndian:
              data.withUnsafeBytes { $0.load(fromByteOffset: 1,
                                             as: UInt16.self) }))
    print("❤️ \(bpm) BPM")
}

flag & 0x01 按 GATT 规范判断 8-bit / 16-bit。

5.3 写入震动指令(自定义特征)

let vibrationCmd = Data([0x01, 0x64]) // 开启震动、强度 100
peripheral.writeValue(vibrationCmd,
                      for: vibrationChar,
                      type: .withResponse)

6. 小结 & Best Practice

  • 翻译不重要,理解最重要:Characteristic 即“Service 中可操作的数据单元”。

  • 设计协议时:

    • 一功能一条 Characteristic,别塞“大杂烩”。
    • 量大时拆 Control / Data 双通道。
    • 能用 SIG 标准 UUID 就别自创。
  • 代码层面:抽常量、集中解析 Data,属性不符立刻抛错,避免隐式失败。

搞清楚 Characteristic 的角色,你就彻底打通 BLE 数据之路:读、写、订阅皆归一处,“特征” 即 “数据通道”。祝调试顺畅!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

依旧风轻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值