     // MARK: CBCentralManagerDelegate
     func centralManagerDidUpdateState(central: CBCentralManager!) {
         println("central manager did update state")
+        if central.state == CBCentralManagerState.PoweredOn {
+            scan()
+        }
     }
     
     func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {
-        println(peripheral.name)
-        central.connectPeripheral(peripheral, options: nil)
-        
-        central.stopScan()
+        // Reject any where the value is above reasonable range
+        if RSSI.integerValue > -15 {
+            return
+        }
+        // Reject if the signal strength is too low to be close enough (Close is around -22dB)
+        if RSSI.integerValue < -35 {
+            return
+        }
+        
+        println("Discovered \(peripheral.name) at \(RSSI)")
+        
+        // OK, it's in range - have we already seen it?
+        if !(discoveredPeripheral?.isEqual(peripheral) != nil) {
+            // Save a local copy of the peripheral, so CoreBluetooth doesn't get rid of it
+            discoveredPeripheral = peripheral
+            
+            // Connect
+            central.connectPeripheral(peripheral, options: nil)
+            println("Connecting to peripheral \(peripheral)")
+        }
+    }
+    
+    func centralManager(central: CBCentralManager!, didFailToConnectPeripheral peripheral: CBPeripheral!, error: NSError!) {
+        println("Failed to connect to \(peripheral). (\(error.localizedDescription))")
+        cleaup()
     }
     
     func centralManager(central: CBCentralManager!, didConnectPeripheral peripheral: CBPeripheral!) {
         println("peripheral connected")
         
-        peripheral.delegate = self;
-        peripheral.discoverServices(nil)
+        // Stop scanning
+        centralManager.stopScan()
+        println("Scanning stopped")
+        
+        // Clear the data that we may already have
+        data?.length = 0
+        
+        // Make sure we get the discovery callbacks
+        peripheral.delegate = self
+        
+        // Search only for services that match our UUID
+        peripheral.discoverServices([uuid])
     }
     
     func centralManager(central: CBCentralManager!, willRestoreState dict: [NSObject : AnyObject]!) {


class BluetoothCentralController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {
    
    var centralManager: CBCentralManager!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        centralManager = CBCentralManager(delegate: self, queue: nil
            , options: [CBCentralManagerOptionRestoreIdentifierKey : "CentralManagerOptionRestoreIdentifier"])
        centralManager.scanForPeripheralsWithServices(nil, options: nil)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    /*

    
    // MARK: CBPeripheralDelegate
    func peripheral(peripheral: CBPeripheral!, didDiscoverServices error: NSError!) {
        if error != nil {
            println("Error discovering services: \(error.localizedDescription)")
            cleaup()
            return
        }
        
        // Discover the characteristic we want
        // Loop through the newly filled peripheral.services array, just in case there's more than one.
        for service in peripheral.services {
            println("Discovered service %@", service)
            
            peripheral.discoverCharacteristics([uuid], forService: service as CBService)
        }
    }
    
    func peripheral(peripheral: CBPeripheral!, didDiscoverCharacteristicsForService service: CBService!, error: NSError!) {
        // Deal with errors if any
        if error != nil {
            println("Error discovering characteristics: \(error.localizedDescription)")
            cleaup()
            return
        }
        
        // Again, we loop through the array, jast in case
        for characteristic in service.characteristics {
            println("Discovered characteristic %@", characteristic)
            // And check if it's the right one
            if characteristic.uuid.isEqual(uuid) {
                // If it is, subscribe to it
                peripheral.setNotifyValue(true, forCharacteristic: characteristic as CBCharacteristic)
            }
            
            // Once this is complete, we just need to wait for the data to come in.
            
//            peripheral.readValueForCharacteristic(characteristic as CBCharacteristic)
        }
    }
    
    func peripheral(peripheral: CBPeripheral!, didUpdateValueForCharacteristic characteristic: CBCharacteristic!, error: NSError!) {
        // Deal with errors if any
        if error != nil {
            println("Error updating characteristics: \(error.localizedDescription)")
            return
        }
        
        let data: NSData = characteristic.value
        
        // Cancel our subscription to the characteristic
        peripheral.setNotifyValue(false, forCharacteristic: characteristic)
        // Disconnect from the peripheral
        centralManager.cancelPeripheralConnection(peripheral)
        
//        peripheral.writeValue(data, forCharacteristic: characteristic, type: CBCharacteristicWriteType.WithResponse)
    }
    
    func peripheral(peripheral: CBPeripheral!, didUpdateNotificationStateForCharacteristic characteristic: CBCharacteristic!, error: NSError!) {
