6#include <QtBluetooth/private/qohosbluetoothenums_p.h>
8#include <QtCore/qcoreapplication.h>
9#include <QtCore/qloggingcategory.h>
10#include <QtCore/qpermissions.h>
11#include <QtCore/qstring.h>
12#include <QtCore/private/qnapi_p.h>
13#include <QtCore/private/qohoscommon_p.h>
14#include <QtCore/private/qohoslogger_p.h>
20Q_DECLARE_LOGGING_CATEGORY(QT_BT_OHOS)
26std::optional<
std::uint32_t> tryGetCodeFromJsBusinessError(
const QNapi::Value &errorValue)
28 if (!errorValue.IsObject())
31 auto errorObject = QNapi::checkedCast<QNapi::Object>(errorValue);
32 auto errorCodeValue = QNapi::getOptionalPropOrEmpty<QNapi::Value>(errorObject,
"code");
34 if (errorCodeValue.IsNumber())
35 return QNapi::checkedCast<QNapi::Number>(errorCodeValue).Uint32Value();
37 if (errorCodeValue.IsString()) {
39 QString::fromStdString(QNapi::checkedCast<QNapi::String>(errorCodeValue));
41 const auto code = codeText.toUInt(&parsed);
46 "%s: non-numeric 'code' property in JS business error object: %s", Q_FUNC_INFO,
47 qUtf8Printable(codeText));
56 const QNapi::Value &errorValue,
const char *callerContextName)
58 const auto optErrorCodeValue = tryGetCodeFromJsBusinessError(errorValue);
59 if (!optErrorCodeValue) {
61 "%s: no numeric 'code' property in JS business error object", callerContextName);
81 "%s: unexpected JS business error %u", callerContextName, *optErrorCodeValue);
88 qOhosPrintfWarning(
"%s: no application instance to check the permission on", callerContextName);
92 if (qApp->checkPermission(QBluetoothPermission{}) == Qt::PermissionStatus::Granted)
95 qCWarning(QT_BT_OHOS,
"%s: access bluetooth permission not granted", callerContextName);
100 QOhosJsState &jsState)
102 using QtOhosBluetooth::enums::ohos::bluetooth::access::BluetoothState;
105 return jsState.tryMapOhosEnumFromJs<BluetoothState>(
106 jsState.eval<QNapi::Number>(
"@ohos.bluetooth.access.getState()"));
107 }
catch (
const Napi::Error &error) {
108 if (
const auto optErrorCode = tryGetKnownErrorCode(error.Value(), Q_FUNC_INFO)) {
110 "%s: cannot read the bluetooth state, error code: %u", Q_FUNC_INFO,
111 qToUnderlying(*optErrorCode));
119 using QtOhosBluetooth::enums::ohos::bluetooth::access::BluetoothState;
121 return readBluetoothState(jsState) == BluetoothState::STATE_ON;
Combined button and popup list for selecting options.
std::optional< QOhosBluetoothErrorCode > tryGetKnownErrorCode(const QNapi::Value &errorValue, const char *callerContextName)
bool checkAccessBluetoothPermissionGranted(const char *callerContextName)
std::optional< QtOhosBluetooth::enums::ohos::bluetooth::access::BluetoothState > readBluetoothState(QOhosJsState &jsState)
bool isBluetoothEnabled(QOhosJsState &jsState)