如何在CoreBluetooth Swift中获得kCBAdvDataManufacturerData?

时间:2015-10-11 02:20:46

标签: ios iphone swift core-bluetooth

我有Broadcom BLE外围广播数据,其中包括以制造商数据形式公布的序列号。当我尝试在iOS(Swift)中提取它时,我无法弄清楚如何将其变为变量或常量。

我能够执行:

println("\(advertisementData)")

' advertisementData'内置于centralManager功能中。在控制台中我得到:

[kCBAdvDataIsConnectable:0,kCBAdvDataLocalName:mangos,kCBAdvDataManufacturerData:< 08e19515 ec7e49fd af85c719 37332ccd cafebabe>]

这是膨胀,但我需要将kCBAdvDataManufacturerData中的数据与字符串进行比较,如果它们相同则让它执行一个函数。

我究竟如何创建一个访问序列号/ kCBAdvDataManufacturerData的变量?我尝试了很多不同的路线。

功能代码:

func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {
    let localData = advertisementData as NSDictionary


println(localData.objectForKey(CBAdvertisementDataLocalNameKey))


var localName: NSString!
if (localData.objectForKey(CBAdvertisementDataLocalNameKey) == nil)
{
    localName = ""
}
else{
    localName = localData.objectForKey(CBAdvertisementDataLocalNameKey) as! NSString

    if (localName == "test"){

        println("Test Found!")
        println("\(advertisementData)")
    }
}

self.tLog(NSString(format: "Discovered %@, RSSI: %@\n", localName, RSSI))
self.discoveredPeripheral = peripheral

if (self.verboseMode())
{
    self.centralManager.connectPeripheral(peripheral, options: nil)
}
}

1 个答案:

答案 0 :(得分:1)

确认。想出来了:

println(advertisementData["kCBAdvDataManufacturerData"] as! NSData)