Sending bluetooth LE data in advertisement on iOS
Asked Answered
D

2

6

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

Dandiprat answered 24/1, 2015 at 18:27 Comment(2)
Can you send an iBeacon formatted advertisement? You get four bytes of data in the major/minor fields.Suited
Unless something changed in iOS 8, you can't modify manufacturer data: https://mcmap.net/q/1915522/-changing-cbadvertisementdatamanufacturerdatakey-from-peripheralSwafford
T
2

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.

Transact answered 30/3, 2015 at 6:43 Comment(0)
C
1

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.

Cuenca answered 12/5, 2020 at 13:9 Comment(1)
@eonist how to use the CBAdvertisementDataManufacturerDataKey data? I can't find the reference about changing the CBAdvertisementDataManufacturerDataKey value.Manaus

© 2022 - 2024 — McMap. All rights reserved.