         
     }
     
+    /// A really naive first attempt at adding a log abstraction for the 
+    /// information textbox
+    ///
+    /// :param: msg The message to log
     func addLogMsg(msg: String) {
         textLog.text = textLog.text + "\n" + msg
         var length = countElements(textLog.text);


import UIKit


class ViewController: UIViewController {
    @IBOutlet weak var textLog: UITextView!
    
    var counter = 0
    var discovery : AnyObject?
    var nc = NSNotificationCenter.defaultCenter()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        textLog.text = ""
        self.addLogMsg("Starting...");
        
        nc.addObserver(self, selector: Selector("bleStateChanged:"), name: "bleStateChangedNotification", object: nil)
        discovery = BTDiscovery()
    }

    override func didReceiveMemoryWarning() {

        textLog.scrollRangeToVisible(range);
    }
    
    /// Listens to notifications about CoreBluetooth state changes
    ///
    /// :param: notification The NSNotification object
    /// :returns: nil
    func btStateChanged(notification: NSNotification) {
        var msg = notification.object as String
        println("got notification: \(msg)")
        dispatch_async(dispatch_get_main_queue(), {
            self.addLogMsg(msg)
            self.statusLabel.text = "Scanning..."
        })
    }
    
    func btConnectionChanged(notification: NSNotification) {
        println("got connection changed notification: \(notification)")
        
        let userinfo = notification.userInfo as [String: Bool]
        let service  = notification.object as BTService
        let peripheral = service.peripheral
        
        if let isConnected: Bool = userinfo["isConnected"] {
            self.isConnected = isConnected
            
            dispatch_async(dispatch_get_main_queue(), {
            
                self.openButton.backgroundColor = UIColor(
                    red: 0.0, green: 0.6, blue: 0.8, alpha: 1.0
                )
                self.openButton.setTitle("Open", forState: UIControlState.Normal)
                self.statusLabel.text = "Connected"
            
                self.addLogMsg("Device connected")
            })
        
        }
    }
    
    func btFoundDevice(notification: NSNotification) {
        println("got found device notification: \(notification)")
        
        dispatch_async(dispatch_get_main_queue(), {
            self.openButton.backgroundColor = UIColor.orangeColor()
            self.statusLabel.text = "Found Device..."
            
            self.addLogMsg("Found correct device")
        })
        
    }
}

