Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qohosbluetoothcommon.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QtBluetooth/private/qohosbluetoothenums_p.h>
7
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>
15
16#include <optional>
17
19
20Q_DECLARE_LOGGING_CATEGORY(QT_BT_OHOS)
21
22namespace QtOhosBluetooth {
23
24namespace {
25
26std::optional<std::uint32_t> tryGetCodeFromJsBusinessError(const QNapi::Value &errorValue)
27{
28 if (!errorValue.IsObject())
29 return std::nullopt;
30
31 auto errorObject = QNapi::checkedCast<QNapi::Object>(errorValue);
32 auto errorCodeValue = QNapi::getOptionalPropOrEmpty<QNapi::Value>(errorObject, "code");
33
34 if (errorCodeValue.IsNumber())
35 return QNapi::checkedCast<QNapi::Number>(errorCodeValue).Uint32Value();
36
37 if (errorCodeValue.IsString()) {
38 const auto codeText =
39 QString::fromStdString(QNapi::checkedCast<QNapi::String>(errorCodeValue));
40 bool parsed = false;
41 const auto code = codeText.toUInt(&parsed);
42 if (parsed)
43 return code;
44
45 qOhosPrintfWarning(
46 "%s: non-numeric 'code' property in JS business error object: %s", Q_FUNC_INFO,
47 qUtf8Printable(codeText));
48 }
49
50 return std::nullopt;
51}
52
53}
54
56 const QNapi::Value &errorValue, const char *callerContextName)
57{
58 const auto optErrorCodeValue = tryGetCodeFromJsBusinessError(errorValue);
59 if (!optErrorCodeValue) {
60 qOhosPrintfError(
61 "%s: no numeric 'code' property in JS business error object", callerContextName);
62 return std::nullopt;
63 }
64
65 const auto errorCode = static_cast<QOhosBluetoothErrorCode>(*optErrorCodeValue);
66 switch (errorCode) {
77 return errorCode;
78 }
79
80 qOhosPrintfError(
81 "%s: unexpected JS business error %u", callerContextName, *optErrorCodeValue);
82 return std::nullopt;
83}
84
85bool checkAccessBluetoothPermissionGranted(const char *callerContextName)
86{
87 if (!qApp) {
88 qOhosPrintfWarning("%s: no application instance to check the permission on", callerContextName);
89 return false;
90 }
91
92 if (qApp->checkPermission(QBluetoothPermission{}) == Qt::PermissionStatus::Granted)
93 return true;
94
95 qCWarning(QT_BT_OHOS, "%s: access bluetooth permission not granted", callerContextName);
96 return false;
97}
98
100 QOhosJsState &jsState)
101{
102 using QtOhosBluetooth::enums::ohos::bluetooth::access::BluetoothState;
103
104 try {
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)) {
109 qOhosPrintfWarning(
110 "%s: cannot read the bluetooth state, error code: %u", Q_FUNC_INFO,
111 qToUnderlying(*optErrorCode));
112 }
113 return std::nullopt;
114 }
115}
116
117bool isBluetoothEnabled(QOhosJsState &jsState)
118{
119 using QtOhosBluetooth::enums::ohos::bluetooth::access::BluetoothState;
120
121 return readBluetoothState(jsState) == BluetoothState::STATE_ON;
122}
123
124}
125
126QT_END_NAMESPACE
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)