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

时间:2015-01-24 18:27:43

标签: swift bluetooth-lowenergy core-bluetooth ios-bluetooth 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)

}

但看起来CBAdvertisementDataManufacturerDataKey中的数据集被剥离而不是通过无线电发送出去。我已经在Apple的文档和在线阅读了我能找到的关于此的所有小废料。共识似乎是Core Bluetooth忽略了数据,因为只支持CBAdvertisementDataLocalNameKey和CBAdvertisementDataServiceUUIDsKey。以上编译并运行正常,我可以在我的BT扫描仪应用程序中“我希望这有效”,但我的两位自定义数据似乎不起作用。

有没有办法绕过这个;任何可接受的CoreBluetooth替代品或任何我都缺少的完全愚蠢的东西?

谢谢,

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

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

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

相关问题