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
qohosbluetoothaccess.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/qohosbluetoothcommon_p.h>
7
8#include <QtCore/qloggingcategory.h>
9#include <QtCore/private/qnapi_p.h>
10#include <QtCore/private/qohoscommon_p.h>
11#include <QtCore/private/qohosjstools_p.h>
12#include <QtCore/private/qohoslogger_p.h>
13
14#include <memory>
15#include <optional>
16
18
20
21namespace QtOhosBluetooth {
22
23namespace {
24
25using BluetoothState = QOhosBluetoothAccessProxy::BluetoothState;
26
27std::shared_ptr<void> registerBluetoothStateChangeConsumer(
28 QOhosJsState &jsState, QOhosConsumer<std::optional<BluetoothState>> stateChangeConsumer)
29{
30 return registerQOhosOnOffMethodsBasedEventHandler(
31 jsState.eval<QNapi::Object>("@ohos.bluetooth.access"), "stateChange",
32 [stateChangeConsumer = std::move(stateChangeConsumer)](const QOhosCallbackInfo &cbInfo) {
33 auto ohosBluetoothState = cbInfo.getFirstArg<QNapi::Number>(Q_FUNC_INFO);
34 stateChangeConsumer(
35 cbInfo.jsState().tryMapOhosEnumFromJs<BluetoothState>(ohosBluetoothState));
36 },
37 {
38 .optOnCallExceptionHandler = [](const Napi::Error &error) {
39 if (const auto optErrorCode = tryGetKnownErrorCode(error.Value(), Q_FUNC_INFO)) {
40 qOhosPrintfWarning(
41 "%s: cannot register the bluetooth state change consumer, error code: %u",
42 Q_FUNC_INFO, qToUnderlying(*optErrorCode));
43 }
44 },
45 });
46}
47
48QOhosSupplier<std::optional<BluetoothState>> makeBluetoothStateSupplier(
49 QOhosConsumer<std::optional<BluetoothState>> bluetoothStateChangeConsumer)
50{
51 bool stateChangeEventRegistered = false;
52
53 auto cachedBluetoothStateSupplier = makeQOhosDataSource<std::optional<BluetoothState>>(
54 readBluetoothState,
55 [&](QOhosJsState &jsState, QOhosConsumer<std::optional<BluetoothState>> stateChangeConsumer) {
56 auto stateChangeConsumerHandle =
57 registerBluetoothStateChangeConsumer(jsState, std::move(stateChangeConsumer));
58 stateChangeEventRegistered = stateChangeConsumerHandle != nullptr;
59
60 return stateChangeConsumerHandle;
61 },
62 std::move(bluetoothStateChangeConsumer),
63 QtOhos::invokeInQtThread,
64 Q_FUNC_INFO);
65
66 return [stateChangeEventRegistered,
67 cachedBluetoothStateSupplier = std::move(cachedBluetoothStateSupplier)]() {
68 return stateChangeEventRegistered
69 ? cachedBluetoothStateSupplier()
70 : QOhosJsThreadGateway::eval(readBluetoothState, Q_FUNC_INFO);
71 };
72}
73
74}
75
77{
78 m_ohosBluetoothStateSupplier = makeBluetoothStateSupplier(
79 [this](std::optional<BluetoothState> optBluetoothState) {
80 if (optBluetoothState)
81 Q_EMIT stateChanged(*optBluetoothState);
82 });
83}
84
86{
87 static auto instance = std::shared_ptr<QOhosBluetoothAccessProxy>(new QOhosBluetoothAccessProxy());
88 return instance;
89}
90
92{
93 return m_ohosBluetoothStateSupplier();
94}
95
97{
98 if (!checkAccessBluetoothPermissionGranted(Q_FUNC_INFO)) {
99 Q_EMIT missingPermission();
100 return QOhosBluetoothErrorCode::PermissionDenied;
101 }
102
103 auto selfRef = QtOhos::makeQThreadSafeRef(this);
104 const auto optErrorCode = QOhosJsThreadGateway::eval(
105 [&](QOhosJsState &jsState) -> std::optional<QOhosBluetoothErrorCode> {
106 try {
107 jsState.evalToPromiseOrRejectOnThrow(
108 enabled ? "@ohos.bluetooth.access.enableBluetoothAsync()"
109 : "@ohos.bluetooth.access.disableBluetoothAsync()")
110 .onCatch(
111 [selfRef](const QOhosCallbackInfo &cbInfo) {
112 const auto errorValue = cbInfo.getFirstArg<QNapi::Value>(Q_FUNC_INFO);
113 const auto errorCode =
114 tryGetKnownErrorCode(errorValue, Q_FUNC_INFO)
115 .value_or(QOhosBluetoothErrorCode::OperationFailed);
116 qOhosPrintfWarning(
117 "%s: the bluetooth switch request was rejected, error code: %u",
118 Q_FUNC_INFO, qToUnderlying(errorCode));
119 selfRef.visitInQtThreadIfAlive(
120 [errorCode](auto &self) {
121 Q_EMIT self.setBluetoothEnabledFailed(errorCode);
122 });
123 });
124 return std::nullopt;
125 } catch (const Napi::Error &error) {
126 return tryGetKnownErrorCode(error.Value(), Q_FUNC_INFO);
127 }
128 },
129 Q_FUNC_INFO);
130
131 if (!optErrorCode)
132 return std::nullopt;
133
134 if (*optErrorCode == QOhosBluetoothErrorCode::PermissionDenied) {
135 qCWarning(
136 QT_BT_OHOS, "%s: no permission to set the bluetooth enabled state %d", Q_FUNC_INFO,
137 enabled);
138 Q_EMIT missingPermission();
139 return optErrorCode;
140 }
141
142 qCWarning(
143 QT_BT_OHOS, "%s: cannot set the bluetooth enabled state %d, error code: %u", Q_FUNC_INFO,
144 enabled, qToUnderlying(*optErrorCode));
145
146 return optErrorCode;
147}
148
149}
150
151QT_END_NAMESPACE
152
153#include "moc_qohosbluetoothaccess_p.cpp"
std::optional< BluetoothState > tryGetBluetoothState() const
std::optional< QOhosBluetoothErrorCode > trySetBluetoothEnabled(bool enabled)
QT_BEGIN_NAMESPACE Q_DECLARE_LOGGING_CATEGORY(lcQIORing)