将 UUID 转换为字符串表示形式,例如:1816 为骑行速度和节奏

Convert UUID to String representation eg: 1816 to Cycling Speed And Cadence

我正在通过蓝牙连接到 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'

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

service.uuid.description

或者使用字符串插值:

"\(service.uuid)"

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