iOS设备作为iBeacon,为什么它不起作用?

时间:2018-04-03 11:41:28

标签: ios swift bluetooth-lowenergy cbperipheral cbperipheralmanager

我正在尝试制作一个使用PeripheralManager将UUID广播到中央的iOS应用。

我认为我的permissionsCapabilities是有序的, 但是当我用中央搜索它时,我仍然无法找到我的灯塔。

有什么我忘记了吗?

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()
    }
}


}

1 个答案:

答案 0 :(得分:0)

问题是蓝牙扫描仪,它没有搜索特定的Proximity UUID。 定位信标应用非常棒,感谢paul