Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
4
#
include
"qohosbluetoothcommon_p.h"
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
18
QT_BEGIN_NAMESPACE
19
20
Q_DECLARE_LOGGING_CATEGORY(QT_BT_OHOS)
21
22
namespace
QtOhosBluetooth
{
23
24
namespace
{
25
26
std
::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
55
std
::optional<
QOhosBluetoothErrorCode
>
tryGetKnownErrorCode
(
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) {
67
case
QOhosBluetoothErrorCode
::
PermissionDenied
:
68
case
QOhosBluetoothErrorCode
::
InvalidParameter
:
69
case
QOhosBluetoothErrorCode
::
CapabilityNotSupported
:
70
case
QOhosBluetoothErrorCode
::
ServiceStopped
:
71
case
QOhosBluetoothErrorCode
::
BluetoothDisabled
:
72
case
QOhosBluetoothErrorCode
::
ProfileNotSupported
:
73
case
QOhosBluetoothErrorCode
::
UserDidNotRespond
:
74
case
QOhosBluetoothErrorCode
::
UserRefused
:
75
case
QOhosBluetoothErrorCode
::
OperationFailed
:
76
case
QOhosBluetoothErrorCode
::
InputOutput
:
77
return
errorCode;
78
}
79
80
qOhosPrintfError(
81
"%s: unexpected JS business error %u"
, callerContextName, *optErrorCodeValue);
82
return
std
::nullopt;
83
}
84
85
bool
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
99
std
::
optional
<
QtOhosBluetooth
::
enums
::
ohos
::
bluetooth
::
access
::
BluetoothState
>
readBluetoothState
(
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
117
bool
isBluetoothEnabled
(QOhosJsState &jsState)
118
{
119
using
QtOhosBluetooth::enums::ohos::bluetooth::access::BluetoothState;
120
121
return
readBluetoothState(jsState) == BluetoothState::STATE_ON;
122
}
123
124
}
125
126
QT_END_NAMESPACE
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
QtOhosBluetooth
Definition
qohosbluetoothaccess.cpp:21
QtOhosBluetooth::tryGetKnownErrorCode
std::optional< QOhosBluetoothErrorCode > tryGetKnownErrorCode(const QNapi::Value &errorValue, const char *callerContextName)
Definition
qohosbluetoothcommon.cpp:55
QtOhosBluetooth::QOhosBluetoothErrorCode
QOhosBluetoothErrorCode
Definition
qohosbluetoothcommon_p.h:30
QtOhosBluetooth::QOhosBluetoothErrorCode::UserDidNotRespond
@ UserDidNotRespond
Definition
qohosbluetoothcommon_p.h:37
QtOhosBluetooth::QOhosBluetoothErrorCode::UserRefused
@ UserRefused
Definition
qohosbluetoothcommon_p.h:38
QtOhosBluetooth::QOhosBluetoothErrorCode::ServiceStopped
@ ServiceStopped
Definition
qohosbluetoothcommon_p.h:34
QtOhosBluetooth::QOhosBluetoothErrorCode::PermissionDenied
@ PermissionDenied
Definition
qohosbluetoothcommon_p.h:31
QtOhosBluetooth::QOhosBluetoothErrorCode::InvalidParameter
@ InvalidParameter
Definition
qohosbluetoothcommon_p.h:32
QtOhosBluetooth::QOhosBluetoothErrorCode::OperationFailed
@ OperationFailed
Definition
qohosbluetoothcommon_p.h:39
QtOhosBluetooth::QOhosBluetoothErrorCode::BluetoothDisabled
@ BluetoothDisabled
Definition
qohosbluetoothcommon_p.h:35
QtOhosBluetooth::QOhosBluetoothErrorCode::InputOutput
@ InputOutput
Definition
qohosbluetoothcommon_p.h:40
QtOhosBluetooth::QOhosBluetoothErrorCode::ProfileNotSupported
@ ProfileNotSupported
Definition
qohosbluetoothcommon_p.h:36
QtOhosBluetooth::QOhosBluetoothErrorCode::CapabilityNotSupported
@ CapabilityNotSupported
Definition
qohosbluetoothcommon_p.h:33
QtOhosBluetooth::checkAccessBluetoothPermissionGranted
bool checkAccessBluetoothPermissionGranted(const char *callerContextName)
Definition
qohosbluetoothcommon.cpp:85
QtOhosBluetooth::readBluetoothState
std::optional< QtOhosBluetooth::enums::ohos::bluetooth::access::BluetoothState > readBluetoothState(QOhosJsState &jsState)
Definition
qohosbluetoothcommon.cpp:99
QtOhosBluetooth::isBluetoothEnabled
bool isBluetoothEnabled(QOhosJsState &jsState)
Definition
qohosbluetoothcommon.cpp:117
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
qtconnectivity
src
bluetooth
ohos
qohosbluetoothcommon.cpp
Generated on
for Qt by
1.18.0