Swift:如何仅为一个Estimote Beacon配置ESTBeaconConnection?

时间:2015-03-21 20:10:40

标签: ios swift ibeacon estimote

我试图从灯塔获得温度。我重写了Estimote的iOS SDK 3.0.0附带的示例项目中的一些代码,但我想从一个灯塔中得到这个,所以我试着这样做:

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

在Xcode中,一切似乎都很好,但是当我尝试运行应用程序时,我在上面遇到了错误:

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) 

有人可以帮忙吗? 以下所有代码:

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

1 个答案:

答案 0 :(得分:2)

这是Estimote SDK 3.0.0和3.0.1中仅限Swift的错误。它已在昨天发布的3.0.2中修复。