ios - 在 Swift 中将 CBUUID 转换为字符串

标签 ios swift core-bluetooth

我正在扫描蓝牙设备。我想将它们记录在一个字符串数组中。我希望能够保存它们,但出现以下错误:

无法将类型“CBUUID”(0x1f2760918)的值转换为“NSString”(0x1f26a42d0)。

CoreBT[9728:3053605] 无法将类型“CBUUID”(0x1f2760918)的值转换为“NSString”(0x1f26a42d0)。

我在下面有以下代码。没有对数组的引用,因为我什至无法将其转换为字符串。

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {

    if let peripheralName = advertisementData[CBAdvertisementDataLocalNameKey] as? String {


        print("peripheral Name: \(peripheralName)")

        let uniqueID = (advertisementData["kCBAdvDataServiceUUIDs"] as! NSArray).firstObject! as! String 
        // The above line produces an error
        //Removing as! String will work prevent error but still cannot get from "any" to "string"

        let x = advertisementData
        print("x: \(x)")
        let y = advertisementData["kCBAdvDataServiceUUIDs"]
        print("y: \(y)")

        print("uniqueID: \(uniqueID)")
        self.UIDCountNumber = UIDCountNumber + 1
        self.UID_Count.text = String(self.UIDCountNumber)//label counting devices


        self.last_UID.text = uniqueID as? String //Label is not changing

    }
}

关于将这些作为字符串获取以便我可以将它们存储在数组中的任何想法。如果你有更好的方法请告诉我。谢谢!

最佳答案

Apple 提供了获取字符串的 API:

https://developer.apple.com/documentation/corebluetooth/cbuuid/1518742-uuidstring

var uuidString: String { get }

关于ios - 在 Swift 中将 CBUUID 转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53191695/

相关文章:

android - 在 Chartboost 中隐藏插页式广告

ios - Core Data中有类似sql "NOT IN"的函数吗?

objective-c - 为什么我真的需要使用协议(protocol)

ios - 为什么当我 pop/self.dismiss 我的 WKInterfacecontroller 时不调用 deinit

ios - 你如何覆盖只读属性以在子类中读写

ios - iOS 的 BLE 上是否可以使用 AVRCP?

iOS ADBannerview - 广告会自行消失吗?

swift - Core Plot 中的可点击图例

Bluetooth Low energy最佳能耗策略

swift - 为什么 Swift 参数标签对 BLE 回调很重要