     }
     
     /// Send a string to the device
-    func sendMessageToDevice(message: String) {
-        guard isReady else { return }
-        
-        if let data = message.dataUsingEncoding(NSUTF8StringEncoding) {
-            connectedPeripheral!.writeValue(data, forCharacteristic: writeCharacteristic!, type: writeType)
+    func sendMessageToDevice(_ message: String) {
+        guard isReady else { return }
+        
+        if let data = message.data(using: String.Encoding.utf8) {
+            connectedPeripheral!.writeValue(data, for: writeCharacteristic!, type: writeType)
         }
     }
     
     /// Send an array of bytes to the device
-    func sendBytesToDevice(bytes: [UInt8]) {
-        guard isReady else { return }
-        
-        let data = NSData(bytes: bytes, length: bytes.count)
-        connectedPeripheral!.writeValue(data, forCharacteristic: writeCharacteristic!, type: writeType)
+    func sendBytesToDevice(_ bytes: [UInt8]) {
+        guard isReady else { return }
+        
+        let data = Data(bytes: UnsafePointer<UInt8>(bytes), count: bytes.count)
+        connectedPeripheral!.writeValue(data, for: writeCharacteristic!, type: writeType)
     }
     
     /// Send data to the device
-    func sendDataToDevice(data: NSData) {
-        guard isReady else { return }
-        
-        connectedPeripheral!.writeValue(data, forCharacteristic: writeCharacteristic!, type: writeType)
-    }
-    
-    
-    //MARK: CBCentralManagerDelegate functions
-    
-    func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
+    func sendDataToDevice(_ data: Data) {
+        guard isReady else { return }
+        
+        connectedPeripheral!.writeValue(data, for: writeCharacteristic!, type: writeType)
+    }
+    
+    
+//MARK: CBCentralManagerDelegate functions
+
+    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
         // just send it to the delegate
         delegate.serialDidDiscoverPeripheral(peripheral, RSSI: RSSI)
     }
     
-    func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
+    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
         // set some stuff right
         peripheral.delegate = self
         pendingPeripheral = nil

        
        // send it to the delegate
        delegate.serialDidConnect(peripheral)
        
        // Okay, the peripheral is connected but we're not ready yet!
        // First get the 0xFFE0 service
        // Then get the 0xFFE1 characteristic of this service
        // Subscribe to it & create a weak reference to it (for writing later on),
        // and then we're ready for communication
        
        peripheral.discoverServices([CBUUID(string: "FFE0")])
    }
    
    func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?) {
        connectedPeripheral = nil
        pendingPeripheral = nil
        
        // send it to the delegate
        delegate.serialDidDisconnect(peripheral, error: error)
    }
    
    func centralManager(central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?) {
        pendingPeripheral = nil
        
        // just send it to the delegate
        delegate.serialDidFailToConnect(peripheral, error: error)
    }
    
    func centralManagerDidUpdateState(central: CBCentralManager) {
        // note that "didDisconnectPeripheral" won't be called if BLE is turned off while connected
        connectedPeripheral = nil
        pendingPeripheral = nil
        
        // send it to the delegate
        delegate.serialDidChangeState(central.state)
    }
    
    
    //MARK: CBPeripheralDelegate functions
    
    func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
        // discover the 0xFFE1 characteristic for all services (though there should only be one)
        for service in peripheral.services! {
            peripheral.discoverCharacteristics([CBUUID(string: "FFE1")], forService: service)
        }
    }
    
    func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) {
        // check whether the characteristic we're looking for (0xFFE1) is present - just to be sure
        for characteristic in service.characteristics! {
            if characteristic.UUID == CBUUID(string: "FFE1") {
                // subscribe to this value (so we'll get notified when there is serial data for us..)
                peripheral.setNotifyValue(true, forCharacteristic: characteristic)
                
                // keep a reference to this characteristic so we can write to it
                writeCharacteristic = characteristic

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