From f8686d388faee727d09053c26a4583ac722a9631 Mon Sep 17 00:00:00 2001 From: lee Date: Mon, 18 Aug 2025 16:45:31 +0800 Subject: [PATCH] Add multi switch Signed-off-by: lee Change-Id: Ifc1416ffef8f2055226794477ce386f3e76e4885 --- frameworks/inner/ipc/common/bt_def.h | 6 +++ .../include/bluetooth_host_observer_stub.h | 2 + .../inner/ipc/include/bluetooth_host_proxy.h | 6 +-- .../bluetooth_service_ipc_interface_code.h | 1 + .../inner/ipc/interface/i_bluetooth_host.h | 6 +-- .../ipc/interface/i_bluetooth_host_observer.h | 1 + .../ipc/src/bluetooth_host_observer_stub.cpp | 11 +++++ .../inner/ipc/src/bluetooth_host_proxy.cpp | 15 ++++-- frameworks/inner/src/bluetooth_host.cpp | 49 +++++++++++++------ .../inner/src/bluetooth_switch_module.cpp | 32 ++++++------ .../inner/src/bluetooth_switch_module.h | 14 +++--- .../napi/src/access/napi_bluetooth_access.cpp | 4 +- .../inner_api/include/bluetooth_errorcode.h | 1 + interfaces/inner_api/include/bluetooth_host.h | 13 +++-- 14 files changed, 109 insertions(+), 52 deletions(-) diff --git a/frameworks/inner/ipc/common/bt_def.h b/frameworks/inner/ipc/common/bt_def.h index c455c1e7..a565fd42 100644 --- a/frameworks/inner/ipc/common/bt_def.h +++ b/frameworks/inner/ipc/common/bt_def.h @@ -1800,6 +1800,12 @@ enum BluetoothSwitchState { STATE_HALF, }; +enum BluetoothTransferredSwitchAction { + TRANS_ACTION_ENABLE_BLUETOOTH = 0, + TRANS_ACTION_DISABLE_BLUETOOTH, + TRANS_ACTION_ENABLE_BLUETOOTH_TO_RESTRICT_MODE, +}; + enum ControlType { PLAY = 0, VIBRATE = 1, diff --git a/frameworks/inner/ipc/include/bluetooth_host_observer_stub.h b/frameworks/inner/ipc/include/bluetooth_host_observer_stub.h index 5a76da0a..75bd63ae 100644 --- a/frameworks/inner/ipc/include/bluetooth_host_observer_stub.h +++ b/frameworks/inner/ipc/include/bluetooth_host_observer_stub.h @@ -51,6 +51,8 @@ private: MessageParcel &data, MessageParcel &reply); static int32_t OnBluetoothStateChangedInner(BluetoothHostObserverStub *stub, MessageParcel &data, MessageParcel &reply); + static int32_t OnBluetoothSwitchActionInner(BluetoothHostObserverStub *stub, + MessageParcel &data, MessageParcel &reply); static int32_t OnBluetoothRefusePolicyChangedInner(BluetoothHostObserverStub *stub, MessageParcel &data, MessageParcel &reply); diff --git a/frameworks/inner/ipc/include/bluetooth_host_proxy.h b/frameworks/inner/ipc/include/bluetooth_host_proxy.h index 5e71b4d6..0c76674b 100644 --- a/frameworks/inner/ipc/include/bluetooth_host_proxy.h +++ b/frameworks/inner/ipc/include/bluetooth_host_proxy.h @@ -34,7 +34,7 @@ public: void RegisterObserver(const sptr &observer) override; void DeregisterObserver(const sptr &observer) override; int32_t EnableBt() override; - int32_t DisableBt(bool isAsync) override; + int32_t DisableBt(bool isAsync, const std::string &callingName) override; int32_t SatelliteControl(int type, int state) override; sptr GetProfile(const std::string &name) override; sptr GetBleRemote(const std::string &name) override; @@ -42,7 +42,7 @@ public: int32_t GetBtState(int &state) override; int32_t GetLocalAddress(std::string &addr) override; int32_t DisableBle() override; - int32_t EnableBle(bool noAutoConnect, bool isAsync) override; + int32_t EnableBle(bool noAutoConnect, bool isAsync, const std::string &callingName) override; std::vector GetProfileList() override; int32_t GetMaxNumConnectedAudioDevices() override; int32_t GetBtConnectionState(int &state) override; @@ -109,7 +109,7 @@ public: int32_t SetVirtualAutoConnectType(const std::string &address, int connType, int businessType) override; int32_t SetFastScanLevel(int level) override; void UpdateVirtualDevice(int32_t action, const std::string &address) override; - int32_t EnableBluetoothToRestrictMode(void) override; + int32_t EnableBluetoothToRestrictMode(const std::string &callingName) override; int32_t ControlDeviceAction(const std::string &deviceId, uint32_t controlType, uint32_t controlTypeVal, uint32_t controlObject) override; int32_t GetLastConnectionTime(const std::string &address, int64_t &connectionTime) override; diff --git a/frameworks/inner/ipc/interface/bluetooth_service_ipc_interface_code.h b/frameworks/inner/ipc/interface/bluetooth_service_ipc_interface_code.h index 51975363..6ce3a02d 100644 --- a/frameworks/inner/ipc/interface/bluetooth_service_ipc_interface_code.h +++ b/frameworks/inner/ipc/interface/bluetooth_service_ipc_interface_code.h @@ -196,6 +196,7 @@ enum BluetoothHostObserverInterfaceCode { BT_HOST_OBSERVER_DEVICE_ADDR_CHANGED, BT_HOST_OBSERVER_STATE_CHANGE_V2, BT_HOST_OBSERVER_REFUSE_POLICY_CHANGE, + BT_HOST_OBSERVER_SWITCH_ACTION_TRANSFER, // The last code, if you want to add a new code, please add it before this BT_HOST_OBSERVER_BUTT }; diff --git a/frameworks/inner/ipc/interface/i_bluetooth_host.h b/frameworks/inner/ipc/interface/i_bluetooth_host.h index 878b606b..245b3170 100644 --- a/frameworks/inner/ipc/interface/i_bluetooth_host.h +++ b/frameworks/inner/ipc/interface/i_bluetooth_host.h @@ -60,7 +60,7 @@ public: virtual void RegisterObserver(const sptr &observer) = 0; virtual void DeregisterObserver(const sptr &observer) = 0; virtual int32_t EnableBt() = 0; - virtual int32_t DisableBt(bool isAsync) = 0; + virtual int32_t DisableBt(bool isAsync, const std::string &callingName) = 0; virtual int32_t SatelliteControl(int type, int state) = 0; virtual sptr GetProfile(const std::string &name) = 0; virtual sptr GetBleRemote(const std::string &name) = 0; @@ -68,7 +68,7 @@ public: virtual int32_t GetBtState(int &state) = 0; virtual int32_t GetLocalAddress(std::string &addr) = 0; virtual int32_t DisableBle() = 0; - virtual int32_t EnableBle(bool noAutoConnect, bool isAsync) = 0; + virtual int32_t EnableBle(bool noAutoConnect, bool isAsync, const std::string &callingName) = 0; virtual std::vector GetProfileList() = 0; virtual int32_t GetMaxNumConnectedAudioDevices() = 0; virtual int32_t GetBtConnectionState(int &state) = 0; @@ -136,7 +136,7 @@ public: virtual int32_t SetVirtualAutoConnectType(const std::string &address, int connType, int businessType) = 0; virtual int32_t SetFastScanLevel(int level) = 0; virtual void UpdateVirtualDevice(int32_t action, const std::string &address) = 0; - virtual int32_t EnableBluetoothToRestrictMode(void) = 0; + virtual int32_t EnableBluetoothToRestrictMode(const std::string &callingName) = 0; virtual int32_t ControlDeviceAction(const std::string &deviceId, uint32_t controlType, uint32_t controlTypeVal, uint32_t controlObject) = 0; virtual int32_t GetLastConnectionTime(const std::string &address, int64_t &connectionTime) = 0; diff --git a/frameworks/inner/ipc/interface/i_bluetooth_host_observer.h b/frameworks/inner/ipc/interface/i_bluetooth_host_observer.h index a92ce1f3..4c54492e 100644 --- a/frameworks/inner/ipc/interface/i_bluetooth_host_observer.h +++ b/frameworks/inner/ipc/interface/i_bluetooth_host_observer.h @@ -39,6 +39,7 @@ public: virtual void OnDeviceNameChanged(const std::string &deviceName) = 0; virtual void OnDeviceAddrChanged(const std::string &address) = 0; virtual void OnBluetoothStateChanged(int32_t state) = 0; + virtual void OnBluetoothSwitchAction(int32_t action, const std::string &callingName) = 0; virtual void OnRefusePolicyChanged(const int32_t pid, const int64_t prohibitedSecondsTime) = 0; }; } // namespace Bluetooth diff --git a/frameworks/inner/ipc/src/bluetooth_host_observer_stub.cpp b/frameworks/inner/ipc/src/bluetooth_host_observer_stub.cpp index 3251493c..a423022f 100644 --- a/frameworks/inner/ipc/src/bluetooth_host_observer_stub.cpp +++ b/frameworks/inner/ipc/src/bluetooth_host_observer_stub.cpp @@ -42,6 +42,8 @@ BluetoothHostObserverStub::BluetoothHostObserverStub() BluetoothHostObserverStub::OnDeviceAddrChangedInner; memberFuncMap_[static_cast(BluetoothHostObserverInterfaceCode::BT_HOST_OBSERVER_STATE_CHANGE_V2)] = BluetoothHostObserverStub::OnBluetoothStateChangedInner; + memberFuncMap_[static_cast(BluetoothHostObserverInterfaceCode::BT_HOST_OBSERVER_SWITCH_ACTION_TRANSFER)] = + BluetoothHostObserverStub::OnBluetoothSwitchActionInner; memberFuncMap_[static_cast(BluetoothHostObserverInterfaceCode::BT_HOST_OBSERVER_REFUSE_POLICY_CHANGE)] = BluetoothHostObserverStub::OnBluetoothRefusePolicyChangedInner; } @@ -187,6 +189,15 @@ int32_t BluetoothHostObserverStub::OnBluetoothStateChangedInner(BluetoothHostObs return NO_ERROR; } +int32_t BluetoothHostObserverStub::OnBluetoothSwitchActionInner(BluetoothHostObserverStub *stub, + MessageParcel &data, MessageParcel &reply) +{ + int32_t action = data.ReadInt32(); + std::string name = data.ReadString(); + stub->OnBluetoothSwitchAction(action, name); + return NO_ERROR; +} + int32_t BluetoothHostObserverStub::OnBluetoothRefusePolicyChangedInner( BluetoothHostObserverStub *stub, MessageParcel &data, MessageParcel &reply) { diff --git a/frameworks/inner/ipc/src/bluetooth_host_proxy.cpp b/frameworks/inner/ipc/src/bluetooth_host_proxy.cpp index 85ca100d..850ff8cc 100644 --- a/frameworks/inner/ipc/src/bluetooth_host_proxy.cpp +++ b/frameworks/inner/ipc/src/bluetooth_host_proxy.cpp @@ -85,7 +85,7 @@ int32_t BluetoothHostProxy::EnableBt() return reply.ReadInt32(); } -int32_t BluetoothHostProxy::DisableBt(bool isAsync) +int32_t BluetoothHostProxy::DisableBt(bool isAsync, const std::string &callingName) { HILOGI("BluetoothHostProxy::DisableBt starts"); MessageParcel data; @@ -97,6 +97,9 @@ int32_t BluetoothHostProxy::DisableBt(bool isAsync) HILOGE("BluetoothHostProxy::DisableBt WriteBool isAsync error"); return BT_ERR_IPC_TRANS_FAILED; } + if (!data.WriteString(callingName)) { + return BT_ERR_IPC_TRANS_FAILED; + } MessageParcel reply; MessageOption option = {MessageOption::TF_SYNC}; @@ -243,7 +246,7 @@ int32_t BluetoothHostProxy::DisableBle() return reply.ReadInt32(); } -int32_t BluetoothHostProxy::EnableBle(bool noAutoConnect, bool isAsync) +int32_t BluetoothHostProxy::EnableBle(bool noAutoConnect, bool isAsync, const std::string &callingName) { MessageParcel data; if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) { @@ -258,6 +261,9 @@ int32_t BluetoothHostProxy::EnableBle(bool noAutoConnect, bool isAsync) HILOGE("BluetoothHostProxy::EnableBle WriteBool isAsync error"); return BT_ERR_IPC_TRANS_FAILED; } + if (!data.WriteString(callingName)) { + return BT_ERR_IPC_TRANS_FAILED; + } MessageParcel reply; MessageOption option = {MessageOption::TF_SYNC}; @@ -1764,13 +1770,16 @@ void BluetoothHostProxy::UpdateVirtualDevice(int32_t action, const std::string & return; } -int32_t BluetoothHostProxy::EnableBluetoothToRestrictMode(void) +int32_t BluetoothHostProxy::EnableBluetoothToRestrictMode(const std::string &callingName) { MessageParcel data; CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor()), BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error"); MessageParcel reply; MessageOption option = {MessageOption::TF_SYNC}; + if (!data.WriteString(callingName)) { + return BT_ERR_IPC_TRANS_FAILED; + } int32_t error = InnerTransact( BluetoothHostInterfaceCode::BT_ENABLE_BLUETOOTH_TO_RESTRICT_MODE, option, data, reply); CHECK_AND_RETURN_LOG_RET((error == BT_NO_ERROR), BT_ERR_INTERNAL_ERROR, "error: %{public}d", error); diff --git a/frameworks/inner/src/bluetooth_host.cpp b/frameworks/inner/src/bluetooth_host.cpp index 575293c2..dfdda385 100644 --- a/frameworks/inner/src/bluetooth_host.cpp +++ b/frameworks/inner/src/bluetooth_host.cpp @@ -145,6 +145,23 @@ public: } } + void OnBluetoothSwitchAction(int32_t action, const std::string &callingName) override + { + HILOGI("bluetooth switch action: %{public}d, callingName: %{public}s", action, callingName.c_str()); + std::lock_guard lock(host_.switchModuleMutex_); + CHECK_AND_RETURN_LOG(host_.switchModule_, "switchModule is nullptr"); + if (action == bluetooth::TRANS_ACTION_ENABLE_BLUETOOTH) { + host_.switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::ENABLE_BLUETOOTH, callingName); + } + if (action == bluetooth::TRANS_ACTION_DISABLE_BLUETOOTH) { + host_.switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::DISABLE_BLUETOOTH, callingName); + } + if (action == bluetooth::TRANS_ACTION_ENABLE_BLUETOOTH_TO_RESTRICT_MODE) { + host_.switchModule_->ProcessBluetoothSwitchEvent( + BluetoothSwitchEvent::ENABLE_BLUETOOTH_TO_RESTRICE_MODE, callingName); + } + } + void OnDiscoveryStateChanged(int32_t status) override { HILOGD("enter, status: %{public}d", status); @@ -439,7 +456,7 @@ public: BluetoothSwitchAction() = default; ~BluetoothSwitchAction() override = default; - int EnableBluetooth(bool noAutoConnect, bool isAsync) override + int EnableBluetooth(bool noAutoConnect, std::string callingName, bool isAsync) override { CHECK_AND_RETURN_LOG_RET(!BluetoothHost::GetDefaultHost().IsBtProhibitedByEdm(), BT_ERR_PROHIBITED_BY_EDM, "bluetooth is prohibited !"); @@ -448,17 +465,17 @@ public: sptr proxy = GetRemoteProxy(BLUETOOTH_HOST); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr"); - return proxy->EnableBle(noAutoConnect, isAsync); + return proxy->EnableBle(noAutoConnect, isAsync, callingName); } - int DisableBluetooth(bool isAsync) override + int DisableBluetooth(std::string callingName, bool isAsync) override { sptr proxy = GetRemoteProxy(BLUETOOTH_HOST); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr"); - return proxy->DisableBt(isAsync); + return proxy->DisableBt(isAsync, callingName); } - int EnableBluetoothToRestrictMode(void) override + int EnableBluetoothToRestrictMode(std::string callingName) override { CHECK_AND_RETURN_LOG_RET(!BluetoothHost::GetDefaultHost().IsBtProhibitedByEdm(), BT_ERR_PROHIBITED_BY_EDM, "bluetooth is prohibited !"); @@ -467,7 +484,7 @@ public: sptr proxy = GetRemoteProxy(BLUETOOTH_HOST); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr"); - return proxy->EnableBluetoothToRestrictMode(); + return proxy->EnableBluetoothToRestrictMode(callingName); } }; @@ -640,12 +657,12 @@ int BluetoothHost::EnableBt() return proxy->EnableBt(); } -int BluetoothHost::DisableBt(bool isAsync) +int BluetoothHost::DisableBt(std::string name, bool isAsync) { HILOGI("enter"); std::lock_guard lock(pimpl->switchModuleMutex_); CHECK_AND_RETURN_LOG_RET(pimpl->switchModule_, BT_ERR_INTERNAL_ERROR, "switchModule is nullptr"); - return pimpl->switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::DISABLE_BLUETOOTH, isAsync); + return pimpl->switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::DISABLE_BLUETOOTH, name, isAsync); } static void PublishBtSwitchRestrictBluetoothEvent(void) @@ -665,17 +682,18 @@ static void PublishBtSwitchRestrictBluetoothEvent(void) } } -int BluetoothHost::RestrictBluetooth() +int BluetoothHost::RestrictBluetooth(std::string name) { HILOGI("enter"); std::lock_guard lock(pimpl->switchModuleMutex_); PublishBtSwitchRestrictBluetoothEvent(); CHECK_AND_RETURN_LOG_RET(pimpl->switchModule_, BT_ERR_INTERNAL_ERROR, "switchModule is nullptr"); - int ret = pimpl->switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::DISABLE_BLUETOOTH); + int ret = pimpl->switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::DISABLE_BLUETOOTH, name); if (ret != BT_NO_ERROR) { return ret; } - ret = pimpl->switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::ENABLE_BLUETOOTH_TO_RESTRICE_MODE); + ret = pimpl->switchModule_->ProcessBluetoothSwitchEvent( + BluetoothSwitchEvent::ENABLE_BLUETOOTH_TO_RESTRICE_MODE, name); return ret; } @@ -790,12 +808,12 @@ BluetoothRemoteDevice BluetoothHost::GetRemoteDevice(const std::string &addr, in return remoteDevice; } -int BluetoothHost::EnableBle(bool isAsync) +int BluetoothHost::EnableBle(std::string name, bool isAsync) { HILOGI("enter"); std::lock_guard lock(pimpl->switchModuleMutex_); CHECK_AND_RETURN_LOG_RET(pimpl->switchModule_, BT_ERR_INTERNAL_ERROR, "switchModule is nullptr"); - return pimpl->switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::ENABLE_BLUETOOTH, isAsync); + return pimpl->switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::ENABLE_BLUETOOTH, name, isAsync); } int BluetoothHost::EnableBluetoothNoAutoConnect() @@ -814,12 +832,13 @@ int BluetoothHost::DisableBle() return proxy->DisableBle(); } -int BluetoothHost::EnableBluetoothToRestrictMode(void) +int BluetoothHost::EnableBluetoothToRestrictMode(std::string name) { HILOGI("enter"); std::lock_guard lock(pimpl->switchModuleMutex_); CHECK_AND_RETURN_LOG_RET(pimpl->switchModule_, BT_ERR_INTERNAL_ERROR, "switchModule is nullptr"); - return pimpl->switchModule_->ProcessBluetoothSwitchEvent(BluetoothSwitchEvent::ENABLE_BLUETOOTH_TO_RESTRICE_MODE); + return pimpl->switchModule_->ProcessBluetoothSwitchEvent( + BluetoothSwitchEvent::ENABLE_BLUETOOTH_TO_RESTRICE_MODE, name); } bool BluetoothHost::IsBrEnabled() const diff --git a/frameworks/inner/src/bluetooth_switch_module.cpp b/frameworks/inner/src/bluetooth_switch_module.cpp index 2f9b30fd..2ce69d90 100644 --- a/frameworks/inner/src/bluetooth_switch_module.cpp +++ b/frameworks/inner/src/bluetooth_switch_module.cpp @@ -48,7 +48,8 @@ void BluetoothSwitchModule::LogBluetoothSwitchEvent(BluetoothSwitchEvent event) } } -int BluetoothSwitchModule::ProcessBluetoothSwitchEvent(BluetoothSwitchEvent event, bool isAsync) +int BluetoothSwitchModule::ProcessBluetoothSwitchEvent( + BluetoothSwitchEvent event, std::string callingName, bool isAsync) { CHECK_AND_RETURN_LOG_RET(switchAction_, BT_ERR_INTERNAL_ERROR, "switchAction is nullptr"); @@ -56,11 +57,11 @@ int BluetoothSwitchModule::ProcessBluetoothSwitchEvent(BluetoothSwitchEvent even LogBluetoothSwitchEvent(event); switch (event) { case BluetoothSwitchEvent::ENABLE_BLUETOOTH: - return ProcessEnableBluetoothEvent(isAsync); + return ProcessEnableBluetoothEvent(callingName, isAsync); case BluetoothSwitchEvent::DISABLE_BLUETOOTH: - return ProcessDisableBluetoothEvent(isAsync); + return ProcessDisableBluetoothEvent(callingName, isAsync); case BluetoothSwitchEvent::ENABLE_BLUETOOTH_TO_RESTRICE_MODE: - return ProcessEnableBluetoothToRestrictModeEvent(); + return ProcessEnableBluetoothToRestrictModeEvent(callingName); case BluetoothSwitchEvent::BLUETOOTH_ON: return ProcessBluetoothOnEvent(); case BluetoothSwitchEvent::BLUETOOTH_OFF: @@ -107,38 +108,41 @@ int BluetoothSwitchModule::ProcessBluetoothSwitchAction( isBtSwitchProcessing_ = false; ffrtQueue_.cancel(taskTimeoutHandle_); } - // Considering interface compatibility, when a thiry party app invokes the Bluetooth switch interface, - // a dialog box is displayed, indicating that the call is success. - if (ret == BT_ERR_DIALOG_FOR_USER_CONFIRM) { + // Considering interface compatibility, + // 1) when a thiry party app invokes the Bluetooth switch interface, a dialog box is displayed, + // indicating that the call is success. + // 2) when the switch operation of the system application is transferred to another module, + // the interface call is considered successful. + if (ret == BT_ERR_DIALOG_FOR_USER_CONFIRM || ret == BT_ERR_SWITCH_OP_TRANSFERRED) { ret = BT_NO_ERROR; } return ret; } -int BluetoothSwitchModule::ProcessEnableBluetoothEvent(bool isAsync) +int BluetoothSwitchModule::ProcessEnableBluetoothEvent(const std::string &callingName, bool isAsync) { return ProcessBluetoothSwitchAction( - [this, isAsync]() { + [this, callingName, isAsync]() { bool noAutoConnect = noAutoConnect_.load(); if (noAutoConnect) { SetNoAutoConnect(false); } - return switchAction_->EnableBluetooth(noAutoConnect, isAsync); + return switchAction_->EnableBluetooth(noAutoConnect, callingName, isAsync); }, BluetoothSwitchEvent::ENABLE_BLUETOOTH); } -int BluetoothSwitchModule::ProcessDisableBluetoothEvent(bool isAsync) +int BluetoothSwitchModule::ProcessDisableBluetoothEvent(const std::string &callingName, bool isAsync) { return ProcessBluetoothSwitchAction( - [this, isAsync]() { return switchAction_->DisableBluetooth(isAsync); }, + [this, callingName, isAsync]() { return switchAction_->DisableBluetooth(callingName, isAsync); }, BluetoothSwitchEvent::DISABLE_BLUETOOTH); } -int BluetoothSwitchModule::ProcessEnableBluetoothToRestrictModeEvent(void) +int BluetoothSwitchModule::ProcessEnableBluetoothToRestrictModeEvent(const std::string &callingName) { return ProcessBluetoothSwitchAction( - [this]() { return switchAction_->EnableBluetoothToRestrictMode(); }, + [this, callingName]() { return switchAction_->EnableBluetoothToRestrictMode(callingName); }, BluetoothSwitchEvent::ENABLE_BLUETOOTH_TO_RESTRICE_MODE); } diff --git a/frameworks/inner/src/bluetooth_switch_module.h b/frameworks/inner/src/bluetooth_switch_module.h index 90191dc3..ccd9cb1d 100644 --- a/frameworks/inner/src/bluetooth_switch_module.h +++ b/frameworks/inner/src/bluetooth_switch_module.h @@ -30,9 +30,9 @@ public: IBluetoothSwitchAction() = default; virtual ~IBluetoothSwitchAction() = default; - virtual int EnableBluetooth(bool, bool) = 0; - virtual int DisableBluetooth(bool) = 0; - virtual int EnableBluetoothToRestrictMode(void) = 0; + virtual int EnableBluetooth(bool, std::string, bool) = 0; + virtual int DisableBluetooth(std::string, bool) = 0; + virtual int EnableBluetoothToRestrictMode(std::string) = 0; }; enum class BluetoothSwitchEvent : int { @@ -51,13 +51,13 @@ public: : ffrtQueue_("bt_switch"), switchAction_(std::move(switchAction)) {} ~BluetoothSwitchModule() = default; - int ProcessBluetoothSwitchEvent(BluetoothSwitchEvent event, bool isAsync = false); + int ProcessBluetoothSwitchEvent(BluetoothSwitchEvent event, std::string callingName = "", bool isAsync = false); void SetNoAutoConnect(bool noAutoConnect); private: - int ProcessEnableBluetoothEvent(bool isAsync = false); - int ProcessDisableBluetoothEvent(bool isAsync = false); - int ProcessEnableBluetoothToRestrictModeEvent(void); + int ProcessEnableBluetoothEvent(const std::string &callingName, bool isAsync); + int ProcessDisableBluetoothEvent(const std::string &callingName, bool isAsync); + int ProcessEnableBluetoothToRestrictModeEvent(const std::string &callingName); int ProcessBluetoothOnEvent(void); int ProcessBluetoothOffEvent(void); int ProcessBluetoothHalfEvent(void); diff --git a/frameworks/js/napi/src/access/napi_bluetooth_access.cpp b/frameworks/js/napi/src/access/napi_bluetooth_access.cpp index 62a02201..2ebae858 100644 --- a/frameworks/js/napi/src/access/napi_bluetooth_access.cpp +++ b/frameworks/js/napi/src/access/napi_bluetooth_access.cpp @@ -276,7 +276,7 @@ napi_value NapiAccess::EnableBluetoothAsync(napi_env env, napi_callback_info inf HILOGI("enter"); auto func = []() { bool isAsync = true; - int32_t ret = BluetoothHost::GetDefaultHost().EnableBle(isAsync); + int32_t ret = BluetoothHost::GetDefaultHost().EnableBle("", isAsync); HILOGI("EnableBluetoothAsync ret: %{public}d", ret); return NapiAsyncWorkRet(ret); }; @@ -291,7 +291,7 @@ napi_value NapiAccess::DisableBluetoothAsync(napi_env env, napi_callback_info in HILOGI("enter"); auto func = []() { bool isAsync = true; - int32_t ret = BluetoothHost::GetDefaultHost().DisableBt(isAsync); + int32_t ret = BluetoothHost::GetDefaultHost().DisableBt("", isAsync); HILOGI("DisableBluetoothAsync ret: %{public}d", ret); return NapiAsyncWorkRet(ret); }; diff --git a/interfaces/inner_api/include/bluetooth_errorcode.h b/interfaces/inner_api/include/bluetooth_errorcode.h index c40eadd8..f40d2017 100644 --- a/interfaces/inner_api/include/bluetooth_errorcode.h +++ b/interfaces/inner_api/include/bluetooth_errorcode.h @@ -88,6 +88,7 @@ enum BtErrCode { // errorCode for inner BT_ERR_DIALOG_FOR_USER_CONFIRM = -100, + BT_ERR_SWITCH_OP_TRANSFERRED = -101, }; } // namespace Bluetooth } // namespace OHOS diff --git a/interfaces/inner_api/include/bluetooth_host.h b/interfaces/inner_api/include/bluetooth_host.h index 86f82950..9d5f73c8 100644 --- a/interfaces/inner_api/include/bluetooth_host.h +++ b/interfaces/inner_api/include/bluetooth_host.h @@ -341,12 +341,13 @@ public: /** * @brief Disable classic. * + * @param name Indicate the calling application to disable bluetooth. The default value is current application. * @param isAsync Indicate Async function flag. * @return Returns true if the operation is accepted; * returns false if the operation is rejected. * @since 6 */ - int DisableBt(bool isAsync = false); + int DisableBt(std::string name = "", bool isAsync = false); /** * @brief Get classic enable/disable state. @@ -404,12 +405,13 @@ public: /** * @brief Enable ble. * + * @param name Indicate the calling application to enable bluetooth. The default value is current application. * @param isAsync Indicate Async function flag. * @return Returns true if the operation is accepted; * returns false if the operation is rejected. * @since 6 */ - int EnableBle(bool isAsync = false); + int EnableBle(std::string name = "", bool isAsync = false); /** * @brief Enable bluetooth without AutoConnect. @@ -423,11 +425,12 @@ public: /** * @brief Enable bluetooth to restrict mode. * + * @param name Indicate the calling application to enable bluetooth. The default value is current application. * @return Returns BT_NO_ERROR if the operation is accepted; * returns others if the operation is rejected. * @since 12 */ - int EnableBluetoothToRestrictMode(void); + int EnableBluetoothToRestrictMode(std::string name = ""); /** * @brief Get br/edr enable/disable state. @@ -784,12 +787,12 @@ public: /** * @brief Restrict Bluetooth BR/EDR ability, just BLE ability available. * - * @param remoteAddr remote device addr. + * @param name Indicate the calling application to restrict bluetooth. The default value is current application. * @return Returns {@link BT_NO_ERROR} if the operation is successful; * returns an error code defined in {@link BtErrCode} otherwise. * @since 12 */ - int RestrictBluetooth(); + int RestrictBluetooth(std::string name = ""); /** * @brief update virtual device * -- Gitee