Dan
Dan

Reputation: 4299

iBeacon broadcast starts, immediately stops

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:

  1. Rebooting the device clears the issue.

  2. 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.

  3. 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.

Upvotes: 0

Views: 483

Answers (2)

Dan
Dan

Reputation: 4299

I diagnosed this problem as some sort of low-level Bluetooth conflict with the "Knock to Unlock" app.

"Knock to Unlock" uses BLE for 2-way comms with a computer. The moment I uninstalled the app, the problem resolved itself. I hope this saves someone in the same case a little aggravation.

Upvotes: 2

davidgyoung
davidgyoung

Reputation: 64916

A few thoughts:

  1. Are you sure your CBPeripheralManager instance is not going out of scope or being overwritten and garbage collected, that your ViewController remains in the foreground and the screen is not locked?

  2. Try using another transmitter app like the free Locate app to verify it is not a device specific issue.

  3. If the Locate app works on the same device, then there must be something else in your app that is interfering with the broadcast at a later time. Perhaps you can share code on Github or elsewhere so others can test.

Upvotes: 0

Related Questions