/// <summary>
        /// Writes the given data in the bluetooth connection interface.
        /// </summary>
        /// <param name="data">The data to be written in the connection interface.</param>
        /// <param name="offset">The start offset in the data to write.</param>
        /// <param name="length">The number of bytes to write.</param>
        /// <exception cref="XBeeException">If there is any XBee error.</exception>
        /// <seealso cref="WriteData(byte[])"/>
        public void WriteData(byte[] data, int offset, int length)
        {
            lock (txLock)
            {
                Debug.WriteLine("----- WriteData " + HexUtils.ByteArrayToHexString(data));
                bool dataWritten = false;

                // Create a task to write in the TX characteristic.
                Task task = Task.Run(async() =>
                {
                    try
                    {
                        byte[] buffer = new byte[length];
                        Array.Copy(data, offset, buffer, 0, length);

                        byte[] dataToWrite = encrypt ? encryptor.TransformFinalBlock(buffer, 0, buffer.Length) : buffer;

                        // Split the data in chunks with a max length of the current MTU.
                        foreach (byte[] chunk in GetChunks(dataToWrite))
                        {
                            // Write the chunk in the TX characteristic.
                            dataWritten = await txCharacteristic.WriteAsync(chunk);
                        }
                    }
                    finally
                    {
                        lock (writeLock)
                        {
                            Monitor.Pulse(writeLock);
                        }
                    }
                });

                if (!task.IsCompleted)
                {
                    // Wait until the task finishes.
                    lock (writeLock)
                    {
                        Monitor.Wait(writeLock, WRITE_TIMEOUT);
                    }
                }

                // If the data could not be written, decrement the counter and throw an exception.
                if (!dataWritten)
                {
                    encryptor.DecrementCounter();
                    throw new XBeeException(ERROR_WRITE);
                }

                // If the task finished with excepction, throw it.
                if (task.Exception != null)
                {
                    throw task.Exception.InnerException;
                }
            }
        }
        /// <summary>
        /// Method executed when new data is received in the RX characteristic.
        /// </summary>
        /// <param name="sender">Characteristic Updated Event sender.</param>
        /// <param name="args">Characteristic Updated Event arguments.</param>
        /// <seealso cref="CharacteristicUpdatedEventArgs"/>
        private void DataReceived(object sender, CharacteristicUpdatedEventArgs args)
        {
            byte[] value = args.Characteristic.Value;

            // If the communication is encrypted, decrypt the received data.
            if (encrypt)
            {
                value = decryptor.TransformFinalBlock(value, 0, value.Length);
            }

            Debug.WriteLine("----- RX char " + HexUtils.ByteArrayToHexString(value));

            Stream.Write(value, 0, value.Length);

            // Notify that data has been received.
            lock (this)
            {
                Monitor.Pulse(this);
            }
        }