     }
     
     /// Try to connect to the given peripheral
-    func connectToPeripheral(peripheral: CBPeripheral) {
+    func connectToPeripheral(_ peripheral: CBPeripheral) {
         pendingPeripheral = peripheral
-        centralManager.connectPeripheral(peripheral, options: nil)
+        centralManager.connect(peripheral, options: nil)
     }
     
     /// Disconnect from the connected peripheral or stop connecting to it

    // ** Required **
    
    /// Called when de state of the CBCentralManager changes (e.g. when bluetooth is turned on/off)
    func serialDidChangeState(newState: CBCentralManagerState)
    
    /// Called when a peripheral disconnected
    func serialDidDisconnect(peripheral: CBPeripheral, error: NSError?)
    
    // ** Optionals **
    
    /// Called when a message is received
    func serialDidReceiveString(message: String)
    
    /// Called when a message is received
    func serialDidReceiveBytes(bytes: [UInt8])
    
    /// Called when a message is received
    func serialDidReceiveData(data: NSData)
    
    /// Called when the RSSI of the connected peripheral is read
    func serialDidReadRSSI(rssi: NSNumber)
    
    /// Called when a new peripheral is discovered while scanning. Also gives the RSSI (signal strength)
    func serialDidDiscoverPeripheral(peripheral: CBPeripheral, RSSI: NSNumber?)
    
    /// Called when a peripheral is connected (but not yet ready for cummunication)
    func serialDidConnect(peripheral: CBPeripheral)
    
    /// Called when a pending connection failed
    func serialDidFailToConnect(peripheral: CBPeripheral, error: NSError?)
    
    /// Called when a peripheral is ready for communication
    func serialIsReady(peripheral: CBPeripheral)
}

// Make some of the delegate functions optional
extension BluetoothSerialDelegate {
    func serialDidReceiveString(message: String) {}
    func serialDidReceiveBytes(bytes: [UInt8]) {}
    func serialDidReceiveData(data: NSData) {}
    func serialDidReadRSSI(rssi: NSNumber) {}
    func serialDidDiscoverPeripheral(peripheral: CBPeripheral, RSSI: NSNumber?) {}
    func serialDidConnect(peripheral: CBPeripheral) {}
    func serialDidFailToConnect(peripheral: CBPeripheral, error: NSError?) {}
    func serialIsReady(peripheral: CBPeripheral) {}
}


final class BluetoothSerial: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate {
    
    //MARK: Variables
    
    /// The delegate object the BluetoothDelegate methods will be called upon
    var delegate: BluetoothSerialDelegate!

    
    /// The connected peripheral (nil if none is connected)
    var connectedPeripheral: CBPeripheral?

    /// The characteristic 0xFFE1 we need to write to, of the connectedPeripheral
    weak var writeCharacteristic: CBCharacteristic?
    
    /// Whether this serial is ready to send and receive data
    var isReady: Bool {
        get {
            return centralManager.state == .poweredOn &&
                   connectedPeripheral != nil &&
                   writeCharacteristic != nil
        }
    }
    
    /// Whether to write to the HM10 with or without response.
    /// Legit HM10 modules (from JNHuaMao) require 'Write without Response',
    /// while fake modules (e.g. from Bolutek) require 'Write with Response'.
    var writeType: CBCharacteristicWriteType = .withoutResponse
    
    
//MARK: functions
    
    /// Always use this to initialize an instance
    init(delegate: BluetoothSerialDelegate) {
