     
 }
 
+struct WriteParameters {
+    let value : NSData
+    let timeout : Double
+    let type : CBCharacteristicWriteType
+}
+
 extension CBCharacteristic : CBCharacteristicWrappable {}
 
 public class Characteristic {

        let promise = Promise<Characteristic>(queue:self.futureQueue)
        if self.canWrite {
            self.ioQueue.async() {
                Logger.debug("write characteristic value=\(value.hexStringValue()), uuid=\(self.uuid.UUIDString)")
                self.writePromises.append(promise)
                self.writeValue(value, type:type)
                self.writing = true
                ++self.writeSequence
                self.timeoutWrite(self.writeSequence, timeout:timeout)
            }
        } else {
            promise.failure(BCError.characteristicWriteNotSupported)

        self.service?.peripheral?.writeValue(value, forCharacteristic:self, type:type)
    }
    
    public func writeNext() {
        if self.writing == false && self.writeParameters.count > 0 {
            let parameters = self.writeParameters[0]
            self.writeParameters.removeAtIndex(0)
            Logger.debug("write characteristic value=\(parameters.value.hexStringValue()), uuid=\(self.uuid.UUIDString)")
            self.writeValue(parameters.value, type:parameters.type)
            self.writing = true
            ++self.writeSequence
            self.timeoutWrite(self.writeSequence, timeout:parameters.timeout)
        }
    }
}
