From f8686d388faee727d09053c26a4583ac722a9631 Mon Sep 17 00:00:00 2001 From: lee Date: Mon, 18 Aug 2025 16:45:31 +0800 Subject: [PATCH 1/6] 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 From 86e8c74f8f1684b5a70c519d830ceb48aed30851 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=84=A4=E4=B8=96=E7=8C=AA?= <1546432050@qq.com> Date: Mon, 18 Aug 2025 14:09:42 +0000 Subject: [PATCH 2/6] update frameworks/inner/src/bluetooth_host.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 愤世猪 <1546432050@qq.com> --- frameworks/inner/src/bluetooth_host.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/frameworks/inner/src/bluetooth_host.cpp b/frameworks/inner/src/bluetooth_host.cpp index dfdda385..d436324b 100644 --- a/frameworks/inner/src/bluetooth_host.cpp +++ b/frameworks/inner/src/bluetooth_host.cpp @@ -1292,6 +1292,7 @@ void BluetoothHost::Close(void) { std::lock_guard lock(pimpl->switchModuleMutex_); pimpl->switchModule_ = nullptr; + BluetoothProfileManager::ClearSystemAbility(); } int32_t BluetoothHost::UpdateCloudBluetoothDevice(const std::vector &cloudDevices) -- Gitee From dd72707834292b5a9f82ce18539d57633b04a481 Mon Sep 17 00:00:00 2001 From: "yangkuo (A)" <1041733144@qq.com> Date: Fri, 15 Aug 2025 17:56:58 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E6=94=AF=E6=8C=81L2HC=201.5M=E5=92=8C2.3M?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangkuo (A) <1041733144@qq.com> --- frameworks/inner/ipc/common/bt_def.h | 19 +- .../js/napi/include/napi_bluetooth_a2dp_src.h | 2 + .../napi/src/a2dp/napi_bluetooth_a2dp_src.cpp | 219 +++++++++++------- interfaces/inner_api/include/bluetooth_def.h | 19 +- 4 files changed, 171 insertions(+), 88 deletions(-) diff --git a/frameworks/inner/ipc/common/bt_def.h b/frameworks/inner/ipc/common/bt_def.h index c455c1e7..f422586f 100644 --- a/frameworks/inner/ipc/common/bt_def.h +++ b/frameworks/inner/ipc/common/bt_def.h @@ -1513,11 +1513,11 @@ enum A2dpUserCodecChannelMode : uint8_t { }; /** - * @brief A2dp codec bit rate enum. + * @brief A2dp codec L2HCST bit rate enum. * * @since 19 */ -enum A2dpUserCodecBitRate : uint32_t { +enum A2dpUserL2hcStCodecBitRate : uint32_t { A2DP_L2HCST_BIT_RATE_DEFAULT = 0x0, A2DP_L2HCST_BIT_RATE_320K = 0x01, A2DP_L2HCST_BIT_RATE_640K = 0x01 << 1, @@ -1531,6 +1531,21 @@ enum A2dpUserCodecBitRate : uint32_t { A2DP_L2HCST_BIT_RATE_480K = 0x01 << 9, }; +/** + * @brief A2dp codec L2HCV2 bit rate enum. + * + * @since 21 + */ +enum A2dpUserL2hcV2CodecBitRate : uint32_t { + A2DP_L2HCV2_BIT_RATE_DEFAULT = 0x0, + A2DP_L2HCV2_BIT_RATE_320K = 0x01, + A2DP_L2HCV2_BIT_RATE_640K = 0x01 << 1, + A2DP_L2HCV2_BIT_RATE_960K = 0x01 << 2, + A2DP_L2HCV2_BIT_RATE_AUTO_RATE = 0x01 << 3, + A2DP_L2HCV2_BIT_RATE_1500K = 0x01 << 4, + A2DP_L2HCV2_BIT_RATE_2300K = 0x01 << 5, +}; + /** * @brief A2dp codec frame length enum. * diff --git a/frameworks/js/napi/include/napi_bluetooth_a2dp_src.h b/frameworks/js/napi/include/napi_bluetooth_a2dp_src.h index 67b66d1f..9b432417 100644 --- a/frameworks/js/napi/include/napi_bluetooth_a2dp_src.h +++ b/frameworks/js/napi/include/napi_bluetooth_a2dp_src.h @@ -66,6 +66,8 @@ enum CodecBitRate { CODEC_BIT_RATE_640000 = 6, CODEC_BIT_RATE_960000 = 7, CODEC_BIT_RATE_ABR = 8, + CODEC_BIT_RATE_1500000 = 9, + CODEC_BIT_RATE_2300000 = 10, }; enum CodecFrameLength { diff --git a/frameworks/js/napi/src/a2dp/napi_bluetooth_a2dp_src.cpp b/frameworks/js/napi/src/a2dp/napi_bluetooth_a2dp_src.cpp index 3048c2b7..21577d10 100644 --- a/frameworks/js/napi/src/a2dp/napi_bluetooth_a2dp_src.cpp +++ b/frameworks/js/napi/src/a2dp/napi_bluetooth_a2dp_src.cpp @@ -37,50 +37,6 @@ std::shared_ptr NapiA2dpSource::observer_ = std::make_sh bool NapiA2dpSource::isRegistered_ = false; thread_local napi_ref g_napiProfile = nullptr; -const static std::map g_codecTypeMap = { - {CODEC_TYPE_SBC, A2DP_CODEC_TYPE_SBC_USER}, - {CODEC_TYPE_AAC, A2DP_CODEC_TYPE_AAC_USER}, - {CODEC_TYPE_L2HC, A2DP_CODEC_TYPE_L2HCV2_USER}, - {CODEC_TYPE_L2HCST, A2DP_CODEC_TYPE_L2HCST_USER}, - {CODEC_TYPE_LDAC, A2DP_CODEC_TYPE_LDAC_USER}, - {CODEC_TYPE_INVALID, A2DP_CODEC_TYPE_NONA2DP_USER}, -}; - -const static std::map g_codecBitsPerSampleMap = { - {CODEC_BITS_PER_SAMPLE_NONE, A2DP_SAMPLE_BITS_NONE_USER}, - {CODEC_BITS_PER_SAMPLE_16, A2DP_SAMPLE_BITS_16_USER}, - {CODEC_BITS_PER_SAMPLE_24, A2DP_SAMPLE_BITS_24_USER}, - {CODEC_BITS_PER_SAMPLE_32, A2DP_SAMPLE_BITS_32_USER}, -}; - -const static std::map g_codecChannelModeMap = { - {CODEC_CHANNEL_MODE_NONE, A2DP_CHANNEL_MODE_NONE_USER}, - {CODEC_CHANNEL_MODE_MONO, A2DP_SBC_CHANNEL_MODE_MONO_USER}, - {CODEC_CHANNEL_MODE_STEREO, A2DP_SBC_CHANNEL_MODE_STEREO_USER}, -}; - -const static std::map g_codecSampleRateMap = { - {CODEC_SAMPLE_RATE_NONE, A2DP_SAMPLE_RATE_NONE_USER}, - {CODEC_SAMPLE_RATE_44100, A2DP_SBC_SAMPLE_RATE_44100_USER}, - {CODEC_SAMPLE_RATE_48000, A2DP_L2HCV2_SAMPLE_RATE_48000_USER}, - {CODEC_SAMPLE_RATE_88200, A2DP_SAMPLE_RATE_NONE_USER}, - {CODEC_SAMPLE_RATE_96000, A2DP_L2HCV2_SAMPLE_RATE_96000_USER}, - {CODEC_SAMPLE_RATE_176400, A2DP_SAMPLE_RATE_NONE_USER}, - {CODEC_SAMPLE_RATE_192000, A2DP_SAMPLE_RATE_NONE_USER}, -}; - -const static std::map g_codecBitRateMap = { - {CODEC_BIT_RATE_96000, A2DP_L2HCST_BIT_RATE_96K}, - {CODEC_BIT_RATE_128000, A2DP_L2HCST_BIT_RATE_128K}, - {CODEC_BIT_RATE_192000, A2DP_L2HCST_BIT_RATE_192K}, - {CODEC_BIT_RATE_256000, A2DP_L2HCST_BIT_RATE_256K}, - {CODEC_BIT_RATE_320000, A2DP_L2HCST_BIT_RATE_320K}, - {CODEC_BIT_RATE_480000, A2DP_L2HCST_BIT_RATE_480K}, - {CODEC_BIT_RATE_640000, A2DP_L2HCST_BIT_RATE_640K}, - {CODEC_BIT_RATE_960000, A2DP_L2HCST_BIT_RATE_960K}, - {CODEC_BIT_RATE_ABR, A2DP_L2HCST_BIT_RATE_AUTO_RATE}, -}; - const static std::map g_codecFrameLengthMap = { {CODEC_FRAME_LENGTH_5MS, A2DP_L2HCST_FRAME_LENGTH_5MS}, {CODEC_FRAME_LENGTH_10MS, A2DP_L2HCST_FRAME_LENGTH_10MS}, @@ -128,6 +84,15 @@ const static std::map g_a2dpCodecBitRateMap = { {A2DP_L2HCST_BIT_RATE_AUTO_RATE, CODEC_BIT_RATE_ABR}, }; +const static std::map g_a2dpL2hcV2BitRateMap = { + {A2DP_L2HCV2_BIT_RATE_320K, CODEC_BIT_RATE_320000}, + {A2DP_L2HCV2_BIT_RATE_640K, CODEC_BIT_RATE_640000}, + {A2DP_L2HCV2_BIT_RATE_960K, CODEC_BIT_RATE_960000}, + {A2DP_L2HCV2_BIT_RATE_1500K, CODEC_BIT_RATE_1500000}, + {A2DP_L2HCV2_BIT_RATE_2300K, CODEC_BIT_RATE_2300000}, + {A2DP_L2HCV2_BIT_RATE_AUTO_RATE, CODEC_BIT_RATE_ABR}, +}; + const static std::map g_a2dpCodecFrameLengthMap = { {A2DP_L2HCST_FRAME_LENGTH_5MS, CODEC_FRAME_LENGTH_5MS}, {A2DP_L2HCST_FRAME_LENGTH_10MS, CODEC_FRAME_LENGTH_10MS}, @@ -359,6 +324,10 @@ napi_value CodecBitRateInit(napi_env env) CodecBitRate::CODEC_BIT_RATE_640000, "CODEC_BIT_RATE_640000"); SetNamedPropertyByInteger(env, codecBitRate, CodecBitRate::CODEC_BIT_RATE_960000, "CODEC_BIT_RATE_960000"); + SetNamedPropertyByInteger(env, codecBitRate, + CodecBitRate::CODEC_BIT_RATE_1500000, "CODEC_BIT_RATE_1500000"); + SetNamedPropertyByInteger(env, codecBitRate, + CodecBitRate::CODEC_BIT_RATE_2300000, "CODEC_BIT_RATE_2300000"); SetNamedPropertyByInteger(env, codecBitRate, CodecBitRate::CODEC_BIT_RATE_ABR, "CODEC_BIT_RATE_ABR"); return codecBitRate; @@ -618,41 +587,61 @@ napi_value NapiA2dpSource::DisableAbsoluteVolume(napi_env env, napi_callback_inf static void ConvertCodecType(A2dpCodecInfo &a2dpCodecInfo, int32_t codecType) { - auto iter = g_codecTypeMap.find(codecType); - if (iter != g_codecTypeMap.end()) { - a2dpCodecInfo.codecType = iter->second; + for (const auto& codecType_ : g_a2dpCodecTypeMap) { + if (codecType_.second == codecType) { + a2dpCodecInfo.codecType = codecType_.first; + break; + } } } static void ConvertCodecBitsPerSample(A2dpCodecInfo &a2dpCodecInfo, int32_t codecBitsPerSample) { - auto iter = g_codecBitsPerSampleMap.find(codecBitsPerSample); - if (iter != g_codecBitsPerSampleMap.end()) { - a2dpCodecInfo.bitsPerSample = iter->second; + for (const auto& bitsPerSample_ : g_a2dpCodecBitsPerSampleMap) { + if (bitsPerSample_.second == codecBitsPerSample) { + a2dpCodecInfo.bitsPerSample = bitsPerSample_.first; + break; + } } } static void ConvertCodecChannelMode(A2dpCodecInfo &a2dpCodecInfo, int32_t codecChannelMode) { - auto iter = g_codecChannelModeMap.find(codecChannelMode); - if (iter != g_codecChannelModeMap.end()) { - a2dpCodecInfo.channelMode = iter->second; + for (const auto& channelMode : g_a2dpCodecChannelModeMap) { + if (channelMode.second == codecChannelMode) { + a2dpCodecInfo.channelMode = channelMode.first; + break; + } } } static void ConvertCodecSampleRate(A2dpCodecInfo &a2dpCodecInfo, int32_t codecSampleRate) { - auto iter = g_codecSampleRateMap.find(codecSampleRate); - if (iter != g_codecSampleRateMap.end()) { - a2dpCodecInfo.sampleRate = iter->second; + for (const auto& sampleRate : g_a2dpCodecSampleRateMap) { + if (sampleRate.second == codecSampleRate) { + a2dpCodecInfo.sampleRate = sampleRate.first; + break; + } } } -static void ConvertCodecBitRate(A2dpCodecInfo &a2dpCodecInfo, int32_t codecBitRate) +static void ConvertL2hcStCodecBitRate(A2dpCodecInfo &a2dpCodecInfo, int32_t codecBitRate) { - auto iter = g_codecBitRateMap.find(codecBitRate); - if (iter != g_codecBitRateMap.end()) { - a2dpCodecInfo.codecSpecific4 = iter->second; + for (const auto& bitRate : g_a2dpCodecBitRateMap) { + if (bitRate.second == codecBitRate) { + a2dpCodecInfo.codecSpecific4 = bitRate.first; + break; + } + } +} + +static void ConvertL2hcV2BitRate(A2dpCodecInfo &a2dpCodecInfo, int32_t codecBitRate) +{ + for (const auto& bitRate : g_a2dpL2hcV2BitRateMap) { + if (bitRate.second == codecBitRate) { + a2dpCodecInfo.codecSpecific4 = bitRate.first; + break; + } } } @@ -698,6 +687,16 @@ static void ConvertCodecBitRateToCodecInfo(CodecInfo &codecInfo, int32_t codecBi } } +static void ConvertL2hcV2BitRateToCodecInfo(CodecInfo &codecInfo, int32_t codecBitRate) +{ + auto iter = g_a2dpL2hcV2BitRateMap.find(codecBitRate); + if (iter != g_a2dpL2hcV2BitRateMap.end()) { + codecInfo.codecBitRate = iter->second; + } else { + codecInfo.codecBitRate = CODEC_BIT_RATE_ABR; // default value is ABR + } +} + static void ConvertCodecInfoToJs(napi_env env, napi_value &object, const A2dpCodecInfo &a2dpCodecInfo) { // convert A2dpCodecInfo to CodecInfo @@ -706,7 +705,12 @@ static void ConvertCodecInfoToJs(napi_env env, napi_value &object, const A2dpCod ConvertCodecChannelModeToCodecInfo(codecInfo, a2dpCodecInfo.channelMode); ConvertCodecBitsPerSampleToCodecInfo(codecInfo, a2dpCodecInfo.bitsPerSample); ConvertCodecTypeToCodecInfo(codecInfo, a2dpCodecInfo.codecType); - ConvertCodecBitRateToCodecInfo(codecInfo, a2dpCodecInfo.codecSpecific4); + if (a2dpCodecInfo.codecType == A2DP_CODEC_TYPE_L2HCST_USER) { + ConvertCodecBitRateToCodecInfo(codecInfo, a2dpCodecInfo.codecSpecific4); + } else if (a2dpCodecInfo.codecType == A2DP_CODEC_TYPE_L2HCV2_USER) { + ConvertL2hcV2BitRateToCodecInfo(codecInfo, a2dpCodecInfo.codecSpecific4); + } + // convert CodecInfo to JS napi_value value = nullptr; napi_create_int32(env, codecInfo.codecType, &value); @@ -752,8 +756,8 @@ static void ConvertCodecBitsPerSampleVector(napi_env env, napi_value &object, in napi_set_element(env, codecBitsPerSampleArray, count, value); count++; } - if (static_cast(codecSampleRate) & A2dpUserCodecBitsPerSample::A2DP_SAMPLE_BITS_16_USER) { - napi_create_int32(env, CODEC_BITS_PER_SAMPLE_16, &value); + if (static_cast(codecSampleRate) & A2dpUserCodecBitsPerSample::A2DP_SAMPLE_BITS_32_USER) { + napi_create_int32(env, CODEC_BITS_PER_SAMPLE_32, &value); napi_set_element(env, codecBitsPerSampleArray, count, value); count++; } @@ -762,8 +766,8 @@ static void ConvertCodecBitsPerSampleVector(napi_env env, napi_value &object, in napi_set_element(env, codecBitsPerSampleArray, count, value); count++; } - if (static_cast(codecSampleRate) & A2dpUserCodecBitsPerSample::A2DP_SAMPLE_BITS_32_USER) { - napi_create_int32(env, CODEC_BITS_PER_SAMPLE_32, &value); + if (static_cast(codecSampleRate) & A2dpUserCodecBitsPerSample::A2DP_SAMPLE_BITS_16_USER) { + napi_create_int32(env, CODEC_BITS_PER_SAMPLE_16, &value); napi_set_element(env, codecBitsPerSampleArray, count, value); count++; } @@ -807,8 +811,8 @@ static void ConvertCodecSampleRateVector(napi_env env, napi_value &object, int32 napi_set_element(env, codecSampleRateArray, count, value); count++; } - if (static_cast(codecSampleRate) & A2dpUserCodecSampleRate::A2DP_SBC_SAMPLE_RATE_44100_USER) { - napi_create_int32(env, CODEC_SAMPLE_RATE_44100, &value); + if (static_cast(codecSampleRate) & A2dpUserCodecSampleRate::A2DP_L2HCV2_SAMPLE_RATE_96000_USER) { + napi_create_int32(env, CODEC_SAMPLE_RATE_96000, &value); napi_set_element(env, codecSampleRateArray, count, value); count++; } @@ -817,8 +821,8 @@ static void ConvertCodecSampleRateVector(napi_env env, napi_value &object, int32 napi_set_element(env, codecSampleRateArray, count, value); count++; } - if (static_cast(codecSampleRate) & A2dpUserCodecSampleRate::A2DP_L2HCV2_SAMPLE_RATE_96000_USER) { - napi_create_int32(env, CODEC_SAMPLE_RATE_96000, &value); + if (static_cast(codecSampleRate) & A2dpUserCodecSampleRate::A2DP_SBC_SAMPLE_RATE_44100_USER) { + napi_create_int32(env, CODEC_SAMPLE_RATE_44100, &value); napi_set_element(env, codecSampleRateArray, count, value); count++; } @@ -826,52 +830,91 @@ static void ConvertCodecSampleRateVector(napi_env env, napi_value &object, int32 napi_set_named_property(env, object, "codecSampleRateArray", codecSampleRateArray); } -static void ConvertCodecBitRateVector(napi_env env, napi_value &object, int32_t codecBitRate) +static void ConvertL2hcV2CodecBitRateVector(napi_env env, napi_value &object, int32_t codecBitRate) +{ + int count = 0; + napi_value value = nullptr; + napi_value codecBitRateArray = nullptr; + napi_create_array(env, &codecBitRateArray); + if (static_cast(codecBitRate) & A2dpUserL2hcV2CodecBitRate::A2DP_L2HCV2_BIT_RATE_AUTO_RATE) { + napi_create_int32(env, CODEC_BIT_RATE_ABR, &value); + napi_set_element(env, codecBitRateArray, count, value); + count++; + } + if (static_cast(codecBitRate) & A2dpUserL2hcV2CodecBitRate::A2DP_L2HCV2_BIT_RATE_320K) { + napi_create_int32(env, CODEC_BIT_RATE_320000, &value); + napi_set_element(env, codecBitRateArray, count, value); + count++; + } + if (static_cast(codecBitRate) & A2dpUserL2hcV2CodecBitRate::A2DP_L2HCV2_BIT_RATE_640K) { + napi_create_int32(env, CODEC_BIT_RATE_640000, &value); + napi_set_element(env, codecBitRateArray, count, value); + count++; + } + if (static_cast(codecBitRate) & A2dpUserL2hcV2CodecBitRate::A2DP_L2HCV2_BIT_RATE_960K) { + napi_create_int32(env, CODEC_BIT_RATE_960000, &value); + napi_set_element(env, codecBitRateArray, count, value); + count++; + } + if (static_cast(codecBitRate) & A2dpUserL2hcV2CodecBitRate::A2DP_L2HCV2_BIT_RATE_1500K) { + napi_create_int32(env, CODEC_BIT_RATE_1500000, &value); + napi_set_element(env, codecBitRateArray, count, value); + count++; + } + if (static_cast(codecBitRate) & A2dpUserL2hcV2CodecBitRate::A2DP_L2HCV2_BIT_RATE_2300K) { + napi_create_int32(env, CODEC_BIT_RATE_2300000, &value); + napi_set_element(env, codecBitRateArray, count, value); + count++; + } + napi_set_named_property(env, object, "codecBitRateArray", codecBitRateArray); +} + +static void ConvertL2hcStCodecBitRateVector(napi_env env, napi_value &object, int32_t codecBitRate) { int count = 0; napi_value value = nullptr; napi_value codecBitRateArray = nullptr; napi_create_array(env, &codecBitRateArray); - if (static_cast(codecBitRate) & A2dpUserCodecBitRate::A2DP_L2HCST_BIT_RATE_96K) { + if (static_cast(codecBitRate) & A2dpUserL2hcStCodecBitRate::A2DP_L2HCST_BIT_RATE_AUTO_RATE) { + napi_create_int32(env, CODEC_BIT_RATE_ABR, &value); + napi_set_element(env, codecBitRateArray, count, value); + count++; + } + if (static_cast(codecBitRate) & A2dpUserL2hcStCodecBitRate::A2DP_L2HCST_BIT_RATE_96K) { napi_create_int32(env, CODEC_BIT_RATE_96000, &value); napi_set_element(env, codecBitRateArray, count, value); count++; } - if (static_cast(codecBitRate) & A2dpUserCodecBitRate::A2DP_L2HCST_BIT_RATE_192K) { + if (static_cast(codecBitRate) & A2dpUserL2hcStCodecBitRate::A2DP_L2HCST_BIT_RATE_192K) { napi_create_int32(env, CODEC_BIT_RATE_192000, &value); napi_set_element(env, codecBitRateArray, count, value); count++; } - if (static_cast(codecBitRate) & A2dpUserCodecBitRate::A2DP_L2HCST_BIT_RATE_256K) { + if (static_cast(codecBitRate) & A2dpUserL2hcStCodecBitRate::A2DP_L2HCST_BIT_RATE_256K) { napi_create_int32(env, CODEC_BIT_RATE_256000, &value); napi_set_element(env, codecBitRateArray, count, value); count++; } - if (static_cast(codecBitRate) & A2dpUserCodecBitRate::A2DP_L2HCST_BIT_RATE_320K) { + if (static_cast(codecBitRate) & A2dpUserL2hcStCodecBitRate::A2DP_L2HCST_BIT_RATE_320K) { napi_create_int32(env, CODEC_BIT_RATE_320000, &value); napi_set_element(env, codecBitRateArray, count, value); count++; } - if (static_cast(codecBitRate) & A2dpUserCodecBitRate::A2DP_L2HCST_BIT_RATE_480K) { + if (static_cast(codecBitRate) & A2dpUserL2hcStCodecBitRate::A2DP_L2HCST_BIT_RATE_480K) { napi_create_int32(env, CODEC_BIT_RATE_480000, &value); napi_set_element(env, codecBitRateArray, count, value); count++; } - if (static_cast(codecBitRate) & A2dpUserCodecBitRate::A2DP_L2HCST_BIT_RATE_640K) { + if (static_cast(codecBitRate) & A2dpUserL2hcStCodecBitRate::A2DP_L2HCST_BIT_RATE_640K) { napi_create_int32(env, CODEC_BIT_RATE_640000, &value); napi_set_element(env, codecBitRateArray, count, value); count++; } - if (static_cast(codecBitRate) & A2dpUserCodecBitRate::A2DP_L2HCST_BIT_RATE_960K) { + if (static_cast(codecBitRate) & A2dpUserL2hcStCodecBitRate::A2DP_L2HCST_BIT_RATE_960K) { napi_create_int32(env, CODEC_BIT_RATE_960000, &value); napi_set_element(env, codecBitRateArray, count, value); count++; } - if (static_cast(codecBitRate) & A2dpUserCodecBitRate::A2DP_L2HCST_BIT_RATE_AUTO_RATE) { - napi_create_int32(env, CODEC_BIT_RATE_ABR, &value); - napi_set_element(env, codecBitRateArray, count, value); - count++; - } napi_set_named_property(env, object, "codecBitRateArray", codecBitRateArray); } @@ -892,7 +935,11 @@ static void ConvertCodecInfoListToJS(napi_env env, napi_value &object, const A2d ConvertCodecBitsPerSampleVector(env, object, a2dpCodecInfo.bitsPerSample); ConvertCodecChannelModeVector(env, object, a2dpCodecInfo.channelMode); ConvertCodecSampleRateVector(env, object, a2dpCodecInfo.sampleRate); - ConvertCodecBitRateVector(env, object, a2dpCodecInfo.codecSpecific3); + if (a2dpCodecInfo.codecType == A2DP_CODEC_TYPE_L2HCST_USER) { + ConvertL2hcStCodecBitRateVector(env, object, a2dpCodecInfo.codecSpecific3); + } else if (a2dpCodecInfo.codecType == A2DP_CODEC_TYPE_L2HCV2_USER) { + ConvertL2hcV2CodecBitRateVector(env, object, a2dpCodecInfo.codecSpecific3); + } if (a2dpCodecInfo.codecType == A2DP_CODEC_TYPE_L2HCST_USER || a2dpCodecInfo.codecType == A2DP_CODEC_TYPE_L2HCV2_USER) { ConvertCodecFrameLengthVector(env, object, a2dpCodecInfo.codecSpecific2); @@ -938,7 +985,11 @@ static napi_status CheckSetCodecPreferenceParam(napi_env env, napi_callback_info if (NapiIsObjectPropertyExist(env, argv[PARAM1], "codecBitRate")) { int32_t codecBitRate = 0; NAPI_BT_CALL_RETURN(NapiParseObjectInt32(env, argv[PARAM1], "codecBitRate", codecBitRate)); - ConvertCodecBitRate(a2dpCodecInfo, codecBitRate); + if (a2dpCodecInfo.codecType == A2DP_CODEC_TYPE_L2HCST_USER) { + ConvertL2hcStCodecBitRate(a2dpCodecInfo, codecBitRate); + } else if (a2dpCodecInfo.codecType == A2DP_CODEC_TYPE_L2HCV2_USER) { + ConvertL2hcV2BitRate(a2dpCodecInfo, codecBitRate); + } } return napi_ok; } diff --git a/interfaces/inner_api/include/bluetooth_def.h b/interfaces/inner_api/include/bluetooth_def.h index 31041701..6e31d242 100644 --- a/interfaces/inner_api/include/bluetooth_def.h +++ b/interfaces/inner_api/include/bluetooth_def.h @@ -1430,11 +1430,11 @@ enum A2dpUserCodecBitsPerSample : uint8_t { }; /** - * @brief A2dp codec bit rate enum. + * @brief A2dp codec L2HCST bit rate enum. * * @since 19 */ -enum A2dpUserCodecBitRate : uint32_t { +enum A2dpUserL2hcStCodecBitRate : uint32_t { A2DP_L2HCST_BIT_RATE_DEFAULT = 0x0, A2DP_L2HCST_BIT_RATE_320K = 0x01, A2DP_L2HCST_BIT_RATE_640K = 0x01 << 1, @@ -1448,6 +1448,21 @@ enum A2dpUserCodecBitRate : uint32_t { A2DP_L2HCST_BIT_RATE_480K = 0x01 << 9, }; +/** + * @brief A2dp codec L2HCV2 bit rate enum. + * + * @since 21 + */ +enum A2dpUserL2hcV2CodecBitRate : uint32_t { + A2DP_L2HCV2_BIT_RATE_DEFAULT = 0x0, + A2DP_L2HCV2_BIT_RATE_320K = 0x01, + A2DP_L2HCV2_BIT_RATE_640K = 0x01 << 1, + A2DP_L2HCV2_BIT_RATE_960K = 0x01 << 2, + A2DP_L2HCV2_BIT_RATE_AUTO_RATE = 0x01 << 3, + A2DP_L2HCV2_BIT_RATE_1500K = 0x01 << 4, + A2DP_L2HCV2_BIT_RATE_2300K = 0x01 << 5, +}; + /** * @brief A2dp codec frame length enum. * -- Gitee From 3c0e596e2b8cec718b0649c4c7f42855970b8313 Mon Sep 17 00:00:00 2001 From: linzlinz <2495670683@qq.com> Date: Wed, 20 Aug 2025 18:49:37 +0800 Subject: [PATCH 4/6] add gatt reason enum Signed-off-by: linzlinz <2495670683@qq.com> --- .../js/napi/include/napi_bluetooth_ble_utils.h | 1 + frameworks/js/napi/src/ble/napi_bluetooth_ble.cpp | 2 ++ .../js/napi/src/ble/napi_bluetooth_ble_utils.cpp | 15 +++++++++++++++ 3 files changed, 18 insertions(+) diff --git a/frameworks/js/napi/include/napi_bluetooth_ble_utils.h b/frameworks/js/napi/include/napi_bluetooth_ble_utils.h index 5a692ad5..ced77c70 100644 --- a/frameworks/js/napi/include/napi_bluetooth_ble_utils.h +++ b/frameworks/js/napi/include/napi_bluetooth_ble_utils.h @@ -72,6 +72,7 @@ napi_value ScanDutyInit(napi_env env); napi_value MatchModeInit(napi_env env); napi_value PhyTypeInit(napi_env env); napi_value ScanReportModeInit(napi_env env); +napi_value GattDisconnectReasonInit(napi_env env); void SetGattClientDeviceId(const std::string &deviceId); std::string GetGattClientDeviceId(); diff --git a/frameworks/js/napi/src/ble/napi_bluetooth_ble.cpp b/frameworks/js/napi/src/ble/napi_bluetooth_ble.cpp index 42f0cf8c..20802047 100644 --- a/frameworks/js/napi/src/ble/napi_bluetooth_ble.cpp +++ b/frameworks/js/napi/src/ble/napi_bluetooth_ble.cpp @@ -1216,6 +1216,7 @@ napi_value PropertyInit(napi_env env, napi_value exports) SetNamedPropertyByInteger(env, advertisingStateObj, static_cast(AdvertisingState::STOPPED), "STOPPED"); napi_value scanReportTypeObj = ScanReportTypeInit(env); + napi_value gattDisconnectReasonObj = GattDisconnectReasonInit(env); #endif napi_property_descriptor exportFuncs[] = { @@ -1228,6 +1229,7 @@ napi_value PropertyInit(napi_env env, napi_value exports) DECLARE_NAPI_PROPERTY("GattWriteType", gattWriteTypeObj), DECLARE_NAPI_PROPERTY("AdvertisingState", advertisingStateObj), DECLARE_NAPI_PROPERTY("ScanReportType", scanReportTypeObj), + DECLARE_NAPI_PROPERTY("GattDisconnectReason", gattDisconnectReasonObj), #endif }; diff --git a/frameworks/js/napi/src/ble/napi_bluetooth_ble_utils.cpp b/frameworks/js/napi/src/ble/napi_bluetooth_ble_utils.cpp index a4c81c8a..d5d0ad60 100644 --- a/frameworks/js/napi/src/ble/napi_bluetooth_ble_utils.cpp +++ b/frameworks/js/napi/src/ble/napi_bluetooth_ble_utils.cpp @@ -511,6 +511,21 @@ napi_value ScanReportModeInit(napi_env env) return reportModeObj; } +napi_value GattDisconnectReasonInit(napi_env env) +{ + napi_value gattDisconnectReasonObj = nullptr; + napi_create_object(env, &gattDisconnectReasonObj); + SetNamedPropertyByInteger(env, gattDisconnectReasonObj, + static_cast(GattDisconnectReason::CONN_TIMEOUT), "CONN_TIMEOUT"); + SetNamedPropertyByInteger(env, gattDisconnectReasonObj, + static_cast(GattDisconnectReason::CONN_TERMINATE_PEER_USER), "CONN_TERMINATE_PEER_USER"); + SetNamedPropertyByInteger(env, gattDisconnectReasonObj, + static_cast(GattDisconnectReason::CONN_TERMINATE_LOCAL_HOST), "CONN_TERMINATE_LOCAL_HOST"); + SetNamedPropertyByInteger(env, gattDisconnectReasonObj, + static_cast(GattDisconnectReason::CONN_UNKNOWN), "CONN_UNKNOWN"); + return gattDisconnectReasonObj; +} + void SetGattClientDeviceId(const std::string &deviceId) { deviceAddr = deviceId; -- Gitee From 81d98d362be0adfc0c37136ad5e95ba71b321f3b Mon Sep 17 00:00:00 2001 From: guoxiadi Date: Thu, 21 Aug 2025 15:49:48 +0800 Subject: [PATCH 5/6] add errmsg Bt Signed-off-by: guoxiadi --- frameworks/js/napi/src/common/napi_bluetooth_error.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/frameworks/js/napi/src/common/napi_bluetooth_error.cpp b/frameworks/js/napi/src/common/napi_bluetooth_error.cpp index 31550532..e29dc216 100644 --- a/frameworks/js/napi/src/common/napi_bluetooth_error.cpp +++ b/frameworks/js/napi/src/common/napi_bluetooth_error.cpp @@ -66,6 +66,8 @@ static std::map napiErrMsgMap { { BtErrCode::BT_ERR_GATT_CONNECTION_NOT_AUTHORIZED, "The connection is not authorized." }, { BtErrCode::BT_ERR_BLE_SCAN_NO_RESOURCE, "Fails to start scan as it is out of hardware resources."}, { BtErrCode::BT_ERR_BLE_SCAN_ALREADY_STARTED, "Failed to start scan as Ble scan is already started by the app."}, + { BtErrCode::BT_ERR_DIALOG_FOR_USER_NOT_RESPOND, "The user does not respond."}, + { BtErrCode::BT_ERR_DIALOG_FOR_USER_REFUSE, "User refuse the action."}, }; std::string GetNapiErrMsg(const napi_env &env, const int32_t errCode) -- Gitee From 9ce8091462ea815080052108b4eb48935910a7c8 Mon Sep 17 00:00:00 2001 From: linzlinz <2495670683@qq.com> Date: Thu, 21 Aug 2025 21:41:58 +0800 Subject: [PATCH 6/6] bugfix for napi cause Signed-off-by: linzlinz <2495670683@qq.com> --- frameworks/js/napi/include/napi_native_object.h | 12 ++++++------ .../src/ble/napi_bluetooth_gatt_client_callback.cpp | 2 +- .../src/ble/napi_bluetooth_gatt_server_callback.cpp | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/frameworks/js/napi/include/napi_native_object.h b/frameworks/js/napi/include/napi_native_object.h index 010b3189..6f1b7ff4 100644 --- a/frameworks/js/napi/include/napi_native_object.h +++ b/frameworks/js/napi/include/napi_native_object.h @@ -140,18 +140,18 @@ private: class NapiNativeStateChangeParam : public NapiNativeObject { public: NapiNativeStateChangeParam(std::string deviceAddr, int connectState, - bool isDisconnected = false, int disconnectReason = -1, int cause = 0) - : deviceAddr_(deviceAddr), connectState_(connectState), isDisconnected_(isDisconnected), - disconnectReason_(disconnectReason), stateChangeCause_(cause) {} + int cause = 0, bool isDisconnected = false, int disconnectReason = -1) + : deviceAddr_(deviceAddr), connectState_(connectState), stateChangeCause_(cause), + isDisconnected_(isDisconnected), disconnectReason_(disconnectReason) {} virtual ~NapiNativeStateChangeParam() override = default; napi_value ToNapiValue(napi_env env) const override; private: std::string deviceAddr_ = ""; int connectState_ = -1; + int stateChangeCause_ = -1; bool isDisconnected_ = false; int disconnectReason_ = -1; - int stateChangeCause_ = -1; }; class NapiNativeBleConnectionStateChangeParam : public NapiNativeStateChangeParam { @@ -159,8 +159,8 @@ public: NapiNativeBleConnectionStateChangeParam(std::string deviceAddr, int connectState) : NapiNativeStateChangeParam(deviceAddr, connectState) {} NapiNativeBleConnectionStateChangeParam( - std::string deviceAddr, int connectState, bool isDisconnected, int disconnectReason) - : NapiNativeStateChangeParam(deviceAddr, connectState, isDisconnected, disconnectReason) {} + std::string deviceAddr, int connectState, int cause, bool isDisconnected, int disconnectReason) + : NapiNativeStateChangeParam(deviceAddr, connectState, cause, isDisconnected, disconnectReason) {} ~NapiNativeBleConnectionStateChangeParam() override = default; }; diff --git a/frameworks/js/napi/src/ble/napi_bluetooth_gatt_client_callback.cpp b/frameworks/js/napi/src/ble/napi_bluetooth_gatt_client_callback.cpp index f8904600..80bf70ac 100644 --- a/frameworks/js/napi/src/ble/napi_bluetooth_gatt_client_callback.cpp +++ b/frameworks/js/napi/src/ble/napi_bluetooth_gatt_client_callback.cpp @@ -62,7 +62,7 @@ void NapiGattClientCallback::OnConnectionStateChangedWithReason(int connectionSt connectionState, disconnectReason, ret); if (connectionState == static_cast(BTConnectState::DISCONNECTED)) { auto nativeObject = std::make_shared( - deviceAddr_, connectionState, true, disconnectReason); + deviceAddr_, connectionState, 0, true, disconnectReason); eventSubscribe_.PublishEvent(STR_BT_GATT_CLIENT_CALLBACK_BLE_CONNECTIION_STATE_CHANGE, nativeObject); return; } diff --git a/frameworks/js/napi/src/ble/napi_bluetooth_gatt_server_callback.cpp b/frameworks/js/napi/src/ble/napi_bluetooth_gatt_server_callback.cpp index d84d681c..8d0a6046 100644 --- a/frameworks/js/napi/src/ble/napi_bluetooth_gatt_server_callback.cpp +++ b/frameworks/js/napi/src/ble/napi_bluetooth_gatt_server_callback.cpp @@ -88,7 +88,7 @@ void NapiGattServerCallback::OnConnectionStateUpdateWithReason( } } auto nativeObject = std::make_shared( - device.GetDeviceAddr(), state, true, disconnectReason); + device.GetDeviceAddr(), state, 0, true, disconnectReason); eventSubscribe_.PublishEvent(STR_BT_GATT_SERVER_CALLBACK_CONNECT_STATE_CHANGE, nativeObject); } } -- Gitee