NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SOLVED: Project 22 - blinking background color

Forums > 100 Days of Swift

In project 22, the background color is blinking between (any color) and grey if there is

else { update(distance: .unknown) }

In locationmanager(didrangebeacons) function.

If I remove the line it works alright.

import UIKit import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate { var hasShownNotification = false

@IBOutlet weak var uuuidIdentifier: UILabel!
var uuids = [String]()

@IBOutlet weak var distanceReading: UILabel!
var locationManager: CLLocationManager?

override func viewDidLoad() {
    super.viewDidLoad()

    locationManager = CLLocationManager()
    locationManager?.delegate = self
    locationManager?.requestAlwaysAuthorization()

    view.backgroundColor = .gray

}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    if status == .authorizedAlways {
        if CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self) {
            if CLLocationManager.isRangingAvailable() {
                startScanning()                }
        }
    }
}

func startScanning() {

    uuids = ["5A4BCFCE-174E-4BAC-A814-092E77F6B7E5", "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6", "E2C56DB5-DFFB-48D2-B060-D0F5A71096E0"]

    for (number,uuidString) in uuids.enumerated() {
        let uuid = UUID(uuidString: uuidString)!
        let beaconIdentity = CLBeaconIdentityConstraint(uuid: uuid, major: 123, minor: 456)
        let beaconRegion = CLBeaconRegion(beaconIdentityConstraint: beaconIdentity, identifier: "MyBeacon\(number)")
        locationManager?.startMonitoring(for: beaconRegion)
        locationManager?.startRangingBeacons(satisfying: beaconIdentity)
    }

}

func update(distance: CLProximity) {
    UIView.animate(withDuration: 1) {
        switch distance {
        case .far:
            self.view.backgroundColor = UIColor.blue
            self.distanceReading.text = "FAR"

        case .near:
            self.view.backgroundColor = UIColor.orange
            self.distanceReading.text = "NEAR"

        case .immediate:
            self.view.backgroundColor = UIColor.red
            self.distanceReading.text = "RIGHT HERE"
        case .unknown:
            self.view.backgroundColor = UIColor.gray
            self.distanceReading.text = "UNKNOWN"
            self.uuuidIdentifier.text = "UNKNOWN"

        @unknown default:
            self.view.backgroundColor = UIColor.red
            self.distanceReading.text = "UNKNOWN"
            self.uuuidIdentifier.text = "UNKNOWN"
        }
    }
}

func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
    print("called")
    if let beacon = beacons.first {
        if hasShownNotification == false {
            hasShownNotification = true
            let ac = UIAlertController(title: "Beacon detected", message: "Beacon has been detected", preferredStyle: .alert)
            ac.addAction(UIAlertAction(title: "OK", style: .default))
            present(ac, animated: true)
        }

        for uuid in uuids {
            if uuid == beacon.uuid.uuidString {
                self.uuuidIdentifier.text = uuid
                update(distance: beacon.proximity)

            }
        }
    }
    else {
        update(distance: .unknown)
    }

}

}

Is it possible that the function is called more often than the beacon is transmitting and that is the cause?

1      

Hi @nyannyanrabu

Is it because you have more then one beacon that it trying to find. In the tutorial they is one one uuid where you have a Array of uuids

2      

Thank you! I see it now!

1      

Hi @nyannyanrabu ,

How did you manage to solve the blinking issue? It's occuring as I attempt Challenge 2 for this project, scanning for multiple beacons. Which I implemented in a very similar way to you (used an array).

Any help would be very beneficial, thank you!

1      

Hacking with Swift is sponsored by MadMachine

SPONSORED Want to explore your Swift skill outside of the Apple world? Join the MadMachine community and start to program microcontrollers in Swift.

Get it now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.