简体   繁体   中英

Sending bluetooth LE data in advertisement on iOS

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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