1、新增了蓝牙连接成功后发送蓝牙的MAC地址

This commit is contained in:
Rdzleo 2026-02-10 15:16:17 +08:00
parent 94067d4adc
commit 1a48e387a8
2 changed files with 54 additions and 1 deletions

View File

@ -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;
}
// ============================================================
// 状态管理
// ============================================================

View File

@ -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_;