在iOS上的广告中发送蓝牙LE数据 [英] Sending bluetooth LE data in advertisement on iOS
问题描述
我的应用程序正在作为Bluetooth LE外围设备运行,并且我试图在广告"中仅发送几个字节的自定义数据.
func btStartBroadcasting(外围设备:CBPeripheralManager!){//创建要发送的字节数组var byteArray = [UInt8]()byteArray.append(0b11011110);//'DE'byteArray.append(0b10101101);//'广告'//将该数组转换为NSData对象var ManufacturerData = NSData(bytes:byteArray,length:byteArray.count)//为服务定义一个UIUD让theUUid = CBUUID(NSUUID:uuid)//构建数据包让dataToBeAdvertised:[String:AnyObject!] = [CBAdvertisementDataLocalNameKey:我希望这个工作",CBAdvertisementDataManufacturerDataKey:制造商数据,CBAdvertisementDataServiceUUIDsKey:[theUUid],]Peripheral.startAdvertising(dataToBeAdvertised)}
但是,看起来CBAdvertisementDataManufacturerDataKey中设置的数据已被剥离,而不是通过无线电发送出去.我已经阅读了所有可以在Apple文档和在线文档中找到的小片段.共识似乎是Core Bluetooth不理会数据,因为仅支持CBAdvertisementDataLocalNameKey和CBAdvertisementDataServiceUUIDsKey.上面的代码可以编译并正常运行,我可以在我的BT扫描器应用程序中希望它能正常工作",但是我的两个自定义数据似乎不起作用.
有什么办法可以避免这种情况;可以替代CoreBluetooth的任何替代选择,或者我缺少的任何完全愚蠢的东西?
谢谢
丹
问题可能是您发送的字节过多.
广告数据包数据长度被规范限制为31个字节.在您的示例中,字符串已经有18个字节,而UUID有16个字节,因此已经太多了.
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.
这篇关于在iOS上的广告中发送蓝牙LE数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!