 
 class BluetoothCentralController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {
     
+    let uuid:NSUUID = NSUUID(UUIDString: "E2C56DB5-DFFB-48D2-B060-D0F5A71096E0")!
     var centralManager: CBCentralManager!
+    var discoveredPeripheral: CBPeripheral?
+    var data: NSMutableData?
     
     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 viewWillDisappear(animated: Bool) {
+        centralManager.stopScan()
+        println("Scanning stopped")
+        
+        super.viewWillDisappear(animated)
     }
     
     override func didReceiveMemoryWarning() {
         super.didReceiveMemoryWarning()
         // Dispose of any resources that can be recreated.
+    }
+    
+    private func scan() {
+        centralManager.scanForPeripheralsWithServices([uuid], options: [CBCentralManagerScanOptionAllowDuplicatesKey : NSNumber(bool: true)])
+        println("Scanning started")
+    }
+    
+    private func cleaup() {
+        if let peripheral = discoveredPeripheral {
+            // Don't do anything if we're not connected
+            if peripheral.state == CBPeripheralState.Disconnected {
+                return
+            }
+            
+            // See if we war subscribed to a characteristic on the peripheral
+            if !peripheral.services.isEmpty {
+                for service: CBService in peripheral.services as [CBService] {
+                    if !service.characteristics.isEmpty {
+                        for characteristic: CBCharacteristic in service.characteristics as [CBCharacteristic] {
+                            if characteristic.UUID.isEqual(uuid) {
+                                if characteristic.isNotifying {
+                                    // It is notifying, so unsubscribe
+                                    peripheral.setNotifyValue(false, forCharacteristic: characteristic)
+                                    
+                                    // And we're done
+                                    return
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+            
+            // If we've got this far, we're connected, but we're not subscribed, so we just disconnect
+            centralManager.cancelPeripheralConnection(peripheral)
+        }
     }
     
     /*

    // MARK: CBCentralManagerDelegate
    func centralManagerDidUpdateState(central: CBCentralManager!) {
        println("central manager did update state")
    }
    
    func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {
        println(peripheral.name)
        central.connectPeripheral(peripheral, options: nil)
        
        central.stopScan()
    }
    
    func centralManager(central: CBCentralManager!, didConnectPeripheral peripheral: CBPeripheral!) {
        println("peripheral connected")
        
        peripheral.delegate = self;
        peripheral.discoverServices(nil)
    }
    
    func centralManager(central: CBCentralManager!, willRestoreState dict: [NSObject : AnyObject]!) {

    
    // 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!) {
