Sending bluetooth LE data in advertisement on iOS

孤街浪徒 提交于 2021-01-27 07:33:23

问题


My app is running as a Bluetooth LE peripheral, and I'm trying to send just a few bytes of custom data in the Advertisement.

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)

}

But it looks like that data set in CBAdvertisementDataManufacturerDataKey is being stripped out and not sent out over the radio. I've read every little scrap I can find about this in Apple's documentation and online. Consensus appears to be that Core Bluetooth disregards the data as only CBAdvertisementDataLocalNameKey and CBAdvertisementDataServiceUUIDsKey are supported. The above compiles and runs fine, and I can "I wish this worked" in my BT scanner app, but my two bits of custom data don't appear to work.

Is there any way to circumvent this; any acceptable alternative to CoreBluetooth or any totally dumb thing that I'm missing?

Thanks,

Dan


回答1:


The problem may be that you are sending too many bytes.

The advertising packet data length is limited to 31 bytes by the spec. From your example, the string already has 18 bytes, while the UUID has 16 bytes, so there're already too many.




回答2:


The problem is that sending custom value for key CBAdvertisementDataManufacturerDataKey is not supported in CBPeripheralManager.

Upwards of iOS 12, you even get an error message in console when you set CBAdvertisementDataManufacturerDataKey and start advertising.



来源:https://stackoverflow.com/questions/28128790/sending-bluetooth-le-data-in-advertisement-on-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!