简体   繁体   中英

CoreBluetooth advertisement error

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]: 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

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.

Anyone got any ideas?

Thanks for your help.

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

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

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.

If you want to advertise as an iBeacon, it's easier and less error prone to utilize the CLBeaconRegion 's peripheralDataWithMeasuredPower method, which will automatically generate appropriate advertisement data for you. Example code snippet below:

let region = CLBeaconRegion(proximityUUID: beacon.proximityUUID,
                            major: beacon.major, minor: beacon.minor,
                            identifier: "iOS beacon")
let advertisementData = region.peripheralDataWithMeasuredPower(nil)
peripheralManger?.startAdvertising(advertisementData)

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