在 iOS 上的广告中发送蓝牙 LE 数据

Dan*_*sma 5 core-bluetooth bluetooth-lowenergy ios-bluetooth swift cbperipheralmanager

我的应用程序作为蓝牙 LE 外围设备运行,我试图在广告中发送几个字节的自定义数据。

func btStartBroadcasting(peripheral: CBPeripheralManager!) {

    // create an array of bytes to send
    var byteArray = [UInt8]()
    byteArray.append(0b11011110); // 'DE'
    byteArray.append(0b10101101); // 'AD'

    // convert that array into an NSData object
    var manufacturerData = NSData(bytes: byteArray,length: byteArray.count)

    // define a UIUD for the service
    let theUUid = CBUUID(NSUUID: uuid)

    // build the bundle of data
    let dataToBeAdvertised:[String: AnyObject!] = [
        CBAdvertisementDataLocalNameKey : "I wish this worked",
        CBAdvertisementDataManufacturerDataKey : manufacturerData,
        CBAdvertisementDataServiceUUIDsKey : [theUUid],
    ]

    peripheral.startAdvertising(dataToBeAdvertised)

}
Run Code Online (Sandbox Code Playgroud)

但 CBAdvertisementDataManufacturerDataKey 中的数据集似乎被删除并且没有通过无线电发送出去。我已经阅读了苹果文档和网上能找到的所有关于此问题的小片段。共识似乎是核心蓝牙忽略数据,因为仅支持 CBAdvertisementDataLocalNameKey 和 CBAdvertisementDataServiceUUIDsKey。上面的代码编译并运行良好,我可以在我的 BT 扫描仪应用程序中“希望这能起作用”,但我的两位自定义数据似乎不起作用。

有什么办法可以避免这个问题吗?有什么可以接受的 CoreBluetooth 替代品或者我错过的任何完全愚蠢的东西吗?

谢谢,

Bog*_*dru 2

问题可能是您发送了太多字节。

规范将广告包数据长度限制为 31 个字节。从您的示例来看,字符串已经有 18 个字节,而 UUID 有 16 个字节,所以已经太多了。