我正在尝试制作一个使用PeripheralManager将UUID广播到中央的iOS应用。
我认为我的permissions和Capabilities是有序的, 但是当我用中央搜索它时,我仍然无法找到我的灯塔。
有什么我忘记了吗?
import UIKit
import CoreBluetooth
import CoreLocation
class ViewController: UIViewController, CBPeripheralManagerDelegate {
var localBeacon: CLBeaconRegion!
var beaconPeripheralData: NSDictionary!
var peripheralManager: CBPeripheralManager!
override func viewDidLoad() {
super.viewDidLoad()
initLocalBeacon()
}
func initLocalBeacon() {
if localBeacon != nil {
stopLocalBeacon()
}
let localBeaconUUID = "e276c0c0-d740-52e8-b778-9ae6e514269e"
let localBeaconMajor: CLBeaconMajorValue = 1
let localBeaconMinor: CLBeaconMinorValue = 1
let uuid = UUID(uuidString: localBeaconUUID)!
localBeacon = CLBeaconRegion(proximityUUID: uuid, major: localBeaconMajor, minor: localBeaconMinor, identifier: "Kristers Beacon")
beaconPeripheralData = localBeacon.peripheralData(withMeasuredPower: -45)
peripheralManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)
}
func stopLocalBeacon() {
peripheralManager.stopAdvertising()
peripheralManager = nil
beaconPeripheralData = nil
localBeacon = nil
}
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
if peripheral.state == .poweredOn {
peripheralManager.startAdvertising(beaconPeripheralData as! [String: AnyObject]!)
} else if peripheral.state == .poweredOff {
stopLocalBeacon()
}
}
}