From ac5bd3e8dc1d47b89939dfdafc122ab4bbbf040b Mon Sep 17 00:00:00 2001 From: luozheng <1732036298@qq.com> Date: Thu, 27 Nov 2025 10:04:18 +0800 Subject: [PATCH] =?utf8?q?=E6=B7=BB=E5=8A=A0=E8=93=9D=E7=89=99=E8=AE=BE?= =?utf8?q?=E5=A4=87=E8=A7=A3=E7=BB=91=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../nativeui/bluetooth/src/BluetoothModel.cpp | 67 ++++++++++++++++++- .../bluetooth/src/Ln_Bluetooth_apiC.cpp | 12 ++++ .../include/bluetooth/BluetoothModel.h | 1 + .../include/bluetooth/Ln_Bluetooth_apiC.h | 4 ++ .../zhihui/leiniao/imsl_ln/leiniao_api_at.c | 4 +- code/sdk/zhihui/leiniao/imsl_ln/ln_api.c | 2 + 6 files changed, 86 insertions(+), 4 deletions(-) diff --git a/code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/BluetoothModel.cpp b/code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/BluetoothModel.cpp index 5f0887437..f5071ed8c 100644 --- a/code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/BluetoothModel.cpp +++ b/code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/BluetoothModel.cpp @@ -567,6 +567,69 @@ int BluetoothModel::UnBindLocalDevice(void) return ret; } +/* 解析蓝牙地址字符串为 bd_addr_t 结构 */ +static int parse_bt_address(const char *addr_str, bd_addr_t *addr) +{ + if (addr_str == NULL || addr == NULL) { + return 0; + } + + // 支持格式: "AA:BB:CC:DD:EE:FF" 或 "AABBCCDDEEFF" + unsigned int addr_bytes[6] = {0}; + int count = sscanf(addr_str, "%02X:%02X:%02X:%02X:%02X:%02X", + &addr_bytes[0], &addr_bytes[1], &addr_bytes[2], + &addr_bytes[3], &addr_bytes[4], &addr_bytes[5]); + + if (count != 6) { + // 尝试无冒号格式 + count = sscanf(addr_str, "%02X%02X%02X%02X%02X%02X", + &addr_bytes[0], &addr_bytes[1], &addr_bytes[2], + &addr_bytes[3], &addr_bytes[4], &addr_bytes[5]); + if (count != 6) { + return 0; + } + } + + // 蓝牙地址是反向存储的 + addr->addr[0] = (unsigned char)addr_bytes[5]; + addr->addr[1] = (unsigned char)addr_bytes[4]; + addr->addr[2] = (unsigned char)addr_bytes[3]; + addr->addr[3] = (unsigned char)addr_bytes[2]; + addr->addr[4] = (unsigned char)addr_bytes[1]; + addr->addr[5] = (unsigned char)addr_bytes[0]; + + return 1; +} + +// 解绑远程设备 +int BluetoothModel::UnBindRemoteDevice(const char *mac) +{ + if (mac == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "UnBindRemoteDevice mac is nullptr"); + return 0; + } + bd_addr_t addr = {0}; + if (parse_bt_address((char*)mac, &addr) != 1) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ERROR: Invalid Bluetooth address format"); + return ERRCODE_FAIL; + } + bool ret = gap_remove_pair(&addr); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "gap_remove_pair %d", ret); + if(ret == false) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "UnBindRemoteDevice gap_remove_pair ret = 0"); + char *bluetoothStatus = "UNBIND_FAILED"; + do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus)); + } + else + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "UnBindRemoteDevice gap_remove_pair ret = 1"); + char *bluetoothStatus = "UNBIND_SUCCESS"; + do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus)); + } + return ret; +} + // 设置绑定的本地设备MAC地址 void BluetoothModel::SetBoundDeviceMac(const char *mac) { @@ -720,8 +783,8 @@ void BtGapPairStatusChangedCallback(const bd_addr_t *bdAddr, int status) bdAddr->addr[3], bdAddr->addr[2], bdAddr->addr[1], bdAddr->addr[0], status); /* addr下标 0 1 2 3 */ if(status == GAP_PAIR_NONE){ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "取消配对"); - char *bluetoothStatus = "UNBIND_SUCCESS"; - do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus)); + // char *bluetoothStatus = "UNBIND_SUCCESS"; + // do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus)); } else if(status == GAP_PAIR_PAIRING){ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "正在配对"); } else if(status == GAP_PAIR_PAIRED){ diff --git a/code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/Ln_Bluetooth_apiC.cpp b/code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/Ln_Bluetooth_apiC.cpp index 7d0617a0e..512e61f86 100644 --- a/code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/Ln_Bluetooth_apiC.cpp +++ b/code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/Ln_Bluetooth_apiC.cpp @@ -184,6 +184,18 @@ extern "C" return 0; } + // 解绑远程设备 + int ln_bt_unbind_remote_device(const char *addr) + { + if (addr == NULL) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ln_bt_unbind_remote_device addr is NULL"); + return -1; + } + OHOS::BluetoothModel::GetInstance()->UnBindRemoteDevice(addr); + return 0; + } + // 获取绑定的本地设备MAC地址 char *ln_bt_get_bound_device_mac(void) { diff --git a/code/sdk/application/wearable/ln/nativeapp/nativeui/include/bluetooth/BluetoothModel.h b/code/sdk/application/wearable/ln/nativeapp/nativeui/include/bluetooth/BluetoothModel.h index cee1db3c9..70f3c9e74 100644 --- a/code/sdk/application/wearable/ln/nativeapp/nativeui/include/bluetooth/BluetoothModel.h +++ b/code/sdk/application/wearable/ln/nativeapp/nativeui/include/bluetooth/BluetoothModel.h @@ -90,6 +90,7 @@ public: int ConnectRemoteDevice(const bd_addr_t *addr); int DisconnectRemoteDevice(void); int UnBindLocalDevice(void); + int UnBindRemoteDevice(const char *mac); void SetBoundDeviceMac(const char *mac); void SetLastConnedDeviceName(const char *name); char *GetBoundDeviceMac(void); diff --git a/code/sdk/application/wearable/ln/nativeapp/nativeui/include/bluetooth/Ln_Bluetooth_apiC.h b/code/sdk/application/wearable/ln/nativeapp/nativeui/include/bluetooth/Ln_Bluetooth_apiC.h index 5751ec66d..c00385b78 100644 --- a/code/sdk/application/wearable/ln/nativeapp/nativeui/include/bluetooth/Ln_Bluetooth_apiC.h +++ b/code/sdk/application/wearable/ln/nativeapp/nativeui/include/bluetooth/Ln_Bluetooth_apiC.h @@ -21,6 +21,10 @@ int ln_bt_disconnect(void); // 解绑本地设备 int ln_bt_unbind(void); +// 解绑远程设备 +int ln_bt_unbind_remote_device(const char *addr); + + // 获取绑定的本地设备MAC地址 char *ln_bt_get_bound_device_mac(void); diff --git a/code/sdk/zhihui/leiniao/imsl_ln/leiniao_api_at.c b/code/sdk/zhihui/leiniao/imsl_ln/leiniao_api_at.c index 2c5bb2b7f..4fb245df0 100644 --- a/code/sdk/zhihui/leiniao/imsl_ln/leiniao_api_at.c +++ b/code/sdk/zhihui/leiniao/imsl_ln/leiniao_api_at.c @@ -1660,8 +1660,8 @@ static uint32_t ln_bluetooth_status_report_at(uint8_t *para, uint32_t paraLen, c } printf("+LN_BLUETOOTH_STATUS_REPORT: OK\n"); printf("[LN][BLUETOOTH_STATUS_REPORT] bluetoothStatus=%s\n", bluetoothStatus); - // do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus)); - push_bluetooth_connect_notification(bluetoothStatus); + do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus)); + // push_bluetooth_connect_notification(bluetoothStatus); return ERRCODE_SUCC; } /* 格式化打印蓝牙地址 */ diff --git a/code/sdk/zhihui/leiniao/imsl_ln/ln_api.c b/code/sdk/zhihui/leiniao/imsl_ln/ln_api.c index c2135c3b3..8932f3af7 100644 --- a/code/sdk/zhihui/leiniao/imsl_ln/ln_api.c +++ b/code/sdk/zhihui/leiniao/imsl_ln/ln_api.c @@ -20,6 +20,7 @@ #ifdef CUSTOM_UTILS #include "custom_utils.h" #endif +#include "bluetooth/Ln_Bluetooth_apiC.h" // 声明播放器消息处理函数 static app_task_definition_t ln_send_taskcfg = {0}; @@ -946,6 +947,7 @@ void ln_ws_process_message(const ws_data_msg_t *data_msg, ws_command_t type) ); /* TODO: 这里应该调用实际的蓝牙解绑接口,执行解绑操作 */ /* 解绑完成后,通过BluetoothStatusReport命令上报最终状态 */ + ln_bt_unbind_remote_device(unbind_data->bluetooth_address); } break; } -- 2.17.1