I'm trying to broadcast from an iOS 8.1+ device using Swift. When I run the app, it does successfully broadcast ... but only for a second. I know this because, from another device with a 'scanner' app, I see the beacon appear; the print statement for "power on" also appears as expected.
I have other print statements in peripheralManagerDidUpdateState, but they're never called, so I have no clue why broadcast stops so quickly.
I'm NOT trying to do anything fancy (monitor for regions, determine proximity, broadcast in the background, etc.) -- this is just a normal, run-of-the-mill iBeacon transmit from the foreground.
import CoreBluetooth
class ViewController: UIViewController, CBPeripheralManagerDelegate {
var beaconRegion = CLBeaconRegion()
var beaconData = NSDictionary()
var beaconManager = CBPeripheralManager()
...
Later:
self.beaconRegion = CLBeaconRegion(proximityUUID: bleUuid,
major: bleMajor,
minor: bleMinor,
identifier: "com.please.work")
Later, to initiate broadcast:
self.beaconData = self.beaconRegion.peripheralDataWithMeasuredPower(nil)
self.beaconManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)
The delegate:
func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager!) {
if(peripheral.state == CBPeripheralManagerState.PoweredOn) {
println("powered on")
self.beaconManager.startAdvertising(self.beaconData)
} else if(peripheral.state == CBPeripheralManagerState.PoweredOff) {
println("powered off")
self.beaconManager.stopAdvertising()
}
else {
println("something else changed")
}
}
UPDATE
This might be due to a problem with my device (iPhone 6); here are my observations:
Rebooting the device clears the issue.
I'm seeing phenomenon in normal apps. First, I start emitting with this: iBeacon Emitter app. Then (on another device), I register the UUID and monitor with this iBeacon Scanner app. The device appears, but after ~minute, it disappears. Thereafter, if I toggle the emitting device, I see the rapid on/off behavior I'm troubleshooting.
More concerning, the behavior occurs across apps. If I reboot (and clear the issue, see #1), and then cause the issue (see #2) ... the problem then appears via other emitter/scanner apps.