如何通过iBeacon根据Firebase用户彼此之间的距离对他们进行排名?

时间:2019-07-01 16:46:28

标签: ios swift firebase ibeacon

我在firebase中有用户。我想根据它们通过手机发出的iBeacon信号彼此之间的距离来对它们进行排名。最好的显示方式是从上到下,从蓝色(接近)到红色(远)。

class homepage: UITableViewController, CLLocationManagerDelegate, CBPeripheralManagerDelegate{
    func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
        var localBeacon: CLBeaconRegion!
        var beaconPeripheralData: NSDictionary!
        var peripheralManager: CBPeripheralManager!

        let localBeaconUUID = "5A4BCFCE-174E-4BAC-A814-092E77F6B7E5"
        let localBeaconMajor: CLBeaconMajorValue = 123
        let localBeaconMinor: CLBeaconMinorValue = 456
        let uuid = UUID(uuidString: localBeaconUUID)!
        localBeacon = CLBeaconRegion(proximityUUID: uuid, major:
            localBeaconMajor, minor: localBeaconMinor, identifier: "Your private identifer here")
        beaconPeripheralData = localBeacon.peripheralData(withMeasuredPower: nil)
        peripheralManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)
    }
    func initLocalBeacon() {
        if localBeacon != nil {
            stopLocalBeacon()
        }


        func stopLocalBeacon() {
            peripheralManager.stopAdvertising()
            peripheralManager = nil
            beaconPeripheralData = nil
            localBeacon = nil
        }
        func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
            if peripheral.state == .poweredOn {
                peripheralManager.startAdvertising(beaconPeripheralData as? [String: Any])
            } else if peripheral.state == .poweredOff {
                peripheralManager.stopAdvertising()
            }
        }

        super.viewDidLoad()
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.requestAlwaysAuthorization()
        self.locationManager.requestWhenInUseAuthorization()
        if CLLocationManager.locationServicesEnabled(){
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
            locationManager.startUpdatingLocation()
        }
    }
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if status == .authorizedAlways {
            if CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self)             {
                if CLLocationManager.isRangingAvailable() {
                    startScanning()
                }
            }
        }
    }
    func startScanning() {
        let uuid = UUID(uuidString: "5A4BCFCE-174E-4BAC-A814-092E77F6B7E5")!
        let beaconRegion = CLBeaconRegion(proximityUUID: uuid, major: 123, minor: 456, identifier: "MyBeacon")
        locationManager.startMonitoring(for: beaconRegion)
        locationManager.startRangingBeacons(in: beaconRegion)
    }
    func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
        if beacons.count > 0 {
            updateDistance(beacons[0].proximity)
        }     else     {
            updateDistance(.unknown)
        }
    }
    func updateDistance(_ distance: CLProximity) {
        UIView.animate(withDuration: 0.8) {
            switch distance {
            case .unknown:
                self.view.backgroundColor = UIColor.gray
            case .far:
                self.view.backgroundColor = UIColor.blue
            case .near:
                self.view.backgroundColor = UIColor.orange
            case .immediate:
                self.view.backgroundColor = UIColor.red
            }
        }
    }

我有一些IBeacon代码(让iPhone发出信号并接收),但是说实话,我必须仔细阅读它才能更好地理解它。我最关心的是将iBeacon代码链接到Firebase用户。我不知道该如何编码,而且在搜索中也没有看到它。

最后,我希望根据当前用户随机显示的Firebase数据,根据每个用户与看着屏幕的用户之间的距离来排序。

0 个答案:

没有答案