// static
void
BluetoothAvrcpManager::InitAvrcpInterface(BluetoothProfileResultHandler* aRes)
{
    BluetoothInterface* btInf = BluetoothInterface::GetInstance();
    if (NS_WARN_IF(!btInf)) {
        // If there's no HFP interface, we dispatch a runnable
        // that calls the profile result handler.
        nsRefPtr<nsRunnable> r =
            new OnErrorProfileResultHandlerRunnable(aRes, NS_ERROR_FAILURE);
        if (NS_FAILED(NS_DispatchToMainThread(r))) {
            BT_LOGR("Failed to dispatch HFP OnError runnable");
        }
        return;
    }

    sBtAvrcpInterface = btInf->GetBluetoothAvrcpInterface();
    if (NS_WARN_IF(!sBtAvrcpInterface)) {
        // If there's no AVRCP interface, we dispatch a runnable
        // that calls the profile result handler.
        nsRefPtr<nsRunnable> r =
            new OnErrorProfileResultHandlerRunnable(aRes, NS_ERROR_FAILURE);
        if (NS_FAILED(NS_DispatchToMainThread(r))) {
            BT_LOGR("Failed to dispatch HFP OnError runnable");
        }
        return;
    }

    BluetoothAvrcpManager* avrcpManager = BluetoothAvrcpManager::Get();
    sBtAvrcpInterface->Init(avrcpManager, new InitAvrcpResultHandler(aRes));
}
  void Init() override
  {
    BluetoothInterface* btInf = BluetoothInterface::GetInstance();
    if (NS_WARN_IF(!btInf)) {
      mRes->OnError(NS_ERROR_FAILURE);
      return;
    }

    sBtAvrcpInterface = btInf->GetBluetoothAvrcpInterface();
    if (NS_WARN_IF(!sBtAvrcpInterface)) {
      mRes->OnError(NS_ERROR_FAILURE);
      return;
    }

    BluetoothA2dpManager* a2dpManager = BluetoothA2dpManager::Get();
    sBtAvrcpInterface->Init(a2dpManager, new InitAvrcpResultHandler(mRes));
  }
// static
void
BluetoothA2dpManager::InitA2dpInterface(BluetoothProfileResultHandler* aRes)
{
  BluetoothInterface* btInf = BluetoothInterface::GetInstance();
  if (!btInf) {
    BT_LOGR("Error: Bluetooth interface not available");
    if (aRes) {
      aRes->OnError(NS_ERROR_FAILURE);
    }
    return;
  }

  sBtA2dpInterface = btInf->GetBluetoothA2dpInterface();
  if (!sBtA2dpInterface) {
    BT_LOGR("Error: Bluetooth A2DP interface not available");
    if (aRes) {
      aRes->OnError(NS_ERROR_FAILURE);
    }
    return;
  }

  int ret = sBtA2dpInterface->Init(&sBtA2dpCallbacks);
  if (ret != BT_STATUS_SUCCESS) {
    BT_LOGR("Warning: failed to init a2dp module");
  }

#if ANDROID_VERSION > 17
  sBtAvrcpInterface = btInf->GetBluetoothAvrcpInterface();
  if (!sBtAvrcpInterface) {
    BT_LOGR("Error: Bluetooth AVRCP interface not available");
    if (aRes) {
      aRes->OnError(NS_ERROR_FAILURE);
    }
    return;
  }

  ret = sBtAvrcpInterface->Init(&sBtAvrcpCallbacks);
  if (ret != BT_STATUS_SUCCESS) {
    BT_LOGR("Warning: failed to init avrcp module");
  }
#endif

  if (aRes) {
    aRes->Init();
  }
}