6#include <QtBluetooth/private/qohosbluetoothcommon_p.h>
8#include <QtCore/private/qcore_ohos_p.h>
9#include <QtCore/private/qnapi_p.h>
10#include <QtCore/private/qohosjstools_p.h>
11#include <QtCore/private/qohoslogger_p.h>
21constexpr int permanentlyDiscoverableDuration = 0;
24QOhosSupplier<T> makeQOhosLazySupplier(
25 T fallbackValue, QOhosSupplier<std::optional<QOhosSupplier<T>>> supplierFactory)
29 QOhosSupplier<T> supplier;
30 QOhosSupplier<std::optional<QOhosSupplier<T>>> optSupplierFactory;
33 auto context =
std::make_shared<Context>();
34 if (
auto optSupplier = supplierFactory()) {
35 context->supplier =
std::move(*optSupplier);
37 context->supplier = [fallbackValue =
std::move(fallbackValue)]() {
return fallbackValue; };
38 context->optSupplierFactory =
std::move(supplierFactory);
42 if (context->optSupplierFactory) {
43 if (
auto optSupplier = context->optSupplierFactory()) {
44 context->supplier =
std::move(*optSupplier);
45 context->optSupplierFactory =
nullptr;
49 return context->supplier();
53std::optional<QOhosBluetoothLocalDeviceProxy::ScanMode> readScanMode(QOhosJsState &jsState)
55 if (!isBluetoothEnabled(jsState))
59 return jsState.tryMapOhosEnumFromJs<QOhosBluetoothLocalDeviceProxy::ScanMode>(
60 jsState.eval<QNapi::Number>(
"@ohos.bluetooth.connection.getBluetoothScanMode()"));
61 }
catch (
const Napi::Error &error) {
62 if (
const auto optErrorCode = tryGetKnownErrorCode(error.Value(), Q_FUNC_INFO)) {
64 "%s: cannot read the bluetooth scan mode, error code: %u", Q_FUNC_INFO, qToUnderlying(*optErrorCode));
70std::shared_ptr<
void> registerScanModeChangeConsumer(
71 QOhosJsState &jsState,
72 QOhosConsumer<std::optional<QOhosBluetoothLocalDeviceProxy::ScanMode>> scanModeChangeConsumer)
74 auto sharedScanModeChangeConsumer = QtOhos::moveToSharedPtr(std::move(scanModeChangeConsumer));
76 auto connectionModule = jsState.eval<QNapi::Object>(
"@ohos.bluetooth.connection");
78 auto scanModeChangeHandlerRef = QtOhos::moveToSharedPtr(
79 QNapi::Reference<>::makePersistentFrom(
82 [weakScanModeChangeConsumer = QtOhos::makeWeakPtr(sharedScanModeChangeConsumer)](
const QOhosCallbackInfo &cbInfo) {
83 auto scanModeChangeConsumer = weakScanModeChangeConsumer.lock();
84 if (!scanModeChangeConsumer) {
86 "%s: got unexpected scan mode change callback call for detached handler",
88 return cbInfo.Env().Undefined();
91 auto ohosScanMode = cbInfo.getFirstArg<QNapi::Number>(Q_FUNC_INFO);
92 (*scanModeChangeConsumer)(
93 cbInfo.jsState().tryMapOhosEnumFromJs<QOhosBluetoothLocalDeviceProxy::ScanMode>(ohosScanMode));
95 return cbInfo.Env().Undefined();
99 connectionModule.eval(
"onScanModeChange(*)", {scanModeChangeHandlerRef->Value()});
100 }
catch (
const Napi::Error &error) {
101 if (
const auto optErrorCode = tryGetKnownErrorCode(error.Value(), Q_FUNC_INFO)) {
103 "%s: cannot register the scan mode change consumer, error code: %u", Q_FUNC_INFO,
104 qToUnderlying(*optErrorCode));
109 return QtOhos::makeDestroyNotifier(
110 [sharedScanModeChangeConsumer, scanModeChangeHandlerRef,
111 connectionModuleWeakRef = QtOhos::moveToSharedPtr(Napi::Weak(connectionModule))]() {
112 auto connectionModuleValue = connectionModuleWeakRef->Value();
113 if (!connectionModuleValue.IsObject()) {
115 "%s: not unregistering the scan mode change consumer, the module is not alive anymore",
121 QNapi::checkedCast<QNapi::Object>(connectionModuleValue).eval(
122 "offScanModeChange(*)", {scanModeChangeHandlerRef->Value()});
123 }
catch (
const Napi::Error &error) {
124 if (
const auto optErrorCode = tryGetKnownErrorCode(error.Value(), Q_FUNC_INFO)) {
126 "%s: cannot unregister the scan mode change consumer, error code: %u",
127 Q_FUNC_INFO, qToUnderlying(*optErrorCode));
133QOhosSupplier<std::optional<QOhosBluetoothLocalDeviceProxy::ScanMode>> makeScanModeSupplier(
134 QOhosConsumer<std::optional<QOhosBluetoothLocalDeviceProxy::ScanMode>> optScanModeChangeConsumer)
136 bool scanModeChangeEventRegistered =
false;
138 auto cachedScanModeSupplier =
139 makeQOhosDataSource<std::optional<QOhosBluetoothLocalDeviceProxy::ScanMode>>(
141 [&](QOhosJsState &jsState,
142 QOhosConsumer<std::optional<QOhosBluetoothLocalDeviceProxy::ScanMode>> scanModeChangeConsumer) {
143 auto scanModeChangeConsumerHandle =
144 registerScanModeChangeConsumer(jsState, std::move(scanModeChangeConsumer));
145 scanModeChangeEventRegistered = scanModeChangeConsumerHandle !=
nullptr;
147 return scanModeChangeConsumerHandle;
149 std::move(optScanModeChangeConsumer),
150 QtOhos::invokeInQtThread,
153 return [scanModeChangeEventRegistered, cachedScanModeSupplier = std::move(cachedScanModeSupplier)]()
154 -> std::optional<QOhosBluetoothLocalDeviceProxy::ScanMode> {
155 if (scanModeChangeEventRegistered) {
156 if (
const auto optCachedScanMode = cachedScanModeSupplier())
157 return optCachedScanMode;
160 return QOhosJsThreadGateway::eval(readScanMode, Q_FUNC_INFO);
191 if (!checkAccessBluetoothPermissionGranted(Q_FUNC_INFO))
194 return QOhosJsThreadGateway::eval(
195 [](QOhosJsState &jsState) -> std::optional<QString> {
196 if (!isBluetoothEnabled(jsState))
200 return QString::fromStdString(
201 jsState.eval<QNapi::String>(
"@ohos.bluetooth.connection.getLocalName()").Utf8Value());
202 }
catch (
const Napi::Error &error) {
203 if (
const auto optErrorCode = tryGetKnownErrorCode(error.Value(), Q_FUNC_INFO)) {
205 "%s: cannot read the local device name, error code: %u", Q_FUNC_INFO,
206 qToUnderlying(*optErrorCode));
215 return m_scanModeSupplier();
219 QOhosBluetoothLocalDeviceProxy::ScanMode scanMode)
221 if (!checkAccessBluetoothPermissionGranted(Q_FUNC_INFO)) {
222 Q_EMIT missingPermission();
223 return QOhosBluetoothErrorCode::PermissionDenied;
226 const auto optErrorCode = QOhosJsThreadGateway::eval(
227 [&](QOhosJsState &jsState) -> std::optional<QOhosBluetoothErrorCode> {
228 if (!isBluetoothEnabled(jsState))
229 return QOhosBluetoothErrorCode::BluetoothDisabled;
233 "@ohos.bluetooth.connection.setBluetoothScanMode(*)",
235 jsState.mapOhosEnumToJs(scanMode),
236 permanentlyDiscoverableDuration,
239 }
catch (
const Napi::Error &error) {
240 return tryGetKnownErrorCode(error.Value(), Q_FUNC_INFO);
244 if (optErrorCode == QOhosBluetoothErrorCode::PermissionDenied) {
245 Q_EMIT missingPermission();
255#include "moc_qohosbluetoothlocaldevice_p.cpp"
std::optional< QString > tryGetLocalDeviceName() const
std::optional< QOhosBluetoothErrorCode > trySetBluetoothScanMode(ScanMode scanMode)
static std::shared_ptr< QOhosBluetoothLocalDeviceProxy > instance()
std::optional< ScanMode > tryGetScanMode()
QOhosBluetoothLocalDeviceProxy()
Combined button and popup list for selecting options.