繁体   English   中英

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

[英]Sending bluetooth LE data in advertisement on iOS

我的应用程序作为蓝牙 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 的文档和在线中可以找到的每一个小片段。 共识似乎是 Core Bluetooth 忽略数据,因为仅支持 CBAdvertisementDataLocalNameKey 和 CBAdvertisementDataServiceUUIDsKey。 以上编译并运行良好,我可以在我的 BT 扫描仪应用程序中“我希望它有效”,但我的两个自定义数据似乎不起作用。

有什么办法可以绕过这个吗? CoreBluetooth 的任何可接受的替代品或我遗漏的任何完全愚蠢的东西?

谢谢,

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

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

问题是,关键发送自定义值CBAdvertisementDataManufacturerDataKey不支持CBPeripheralManager

在 iOS 12 之后,当您设置CBAdvertisementDataManufacturerDataKey并开始广告时,您甚至会在控制台中收到错误消息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM