CoreBluetooth广告错误 [英] CoreBluetooth advertisement error

查看:150
本文介绍了CoreBluetooth广告错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试播发一些服务数据时出现一个奇怪的错误.

I have a strange error when attempting to advertise some service data.

func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion) {
    if let beacon = beacons.first, username = NSUserDefaults.standardUserDefaults().objectForKey("username") {
        var major = beacon.major.integerValue
        var minor = beacon.minor.integerValue

        let advertismentData:[String:AnyObject] = [
            CBAdvertisementDataServiceUUIDsKey:[AppDelegate.MajorUUID, AppDelegate.MinorUUID, AppDelegate.IdentifierUUID],
            CBAdvertisementDataServiceDataKey:[AppDelegate.MajorUUID: NSData(bytes: &major, length: sizeof(Int)), AppDelegate.MinorUUID: NSData(bytes: &minor, length: sizeof(Int)), AppDelegate.IdentifierUUID: username.dataUsingEncoding(NSUTF8StringEncoding)]
        ]
        peripheralManger?.startAdvertising(advertismentData)
        manager.stopRangingBeaconsInRegion(region)
    }
}

通过传递开始广告时,会发生以下错误:

When passed to start advertising the following error occurs:

2015-10-09 11:17:05.563 BeConvo [280:21134]-[CBUUID UTF8String]: 无法识别的选择器已发送到实例0x13dd5b420 2015-10-09 11:17:05.564 BeConvo [280:21134] *由于未捕获而终止应用程序 异常"NSInvalidArgumentException",原因:-[CBUUID UTF8String]: 无法识别的选择器已发送到实例0x13dd5b420' * 首次引发调用堆栈:(0x185364f5c 0x199f5bf80 0x18536bc6c 0x185368c14 0x18526cdcc 0x184fe56b4 0x185361f00 0x184fe4634 0x184fe58a8 0x184fe56d8 0x185361f00 0x184fe4634 0x184fe44dc 0x184fe4704 0x184fe47e8 0x184fe6104 0x184fe7190 0x1000eb9a4 0x1000ebae0 0x185af5f04 0x185af0b14 0x185aeaef4 0x18531c48c 0x18531bdc4 0x18531a20c 0x185248dc0 0x19039c088 0x18a922f44 0x1000ec328 0x19a7868b8)libc ++ abi.dylib:未捕获而终止 NSException类型的异常

2015-10-09 11:17:05.563 BeConvo[280:21134] -[CBUUID UTF8String]: unrecognized selector sent to instance 0x13dd5b420 2015-10-09 11:17:05.564 BeConvo[280:21134] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CBUUID UTF8String]: unrecognized selector sent to instance 0x13dd5b420' * First throw call stack: (0x185364f5c 0x199f5bf80 0x18536bc6c 0x185368c14 0x18526cdcc 0x184fe56b4 0x185361f00 0x184fe4634 0x184fe58a8 0x184fe56d8 0x185361f00 0x184fe4634 0x184fe44dc 0x184fe4704 0x184fe47e8 0x184fe6104 0x184fe7190 0x1000eb9a4 0x1000ebae0 0x185af5f04 0x185af0b14 0x185aeaef4 0x18531c48c 0x18531bdc4 0x18531a20c 0x185248dc0 0x19039c088 0x18a922f44 0x1000ec328 0x19a7868b8) libc++abi.dylib: terminating with uncaught exception of type NSException

如果我删除ServiceDataKey或删除字典的内容,则广告成功.

If I remove the ServiceDataKey or remove the contents of the dictionary then the advertisement succeeds.

func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion) {
    if let beacon = beacons.first, username = NSUserDefaults.standardUserDefaults().objectForKey("username") {
        var major = beacon.major.integerValue
        var minor = beacon.minor.integerValue

        let advertismentData:[String:AnyObject] = [
            CBAdvertisementDataServiceUUIDsKey:[AppDelegate.MajorUUID, AppDelegate.MinorUUID, AppDelegate.IdentifierUUID],
            CBAdvertisementDataServiceDataKey:[]
        ]
        peripheralManger?.startAdvertising(advertismentData)
        manager.stopRangingBeaconsInRegion(region)
    }
}

我已经尝试了每个键/值对是否是特定的,但是它们中的任何一个组合都会导致错误发生.

I have tried each one of the key/value pairs to see if it a specific one but any of them in any combination causes the error to occur.

有人有什么主意吗?

感谢您的帮助.

推荐答案

不幸的是,CBAdvertisementDataServiceDataKey在iOS上是只读的.发现外围设备时可以使用它来读取服务数据,但是不能使用CBPeriperalManager来使用它来传输服务数据.请参阅此处以获取更多信息: https://stackoverflow.com/a/31856456/1461050

Unfortunately, the CBAdvertisementDataServiceDataKey is read only on iOS. While you can use it to read service data when discovering peripherals, you can not use it to transmit service data using a CBPeriperalManager. See here for more info: https://stackoverflow.com/a/31856456/1461050

如果您希望发送iBeacon数据包,则可以使用region.peripheralDataWithMeasuredPower(nil),如@heypiotr在其答案中建议的那样.

If you wish to transmit an iBeacon packet, you can use region.peripheralDataWithMeasuredPower(nil) as @heypiotr suggests in his answer.

为便于理解,请注意iBeacon广告是制造商的蓝牙广告,而不是服务广告.因此,即使显示的技术行之有效,也没有理由如示例中所示填充CBAdvertisementDataServiceUUIDsKey.发送服务广告不会触发iBeacon接收器.

For the sake of understanding, note that iBeacon advertisements are manufacturer Bluetooth advertisements, not service advertisements. So even if the technique shown did work, there would be no reason to populate CBAdvertisementDataServiceUUIDsKey as shown in the example. Sending a service advertisement would not trigger iBeacon receivers.

这篇关于CoreBluetooth广告错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆