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
qbluetoothserviceinfo_ohos.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/qbluetoothserviceinfo.h>
7#include <QtBluetooth/private/qbluetoothserver_ohos_p.h>
8#include <QtBluetooth/private/qohosbluetoothserver_p.h>
9
10#include <QtCore/qloggingcategory.h>
11
12#include <optional>
13
15
17
18namespace {
19
21{
22 if (serverChannel < 0) {
23 qCWarning(QT_BT_OHOS, "%s: Only RFCOMM services can be registered on HarmonyOS", Q_FUNC_INFO);
24 return std::nullopt;
25 }
26
27 if (serverChannel == 0) {
28 qCCritical(
29 QT_BT_OHOS, "%s: No RFCOMM port in the service. Cannot register service.", Q_FUNC_INFO);
30 return std::nullopt;
31 }
32
33 return static_cast<quint16>(serverChannel);
34}
35
36}
37
38QBluetoothServiceInfoPrivate::QBluetoothServiceInfoPrivate() = default;
39
41{
43}
44
45bool QBluetoothServiceInfoPrivate::isRegisteredWithServerProxy() const
46{
47 const auto serverProxy = registeredServerProxy.lock();
48 return serverProxy && optRegisteredServiceId
49 && serverProxy->tryGetRegisteredServiceId() == optRegisteredServiceId;
50}
51
53{
54 return registered && isRegisteredWithServerProxy();
55}
56
58{
59 if (!registered)
60 return false;
61
62 registered = false;
63
64 if (isRegisteredWithServerProxy()) {
65 registeredServerProxy.lock()->close();
66 } else {
67 qCDebug(
68 QT_BT_OHOS, "%s: the server is already closed. The service is unregistered anyway.",
69 Q_FUNC_INFO);
70 }
71
72 registeredServerProxy.reset();
73 optRegisteredServiceId.reset();
74
75 return true;
76}
77
78bool QBluetoothServiceInfoPrivate::registerService(const QBluetoothAddress &localAdapter)
79{
80 if (!localAdapter.isNull()) {
81 qCWarning(
82 QT_BT_OHOS,
83 "%s: HarmonyOS does not support multi device adapters. Ignoring local adapter address ...",
84 Q_FUNC_INFO);
85 }
86
88 return false;
89
90 registered = false;
91 registeredServerProxy.reset();
92 optRegisteredServiceId.reset();
93
94 const auto optPort = tryGetRfcommServerPort(serverChannel());
95 if (!optPort)
96 return false;
97
98 const auto serviceName = attributes.value(QBluetoothServiceInfo::ServiceName).toString();
99 const auto uuid = attributes.value(QBluetoothServiceInfo::ServiceId).value<QBluetoothUuid>();
100
101 if (serviceName.isEmpty()) {
102 qCCritical(
103 QT_BT_OHOS, "%s: No service name in the service. Cannot register service.",
104 Q_FUNC_INFO);
105 return false;
106 }
107
108 if (uuid.isNull()) {
109 qCCritical(
110 QT_BT_OHOS, "%s: No service UUID in the service. Cannot register service.",
111 Q_FUNC_INFO);
112 return false;
113 }
114
115 if (QtOhosBluetooth::isServiceNameAlreadyRegistered(serviceName)) {
116 qCCritical(QT_BT_OHOS, "%s: Server with such service name is registered", Q_FUNC_INFO);
117 return false;
118 }
119
120 auto serverProxyContext = QtOhosBluetooth::tryGetServerProxyContext(*optPort);
121 if (!serverProxyContext) {
122 qCCritical(QT_BT_OHOS, "%s: Server is not active. Cannot register services.", Q_FUNC_INFO);
123 return false;
124 }
125
126 auto proxy = serverProxyContext->bluetoothServerProxy;
127
128 if (proxy->isServiceRegistered()) {
129 qCCritical(
130 QT_BT_OHOS, "%s: Server has already registered service. Cannot register services.",
131 Q_FUNC_INFO);
132 return false;
133 }
134
135 const auto secure = (serverProxyContext->securityFlags != QBluetooth::Security::NoSecurity);
136
137 if (!proxy->listen(serviceName, uuid.toString(QUuid::WithoutBraces), secure))
138 return false;
139
140 if (!proxy->accept()) {
141 proxy->close();
142 return false;
143 }
144
145 registered = true;
146 registeredServerProxy = proxy;
147 optRegisteredServiceId = proxy->tryGetRegisteredServiceId();
148
149 return true;
150}
151
152QT_END_NAMESPACE
bool registerService(const QBluetoothAddress &localAdapter=QBluetoothAddress())
std::optional< quint16 > tryGetRfcommServerPort(int serverChannel)
QT_BEGIN_NAMESPACE Q_DECLARE_LOGGING_CATEGORY(lcQIORing)