I am developing an App using BLE to connect to a Microcontroller. I currently have all that up and running, also to read the RSSI from the BLE link. I am able to display the RSSI fine, to determine the signal strength. But I would like design the label to change colors depending on the signal strength. 0 to -30 (green), -30 to -60(yellow), and -60 to -90(red). I am fairly new to swift. But I have made a Range test view controller.
Obviously my code will just update the color one time. There might be something very simple I'm missing, I just can't quite figure it out. Also, probably a while loop, or for-in would solve this. But so far I haven't been able to get it right.
Anyway if anyone has tips on what route I should take, that would be greatly appreciated!
Here is the code:
@IBOutlet weak var RSSILabel: UILabel!
@IBOutlet weak var deviceLabel: UILabel!
var timer = Timer()
var progressHUD: MBProgressHUD?
func serialDidChangeState() {
}
func serialDidDisconnect(_ peripheral: CBPeripheral, error: NSError?)
{
}
override func viewDidLoad() {
super.viewDidLoad()
if (serial?.isReady)! {
deviceLabel.text = serial?.connectedPeripheral!.name
serial?.readRSSI()
readRssiTimer()
showRssiInLabel()
}
}
func readRssiTimer() {
self.timer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true, block: { (Timer) in
serial?.readRSSI()})
}
func showRssiInLabel(){
self.timer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true, block: { (Timer) in
self.RSSILabel.text = serial?.realtimeRSSI.stringValue})
if (serial?.self.realtimeRSSI.intValue)! > -40 {
RSSILabel.textColor = UIColor.green
RSSILabel.font = UIFont(name: "HelveticaNeue", size: CGFloat(22))
self.view.addSubview(RSSILabel)
} else if (serial?.realtimeRSSI.intValue)! <= -40 && (serial?.realtimeRSSI.intValue)! > -70 {
RSSILabel.textColor = UIColor.yellow
RSSILabel.font = UIFont(name: "HelveticaNeue", size: CGFloat(22))
self.view.addSubview(RSSILabel)
} else if (serial?.realtimeRSSI.intValue)! <= -70 && (serial?.realtimeRSSI.intValue)! > -90 {
RSSILabel.textColor = UIColor.red
RSSILabel.font = UIFont(name: "HelveticaNeue", size: CGFloat(22))
self.view.addSubview(RSSILabel)
}
}
Since you have your UILabel in the Storyboard defined as well in your ViewController as @IBOutlet weak var RSSILabel: UILabel!
means you didn't created RSSILabel programmatically. That means you don't need to do self.view.addSubview(RSSILabel)
. Also you can avoid setting the font family each time for every if case as they all have the same. And probably can avoid having serial?
which at some point might be nil, so better to check it since you declare it optional and then play with it by saying to the compiler you know it's not nil with serial!
. Also changes to UI should happen in the main thread and might be that you are not in the main thread while changing a UILabel color, so I putted there the DispatchQueue.main.async{}
block
This should work if you are debugging check and the color should be able to change anyway:
func showRssiInLabel(){
if serial != nil {
self.timer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true, block: { (Timer) in
DispatchQueue.main.async {
self.RSSILabel.text = serial!.realtimeRSSI.stringValue
}
})
var labelColor: UIColor = UIColor.green
if (serial!.self.realtimeRSSI.intValue)! > -40 {
labelColor = UIColor.green
} else if (serial!.realtimeRSSI.intValue)! <= -40 && (serial!.realtimeRSSI.intValue)! > -70 {
labelColor = UIColor.yellow
} else if (serial!.realtimeRSSI.intValue)! <= -70 && (serial!.realtimeRSSI.intValue)! > -90 {
labelColor = UIColor.red
}
DispatchQueue.main.async {
RSSILabel.font = UIFont(name: "HelveticaNeue", size: CGFloat(22))
RSSILabel.textColor = labelColor
}
}
}