 
 import Foundation
 import CoreBluetooth
+import XCGLogger
+import Async
 
 // MARK: -
 // MARK: EPLCentralManagerDelegate Protocol
 @objc public protocol EPLCentralManagerDelegate {
     // required functions
     func afterBLEIsReady(central: EPLCentralManager!)
-    func afterScanIsDone(central: EPLCentralManager!)
+    func didDiscoverPeripherals(central: EPLCentralManager!)
     func didConnectedPeripheral(peripheral: EPLPeripheral!)
     
     // optional functions
+    optional func afterScanTimeout(central: EPLCentralManager!)
 }
 
 // MARK: -

    
    // MARK: -
    // MARK: Internal Interface
    @objc func scanTimeout() {
        println("timer stop")
        //        self.cbCentralManager.stopScan()
        delegate?.afterScanIsDone(self)
    }
    
    // MARK: -
    // MARK: Public Interface
    public override init() {

    
    
    public func scan(timeout: Double = 10.0, allowDuplicated: Bool = false) {
        self.log.debug("Start Scanning for peripherals")
        self.scanOption[CBCentralManagerScanOptionAllowDuplicatesKey] = allowDuplicated
        self.scanOption[CBCentralManagerScanOptionSolicitedServiceUUIDsKey] = []
        self.cbCentralManager!.scanForPeripheralsWithServices(nil, options: self.scanOption)

        // insert a background delay then rise a timeout event
        Async.main(after: timeout) {
            self.log.debug("Scan Timeout")
            self.stopScan()
            self.delegate?.afterScanTimeout!(self)
        }
    }
    
    public func stopScan() {
        log.debug("Stop scanning")
        self.cbCentralManager!.stopScan()
    }

    public func connect(peripheral: EPLPeripheral, options: [NSObject: AnyObject]! = nil) {
        var msg = "Connect to " + peripheral.name
        log.debug(msg)
        self.cbCentralManager?.connectPeripheral(peripheral.cbPeripheral, options: options)
    }
    
    // MARK: -
