nsresult
StartStopGonkBluetooth(bool aShouldEnable)
{
    bool result;

    // Platform specific check for gonk until object is divided in
    // different implementations per platform. Linux doesn't require
    // bluetooth firmware loading, but code should work otherwise.
    if (!EnsureBluetoothInit()) {
        NS_ERROR("Failed to load bluedroid library.\n");
        return NS_ERROR_FAILURE;
    }

    // return 1 if it's enabled, 0 if it's disabled, and -1 on error
    int isEnabled = IsBluetoothEnabled();

    if ((isEnabled == 1 && aShouldEnable) || (isEnabled == 0 && !aShouldEnable)) {
        result = true;
    } else if (isEnabled < 0) {
        result = false;
    } else if (aShouldEnable) {
        result = (EnableBluetooth() == 0) ? true : false;
    } else {
        result = (DisableBluetooth() == 0) ? true : false;
    }
    if (!result) {
        NS_WARNING("Could not set gonk bluetooth firmware!");
        return NS_ERROR_FAILURE;
    }

    return NS_OK;
}
bool
BluetoothGonkService::IsEnabledInternal()
{
  MOZ_ASSERT(!NS_IsMainThread());

  if (!EnsureBluetoothInit()) {
    NS_ERROR("Failed to load bluedroid library.\n");
    return false;
  }

  return (sBluedroidFunctions.bt_is_enabled() == 1);
}
static nsresult
StartStopGonkBluetooth(bool aShouldEnable)
{
  bool result;
  
  // Platform specific check for gonk until object is divided in
  // different implementations per platform. Linux doesn't require
  // bluetooth firmware loading, but code should work otherwise.
  if (!EnsureBluetoothInit()) {
    NS_ERROR("Failed to load bluedroid library.\n");
    return NS_ERROR_FAILURE;
  }

  // return 1 if it's enabled, 0 if it's disabled, and -1 on error
  int isEnabled = sBluedroidFunctions.bt_is_enabled();

  if ((isEnabled == 1 && aShouldEnable) || (isEnabled == 0 && !aShouldEnable)) {
    return NS_OK;
  }
  if (aShouldEnable) {
    result = (sBluedroidFunctions.bt_enable() == 0) ? true : false;
    if (sBluedroidFunctions.bt_is_enabled() < 0) {
      // if isEnabled < 0, this means we brought up the firmware, but something
      // went wrong with bluetoothd. Post a warning message, but try to proceed
      // with firmware unloading if that was requested, so we can retry later.
      BT_WARNING("Bluetooth firmware up, but cannot connect to HCI socket! "
        "Check bluetoothd and try stopping/starting bluetooth again.");
      // Just disable now, return an error.
      if (sBluedroidFunctions.bt_disable() != 0) {
        BT_WARNING("Problem shutting down bluetooth after error in bringup!");
      }
      return NS_ERROR_FAILURE;
    }
  } else {
    result = (sBluedroidFunctions.bt_disable() == 0) ? true : false;
  }
  if (!result) {
    BT_WARNING("Could not set gonk bluetooth firmware!");
    return NS_ERROR_FAILURE;
  }
  
  return NS_OK;
}
    NS_IMETHOD Run() 
    {
      MOZ_ASSERT(!NS_IsMainThread());

      bool result;

#ifdef MOZ_WIDGET_GONK
      // Platform specific check for gonk until object is divided in
      // different implementations per platform. Linux doesn't require
      // bluetooth firmware loading, but code should work otherwise.
      if(!EnsureBluetoothInit()) {
        NS_ERROR("Failed to load bluedroid library.\n");
        return NS_ERROR_FAILURE;
      }

      // return 1 if it's enabled, 0 if it's disabled, and -1 on error
      int isEnabled = sBluedroidFunctions.bt_is_enabled();

      if ((isEnabled == 1 && mEnabled) || (isEnabled == 0 && !mEnabled)) {
        result = true;
      } else if (isEnabled < 0) {
        result = false;
      } else if (mEnabled) {
        result = (sBluedroidFunctions.bt_enable() == 0) ? true : false;
      } else {
        result = (sBluedroidFunctions.bt_disable() == 0) ? true : false;
      }
#else
      result = true;
      NS_WARNING("No bluetooth support in this build configuration, faking a success event instead");
#endif

      // Create a result thread and pass it to Main Thread, 
      nsCOMPtr<nsIRunnable> resultRunnable = new ToggleBtResultTask(mAdapterPtr, mDOMRequest, mEnabled, result);

      if (NS_FAILED(NS_DispatchToMainThread(resultRunnable))) {
        NS_WARNING("Failed to dispatch to main thread!");
      }

      return NS_OK;
    }