ios - CoreBluetooth 广告错误

标签 ios bluetooth-lowenergy core-bluetooth ibeacon

我在尝试宣传某些服务数据时遇到一个奇怪的错误。

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)
    }
}

当通过开始广告时出现以下错误:

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 或删除字典的内容,则广告成功。

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)
    }
}

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

有人有什么想法吗?

感谢您的帮助。

最佳答案

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

如果你想传输一个 iBeacon 数据包,你可以使用 region.peripheralDataWithMeasuredPower(nil) 正如@heypiotr 在他的回答中所建议的那样。

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

关于ios - CoreBluetooth 广告错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33035999/

相关文章:

android - 在 android 8(oreo) 上使用 altbeacon 库,后台信标扫描不工作

ios - 更快地重新连接 CBPeripheral

ios - 设置 NSStreams 的正确方法?

ios - CoreBluetooth 可以报告外围设备的剩余电池生命周期吗?

iOS Swift Http 请求 post 方法使用 header x-www-form-urlencoded?

ios - 从 NSString 中删除 html 实体 - 适用于 iOS 模拟器和设备的解决方案

c# - Windows UWP 蓝牙 LE 设备重新连接

java - Ble 扫描设备 Android

ios - 为什么我已经设置了一个原型(prototype)单元但什么也没有出现?

ios - PhoneGap 将图像下载到特定文件夹