2

I'm trying to set up my iPhone to broadcast an iBeacon signal for another iPhone to pick up. The iPhone that's supposed to pick up the signal is working, but I can't get the other iPhone to broadcast the signal.

I'm following this tutorial: https://www.hackingwithswift.com/example-code/location/how-to-make-an-iphone-transmit-an-ibeacon

And here's my code:

import CoreBluetooth
import CoreLocation

class iBeaconTransmission: NSObject, CBPeripheralManagerDelegate {

    var localBeacon: CLBeaconRegion!
    var beaconPeripheralData: NSDictionary!
    var peripheralManager: CBPeripheralManager!

    func initLocalBeacon() {
        if localBeacon != nil {
            stopLocalBeacon()
        }

        let localBeaconUUID = "placeholder"
        let localBeaconMajor: CLBeaconMajorValue = 123
        let localBeaconMinor: CLBeaconMinorValue = 456

        let uuid = NSUUID(UUIDString: localBeaconUUID)!
        localBeacon = CLBeaconRegion(proximityUUID: uuid, major: localBeaconMajor, minor: localBeaconMinor, identifier: "placeholder")

        self.beaconPeripheralData = self.localBeacon.peripheralDataWithMeasuredPower(nil)
        self.peripheralManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)
    }

    func stopLocalBeacon() {
        peripheralManager.stopAdvertising()
        peripheralManager = nil
        beaconPeripheralData = nil
        localBeacon = nil
    }

    func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager) {
        print("Peripheral Manager")
        if peripheral.state == .PoweredOn {
            print("Transmitting")
            peripheralManager.startAdvertising(beaconPeripheralData as! [String: AnyObject]!)
        } else if peripheral.state == .PoweredOff {
            peripheralManager.stopAdvertising()
        }
    }
}

Another note, location services is already set to always authorized.

"Peripheral Manager" is also never being printed, so the function that's in isn't being called for some reason.

Thanks!

Nick
  • 155
  • 1
  • 2
  • 14
  • Are you actually using `placeholder` as the uuid? And what debugging have you done. – Wain Feb 28 '16 at 09:41
  • No I'm using an actual UUID. I put placeholder in for the purposes of posting to stack overflow. I've place breakpoints at the beginning of each function and stepped through, and the only thing I noticed is func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager) is not being called. I'm not sure why though. – Nick Feb 28 '16 at 09:54
  • Have you requested location authorisation? – Wain Feb 28 '16 at 10:01
  • Yes. I've aloud it to always track my location on my device. – Nick Feb 28 '16 at 10:04
  • Call start advertising immediately, it shouldn't work but the error may offer some illumination – Wain Feb 28 '16 at 10:20
  • Here's what I got: fatal error: unexpectedly found nil while unwrapping an Optional value – Nick Feb 28 '16 at 11:08
  • For some reason moving the code into a view controller worked. – Nick Feb 28 '16 at 12:20

1 Answers1

0

Fixed. I moved the code into a view controller and called initLocalBeacon() in the viewDidLoad function.

Nick
  • 155
  • 1
  • 2
  • 14