25using BluetoothState = QOhosBluetoothAccessProxy::BluetoothState;
27std::shared_ptr<
void> registerBluetoothStateChangeConsumer(
28 QOhosJsState &jsState, QOhosConsumer<std::optional<BluetoothState>> stateChangeConsumer)
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);
35 cbInfo.jsState().tryMapOhosEnumFromJs<BluetoothState>(ohosBluetoothState));
38 .optOnCallExceptionHandler = [](
const Napi::Error &error) {
39 if (
const auto optErrorCode = tryGetKnownErrorCode(error.Value(), Q_FUNC_INFO)) {
41 "%s: cannot register the bluetooth state change consumer, error code: %u",
42 Q_FUNC_INFO, qToUnderlying(*optErrorCode));
48QOhosSupplier<std::optional<BluetoothState>> makeBluetoothStateSupplier(
49 QOhosConsumer<std::optional<BluetoothState>> bluetoothStateChangeConsumer)
51 bool stateChangeEventRegistered =
false;
53 auto cachedBluetoothStateSupplier = makeQOhosDataSource<std::optional<BluetoothState>>(
55 [&](QOhosJsState &jsState, QOhosConsumer<std::optional<BluetoothState>> stateChangeConsumer) {
56 auto stateChangeConsumerHandle =
57 registerBluetoothStateChangeConsumer(jsState, std::move(stateChangeConsumer));
58 stateChangeEventRegistered = stateChangeConsumerHandle !=
nullptr;
60 return stateChangeConsumerHandle;
62 std::move(bluetoothStateChangeConsumer),
63 QtOhos::invokeInQtThread,
66 return [stateChangeEventRegistered,
67 cachedBluetoothStateSupplier = std::move(cachedBluetoothStateSupplier)]() {
68 return stateChangeEventRegistered
69 ? cachedBluetoothStateSupplier()
70 : QOhosJsThreadGateway::eval(readBluetoothState, Q_FUNC_INFO);
78 m_ohosBluetoothStateSupplier = makeBluetoothStateSupplier(
79 [
this](std::optional<BluetoothState> optBluetoothState) {
80 if (optBluetoothState)
81 Q_EMIT stateChanged(*optBluetoothState);
93 return m_ohosBluetoothStateSupplier();
98 if (!checkAccessBluetoothPermissionGranted(Q_FUNC_INFO)) {
99 Q_EMIT missingPermission();
100 return QOhosBluetoothErrorCode::PermissionDenied;
103 auto selfRef = QtOhos::makeQThreadSafeRef(
this);
104 const auto optErrorCode = QOhosJsThreadGateway::eval(
105 [&](QOhosJsState &jsState) -> std::optional<QOhosBluetoothErrorCode> {
107 jsState.evalToPromiseOrRejectOnThrow(
108 enabled ?
"@ohos.bluetooth.access.enableBluetoothAsync()"
109 :
"@ohos.bluetooth.access.disableBluetoothAsync()")
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);
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);
125 }
catch (
const Napi::Error &error) {
126 return tryGetKnownErrorCode(error.Value(), Q_FUNC_INFO);
134 if (*optErrorCode == QOhosBluetoothErrorCode::PermissionDenied) {
136 QT_BT_OHOS,
"%s: no permission to set the bluetooth enabled state %d", Q_FUNC_INFO,
138 Q_EMIT missingPermission();
143 QT_BT_OHOS,
"%s: cannot set the bluetooth enabled state %d, error code: %u", Q_FUNC_INFO,
144 enabled, qToUnderlying(*optErrorCode));