Search code examples
iosswiftibeaconestimote

Swift: How to configure ESTBeaconConnection for only one Estimote Beacon?


I'm trying to get a temperature from a beacon. I rewrote some code from example project comes with Estimote's iOS SDK 3.0.0, but I want to get this from only one beacon, so i tried to do it like this:

beaconConnection = ESTBeaconConnection(proximityUUID: NSUUID(UUIDString: "B9407F30-F5F8-466E-AFF9-25556B57FE6D"), major: 30132, minor: 50914, delegate: self)

In Xcode everything seems to be good, but when I try to run application, I got an error on line above:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '[<ESTBeaconConnection 0x17639470> setNilValueForKey]: could not set nil as the value for the key connectionStatus.'
*** First throw call stack:
(0x248495a7 0x32567c77 0x248494ed 0x25556fc1 0x254d97fd 0x254d96b5 0xdb9ed 0x642187 0x642e19 0xdb925 0x98ed4 0x96008 0x96238 0x27d4db65 0x27d4d8d5 0x27d53797 0x27d511ef 0x27dbb031 0x27fad34f 0x27faf781 0x27fba1a9 0x27fae063 0x2b11f0a1 0x2481025d 0x2480f521 0x2480e07b 0x2475ab51 0x2475a963 0x27db1b8f 0x27dacc91 0x9ce18 0x9ce54 0x32b10aaf)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Can anybody help? All code below:

import UIKit

class ViewController: UIViewController, ESTBeaconManagerDelegate, ESTBeaconConnectionDelegate {

    @IBOutlet weak var temperatureLabel: UILabel!
    @IBOutlet weak var activityLabel: UILabel!
    @IBOutlet weak var activityIndicator: UIActivityIndicatorView!

    var beaconConnection = ESTBeaconConnection()
    var readTemperatureWithInterval:NSTimer = NSTimer()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.activityIndicator.startAnimating()
        beaconConnection = ESTBeaconConnection(proximityUUID: NSUUID(UUIDString: "B9407F30-F5F8-466E-AFF9-25556B57FE6D"), major: 30132, minor: 50914, delegate: self)
        beaconConnection.startConnection()
    }

    override func viewDidDisappear(animated: Bool) {
        super.viewWillDisappear(animated)

        if (self.beaconConnection.connectionStatus == ESTConnectionStatus.Connected)
        {
            self.readTemperatureWithInterval.invalidate()
            self.beaconConnection.cancelConnection()
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func beaconConnectionDidSucceed(connection: ESTBeaconConnection!) {
        self.activityIndicator.stopAnimating()
        self.activityLabel.text = "Connected!"
        self.readTemperatureWithInterval = NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: "", userInfo: nil, repeats: true)
    }

    func beaconConnection(connection: ESTBeaconConnection!, didFailWithError error: NSError!) {
        self.activityLabel.text = "Connection failed";
        activityIndicator.stopAnimating()
        self.activityLabel.textColor = UIColor.redColor()
        let alert:UIAlertView = UIAlertView(title: "Connection error", message: error.localizedDescription, delegate: nil, cancelButtonTitle: "ok")
        alert.show()
    }

    func readBeaconTemperature() {
        self.beaconConnection.readTemperatureWithCompletion() { value, error in
            if (error != nil)
            {
                self.temperatureLabel.text = NSString(format: "%.1f°C", value.floatValue)
                self.activityIndicator.stopAnimating()
            }
            else
            {
                self.activityLabel.text = NSString(format: "Error:%@", error.localizedDescription)
                self.activityLabel.textColor = UIColor.redColor()
            }
        }
    }
}

Solution

  • This was a Swift-only bug in the Estimote SDK 3.0.0 and 3.0.1. It's fixed in 3.0.2, released yesterday.