【问题标题】:Convert UUID to String representation eg: 1816 to Cycling Speed And Cadence将 UUID 转换为字符串表示,例如:1816 转换为 Cycling Speed 和 Cadence
【发布时间】:2020-11-08 07:46:09
【问题描述】:

我正在通过蓝牙连接到 Cycling Speed and Cadence 服务,并将外围设备保存到数组中(用于表格视图)。

我已经实现了这个结构

struct BTPeripheral: Identifiable {
    let id: Int
    let name: String
    let uuid: UUID
    let desc: String
}

我打电话是这样

discoveredBTDevices.append(BTPeripheral.init(
                           id: discoveredBTDevices.count, 
                           name: peripheral.name!, 
                           uuid: peripheral.identifier, 
                           desc: service.uuid.uuidstring) // trying service.uuid results in error
)

导致这个输出

id: 0, 
name: "Wahoo CADENCE 1DE8", 
uuid: E98F3843-1C6A-E4DE-6EF3-76B013950007, 
desc: "1816"

描述为字符串1816(即cycle Speed & Cad服务的UUID)

let serviceCyclingSpeedAndCadenceUUID             = CBUUID(string: "1816")

如何将 1816 转换为打印出来的字符串“Cycling Speed and Cadence”

print(service.uuid) // this results in "cycling Speed and Cadence"

如上所述,输入service.uuid 会导致错误Cannot convert value of type 'CBUUID' to expected argument type 'String'

【问题讨论】:

标签: swift core-bluetooth


【解决方案1】:

你可以得到它的description(这就是print最终会做的事情,因为CBUUID符合CustomStringConvertible):

service.uuid.description

或者使用字符串插值:

"\(service.uuid)"

debugDescription 也会产生相同的结果,但我不会使用它,除非这实际上是用于调试。

【讨论】:

  • 我尝试过 (service.uuid) 但不知道我还需要包含引号。我之前也尝试过 peripheral.description,但这只是给了我一长串项目。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-01
  • 2015-05-04
  • 1970-01-01
  • 1970-01-01
  • 2016-06-04
  • 1970-01-01
  • 2015-11-14
相关资源
最近更新 更多