4

我的应用程序作为蓝牙 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)

}

但看起来 CBAdvertisementDataManufacturerDataKey 中的数据集正在被剥离,而不是通过无线电发送出去。我已经阅读了在 Apple 的文档和在线中可以找到的所有相关信息。共识似乎是核心蓝牙忽略数据,因为仅支持 CBAdvertisementDataLocalNameKey 和 CBAdvertisementDataServiceUUIDsKey。上面的编译和运行良好,我可以在我的 BT 扫描仪应用程序中“我希望这能工作”,但我的两位自定义数据似乎不起作用。

有什么办法可以规避这个;CoreBluetooth 的任何可接受的替代品或我缺少的任何完全愚蠢的东西?

谢谢,

4

2 回答 2

2

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

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

于 2015-03-30T06:43:22.347 回答
0

CBAdvertisementDataManufacturerDataKey问题是不支持为 key 发送自定义值CBPeripheralManager

从 iOS 12 开始,您甚至在设置CBAdvertisementDataManufacturerDataKey和开始广告时都会在控制台中收到错误消息。

于 2020-05-12T13:09:47.527 回答