From 1a48e387a8cc656f560efc2c1690b57cd659d98f Mon Sep 17 00:00:00 2001 From: Rdzleo Date: Tue, 10 Feb 2026 15:16:17 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E6=96=B0=E5=A2=9E=E4=BA=86=E8=93=9D?= =?UTF-8?q?=E7=89=99=E8=BF=9E=E6=8E=A5=E6=88=90=E5=8A=9F=E5=90=8E=E5=8F=91?= =?UTF-8?q?=E9=80=81=E8=93=9D=E7=89=99=E7=9A=84MAC=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/bluetooth_provisioning.cc | 53 +++++++++++++++++++++++++++++++++- main/bluetooth_provisioning.h | 2 ++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/main/bluetooth_provisioning.cc b/main/bluetooth_provisioning.cc index 61b515f..92c5835 100644 --- a/main/bluetooth_provisioning.cc +++ b/main/bluetooth_provisioning.cc @@ -133,7 +133,8 @@ BluetoothProvisioning::BluetoothProvisioning() , initialized_(false) , delayed_disconnect_(false) , wifi_connecting_(false) - , mac_address_sent_(false) { + , mac_address_sent_(false) + , ble_mac_sent_(false) { wifi_credentials_.ssid.clear(); wifi_credentials_.password.clear(); @@ -713,6 +714,11 @@ void BluetoothProvisioning::HandleGattsEvent(esp_gatts_cb_event_t event, uint16_t cccd_value = param->write.value[0] | (param->write.value[1] << 8); notify_enabled_ = (cccd_value == 0x0001); ESP_LOGI(TAG, "NOTIFY %s", notify_enabled_ ? "已启用" : "已禁用"); + + // NOTIFY启用后立即发送BLE MAC地址,确保iOS/Android都能获取 + if (notify_enabled_) { + SendBleMacAddress(); + } } if (param->write.need_rsp) { @@ -1092,9 +1098,54 @@ bool BluetoothProvisioning::SendMacAddressReliably() { void BluetoothProvisioning::ResetMacSendingState() { mac_address_sent_ = false; + ble_mac_sent_ = false; ESP_LOGI(TAG, "MAC地址发送状态已重置"); } +// ============================================================ +// SendBleMacAddress — 通过 GATT NOTIFY 发送蓝牙MAC地址 +// 在客户端启用NOTIFY后立即调用,确保iOS/Android都能获取BLE MAC +// ============================================================ + +bool BluetoothProvisioning::SendBleMacAddress() { + if (!client_connected_ || !notify_enabled_) { + ESP_LOGW(TAG, "无法发送BLE MAC: connected=%d, notify=%d", + client_connected_, notify_enabled_); + return false; + } + + if (ble_mac_sent_) { + ESP_LOGI(TAG, "BLE MAC已发送过,跳过"); + return true; + } + + const uint8_t* ble_addr = esp_bt_dev_get_address(); + if (!ble_addr) { + ESP_LOGE(TAG, "获取BLE MAC地址失败"); + return false; + } + + // 构建数据包: 0x84 + "BLE_MAC:xx:xx:xx:xx:xx:xx" + char mac_data[32]; + snprintf(mac_data, sizeof(mac_data), "BLE_MAC:%02x:%02x:%02x:%02x:%02x:%02x", + ble_addr[0], ble_addr[1], ble_addr[2], + ble_addr[3], ble_addr[4], ble_addr[5]); + + uint8_t buf[1 + 32]; + buf[0] = PROV_RESP_CUSTOM_DATA; + uint8_t data_len = strlen(mac_data); + memcpy(&buf[1], mac_data, data_len); + + if (SendNotify(buf, 1 + data_len)) { + ble_mac_sent_ = true; + ESP_LOGI(TAG, "✅ BLE MAC地址发送成功: %s", mac_data); + return true; + } + + ESP_LOGW(TAG, "❌ BLE MAC地址发送失败"); + return false; +} + // ============================================================ // 状态管理 // ============================================================ diff --git a/main/bluetooth_provisioning.h b/main/bluetooth_provisioning.h index 1a587b7..9e1ad38 100644 --- a/main/bluetooth_provisioning.h +++ b/main/bluetooth_provisioning.h @@ -126,6 +126,7 @@ public: void ReportWiFiStatus(bool success, uint8_t reason = 0); void SendWiFiList(const wifi_ap_record_t* ap_list, uint16_t ap_count); bool SendMacAddressReliably(); + bool SendBleMacAddress(); void ResetMacSendingState(); private: @@ -137,6 +138,7 @@ private: bool delayed_disconnect_; bool wifi_connecting_; bool mac_address_sent_; + bool ble_mac_sent_; static BluetoothProvisioning* instance_;