I am working on a Bluetooth Low Energy (BLE) application for iOS and I need to replicate the advertising behavior from an existing Android application.
In the Android (Kotlin) version, the advertising data is configured like this:
val confirmationAdvertiseData = AdvertiseData.Builder()
.setIncludeDeviceName(false)
.addManufacturerData(MANUFACTURER_ID, payload)
.build()
This code creates an advertisement packet that does not include the device’s name but does include custom manufacturer-specific data. MANUFACTURER_ID is a 16-bit identifier assigned by the Bluetooth SIG, and payload is a byte array of custom data.
I’m trying to achieve the same in Swift using the CoreBluetooth framework. I’ve looked into CBPeripheralManager and the startAdvertising method, which takes a dictionary of advertisement data.
I believe the key I should be using is CBAdvertisementDataManufacturerDataKey, but I’m not sure how to structure the data to include my MANUFACTURER_ID and the payload correctly.
How can I construct the advertisement data dictionary in Swift to replicate the functionality of the Kotlin code above, specifically setting the manufacturer-specific data?
I have tried
let advertisementData: [String: Any] = [
CBAdvertisementDataManufacturerDataKey: manufacturerData,
CBAdvertisementDataLocalNameKey: payload
]
peripheralManager.startAdvertising(advertisementData)
But it gives me a warning that the dictionary keys doesn’t exist and i think it drops the data and doesn’t advertise anything. I have checked stack overflow and found a post that said IOS doesn’t permit it, but it was almost a decade old. I just want to send a custom byte array through bluetooth low energy using an iphone.
Any help would be greatly appreciated!