content: package com.adafruit.bluefruit.le.connect.ble.peripheral;
  
  import android.content.Context;
  import android.util.Log;
  
  import androidx.annotation.NonNull;
  import androidx.annotation.Nullable;
  
  import com.adafruit.bluefruit.le.connect.ble.UartPacket;
  import com.adafruit.bluefruit.le.connect.ble.UartPacketManagerBase;
  import com.adafruit.bluefruit.le.connect.mqtt.MqttManager;
  import com.adafruit.bluefruit.le.connect.mqtt.MqttSettings;
  
  public class UartPeripheralModePacketManager extends UartPacketManagerBase {
      // Log
      private final static String TAG = UartPeripheralModePacketManager.class.getSimpleName();
  
      // region Lifecycle
      public UartPeripheralModePacketManager(@NonNull Context context, @Nullable Listener listener, boolean isPacketCacheEnabled, @Nullable MqttManager mqttManager) {
          super(context, listener, isPacketCacheEnabled, mqttManager);
      }
      // endregion
  
      // region Send data
  
      public void send(@NonNull UartPeripheralService uartPeripheralService, @NonNull byte[] data/*, BlePeripheral.UpdateDatabaseCompletionHandler completionHandler*/) {
          mSentBytes += data.length;
          uartPeripheralService.setRx(data);
      }
  
      public void send(@NonNull UartPeripheralService uartPeripheralService, byte[] data, boolean wasReceivedFromMqtt) {
          if (mMqttManager != null) {
              // Mqtt publish to TX
              if (MqttSettings.isPublishEnabled(mContext)) {
                  final String topic = MqttSettings.getPublishTopic(mContext, MqttSettings.kPublishFeed_TX);
                  if (topic != null) {
                      final int qos = MqttSettings.getPublishQos(mContext, MqttSettings.kPublishFeed_TX);
                      mMqttManager.publish(topic, data, qos);
                  }
              }
          }
  
          // Create data and send to Uart
          UartPacket uartPacket = new UartPacket(null, UartPacket.TRANSFERMODE_TX, data);
  
          try {
              mPacketsSemaphore.acquire();        // don't append more data, till the delegate has finished processing it
          } catch (InterruptedException e) {
              Log.w(TAG, "InterruptedException: " + e.toString());
          }
          mPackets.add(uartPacket);
          mPacketsSemaphore.release();
  
          Listener listener = mWeakListener.get();
          if (listener != null) {
              mMainHandler.post(() -> listener.onUartPacket(uartPacket));
          }
  
          final boolean isMqttEnabled = mMqttManager != null;
          final boolean shouldBeSent = !wasReceivedFromMqtt || (isMqttEnabled && MqttSettings.getSubscribeBehaviour(mContext) == MqttSettings.kSubscribeBehaviour_Transmit);
  
          if (shouldBeSent) {
              send(uartPeripheralService, data);
          }
      }
  
      // endregion
  }
  