2016-02-28 53 views
2

我试图设置我的iPhone来广播另一个iPhone的iBeacon信号来接收。应该接收信号的iPhone正在工作,但我无法让其他iPhone播放信号。使用iPhone作为iBeacon发射器

我下面这个教程: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() 
     } 
    } 
} 

另要注意,位置服务已被设置为始终授权。

“外围管理器”也永远不会被打印,因此,由于某种原因,所调用的功能未被调用。

谢谢!

+0

你真的使用'placeholder'作为uuid吗?你做了什么调试。 – Wain

+0

不,我正在使用实际的UUID。为了发布到堆栈溢出,我放置了占位符。我已经在每个函数的开始处放置了断点,并且通过了,我注意到的唯一情况是func peripheralManagerDidUpdateState(peripheral:CBPeripheralManager)没有被调用。我不知道为什么。 – Nick

+0

您是否要求位置授权? – Wain

回答

0

固定。我将代码移入视图控制器,并在viewDidLoad函数中调用initLocalBeacon()。