         return "Hello from BTDiscovery.";
     }
     
+    
+    func appWillResignActive() {
+        println("Discovery Manager: will resign active")
+        
+        if (self.activePeripheral == nil) {
+            println("  Got nil activePeripheral. Not connected.")
+            return
+        }
+        
+        if let state = self.activePeripheral?.state {
+            if (state == CBPeripheralState.Connected) {
+                let p = self.activePeripheral
+                self.resetConnection()
+                self.centralManager?.cancelPeripheralConnection(p)
+            }
+        }
+        else {
+            println("  Did not get state or state is not connected.")
+        }
+    }
+    
+    
+    func appDidBecomeActive() {
+        println("Discovery Manager: did become active")
+        
+        if self.centralManager?.state == CBCentralManagerState.PoweredOn {
+            self.startScanning()
+        }
+        else {
+            println("  App became active but Bluetooth not on.")
+        }
+    }
     
     
     func delay(delay:Double, closure:()->()) {

        case CBCentralManagerState.PoweredOn:
            println("BLE state Powered On")
            state = nil
            self.startScanning()
            break
        case CBCentralManagerState.Unknown:
            println("BLE state unknown")

        central.stopScan()
    }
    
    
    func centralManager(central: CBCentralManager!, didDisconnectPeripheral peripheral: CBPeripheral!, error: NSError!) {
        println("Called did Disconnect Peripheral: \(peripheral)")
        
        nc.postNotificationName("btStateChangedNotification", object: "Disconnected", userInfo: ["peripheral": peripheral])
        
        // when activePeripheral is set to nil we know we don't need to reconnect.
        if (peripheral == self.activePeripheral) {
            self.resetConnection()
            self.startScanning()
        }
    }
    
    
    func centralManager(
        central: CBCentralManager!,
        didDiscoverPeripheral peripheral: CBPeripheral!,
        advertisementData: [NSObject : AnyObject]!,
        RSSI: NSNumber!) {
        
        let uuid = peripheral.identifier.UUIDString;
        
