     
 
     func startScanning() {
-        nc.postNotificationName("btStateChangedNotification", object: "Scanning")
-        
         if let central = self.centralManager {
-            central.scanForPeripheralsWithServices(
-                [CBUUID(string: btConst.SERVICE_UUID)],
-                options: nil
-            )
+            if central.state == CBCentralManagerState.PoweredOff {
+                self.resetConnection()
+                nc.postNotificationName(
+                    "btStateChangedNotification",
+                    object: "Bluetooth Off"
+                )
+                return
+            }
+            
+            if central.state == CBCentralManagerState.PoweredOn {
+                nc.postNotificationName(
+                    "btStateChangedNotification",
+                    object: "Scanning"
+                )
+        
+                central.scanForPeripheralsWithServices(
+                    [CBUUID(string: btConst.SERVICE_UUID)],
+                    options: nil
+                )
+            
+                NSLog("BTDiscovery: setting timer to stop scan:")
+                dispatch_async(dispatch_get_main_queue(), {
+                    self.scanTimeout = NSTimer.scheduledTimerWithTimeInterval(
+                        30.0,
+                        target: self,
+                        selector: "handleCheckAndStopScan",
+                        userInfo: nil,
+                        repeats: false
+                    )
+                })
+                
+                return
+            }
+            
+            NSLog("BTDiscovery: Got other state than expected: \(central.state)")
         }
     }
     
     func resetConnection() {
         self.activeService = nil
         self.activePeripheral = nil
+    }
+    
+    
+    /// handler for NSTimer to stop the scanner after a set interval.
+    func handleCheckAndStopScan() {
+        NSLog("BTDiscovery: Told to check and stop scan.")
+        if let centr = self.centralManager {
+            if centr.state != CBCentralManagerState.PoweredOn {
+                self.resetConnection()
+                return
+            }
+            
+            centr.stopScan()
+            nc.postNotificationName(
+                "BTDiscoveryScanningTimedOutNotification",
+                object: self,
+                userInfo: nil
+            )
+        }
+        
     }
     
     

    func appWillEnterForeground() {
        println("Discovery Manager: will enter foreground")
        
        if self.centralManager?.state == CBCentralManagerState.PoweredOn {
            self.startScanning()
        }
        else {
            println("  App became active but Bluetooth not on.")
        }
    }
    

        
        var msg = "Low Signal: \(signal)"
        nc.postNotificationName("btStateChangedNotification", object: msg)
        if let central = self.centralManager {
            central.stopScan()
            self.resetConnection()
        }
        
        delay(1.0) {
            // Wait one second to start scanning again.
