使用iPhone作为iBeacon发射机
我正在试图设置我的iPhone来广播另一个iPhone的iBeacon信号。 应该拿起信号的iPhone正在工作,但我不能让另一个iPhone来传播信号。
我正在学习本教程: https : //www.hackingwithswift.com/example-code/location/how-to-make-an-iphone-transmit-an-ibeacon
这是我的代码:
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() } } }
另一个说明,位置服务已经设置为始终授权。
“外围pipe理器”也从来没有被打印,所以在这个function是不是因为某种原因被调用。
谢谢!
固定。 我将代码移到了视图控制器中,并在viewDidLoad函数中调用了initLocalBeacon()。