【问题标题】:Sending bluetooth LE data in advertisement on iOS在 iOS 上以广告形式发送蓝牙 LE 数据
【发布时间】:2015-03-23 14:33:57
【问题描述】:

我的应用程序作为蓝牙 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 的任何可接受的替代品或我缺少的任何完全愚蠢的东西?

谢谢,

【问题讨论】:

  • 你能发送一个 iBeacon 格式的广告吗?您在主要/次要字段中获得四个字节的数据。
  • 除非在 iOS 8 中有所改变,否则您不能修改制造商数据:stackoverflow.com/a/23790973/2128900

标签: swift bluetooth-lowenergy core-bluetooth ios-bluetooth cbperipheralmanager


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 这是不正确的。在 iOS 中做广告时,我可以使用 CBAdvertisementDataManufacturerDataKey
    • @eonist 如何使用 CBAdvertisementDataManufacturerDataKey 数据?我找不到有关更改 CBAdvertisementDataManufacturerDataKey 值的参考。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 2022-07-08
    • 1970-01-01
    相关资源
    最近更新 更多