                 }
             }
         }
-        
-        // Write data to our characteristic
-        recursiveWrite(packetList) { result in completion(result) }    }
+
+        // Write data to our characteristic if write not already in process of writing
+        packetQueue.numSets += 1
+        recursiveWrite() { result in completion(result) }
+    }
+    
 }

    
    /// `SwiftyBluetooth` peripheral for Moment
    var peripheral: Peripheral?

    // MARK: - Initialization
    

    
}

/// A simple FIFO (first in, first out) queue for managing data to be written to the Javascript BLE characteristic (thread safe)
final internal class PacketQueue {
    /// Internal array for managing queue
    private var array = [ArraySlice<UInt8>]()
    
    /// Internal access queue for managing packet queue
    private let accessQueue = DispatchQueue(label: "SynchronizedArrayAccess", attributes: .concurrent)
    
    /// Variable for counting the number of packet sets queued
    var numSets = 0
    
    /// Variable for getting count in queue
    var count: Int {
        var count = 0
        
        accessQueue.sync {
            count = self.array.count
        }
        
        return count
    }
    
    /// Variable for checking if the queue is empty
    var isEmpty: Bool {
        var isEmpty = true
        
        accessQueue.sync {
            isEmpty = self.array.isEmpty
        }
        
        return isEmpty
    }
    
    /// Method for adding item to the queue
    func enqueue(_ element: ArraySlice<UInt8>) {
        accessQueue.async(flags:.barrier) {
            self.array.append(element)
        }
    }
    
    /// Method for removing item from the queue
    func dequeue() -> ArraySlice<UInt8>? {
        var element: ArraySlice<UInt8>? = nil
        
        accessQueue.sync {
            element = self.array.first
        }
        
        accessQueue.async(flags:.barrier) {
            self.array.removeFirst()
        }
        
        return element
    }
}

// MARK: - Bluetooth Manager Class

/// Global variable readily allows access to singleton manager
