mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	Shorten the ble name to prevent crash with long device names (#1920)
This commit is contained in:
		| @@ -91,7 +91,16 @@ bool ESP32BLE::ble_setup_() { | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   err = esp_ble_gap_set_device_name(App.get_name().c_str()); | ||||
|   std::string name = App.get_name(); | ||||
|   if (name.length() > 20) { | ||||
|     if (App.is_name_add_mac_suffix_enabled()) { | ||||
|       name.erase(name.begin() + 13, name.end() - 7);  // Remove characters between 13 and the mac address | ||||
|     } else { | ||||
|       name = name.substr(0, 20); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   err = esp_ble_gap_set_device_name(name.c_str()); | ||||
|   if (err != ESP_OK) { | ||||
|     ESP_LOGE(TAG, "esp_ble_gap_set_device_name failed: %d", err); | ||||
|     return false; | ||||
|   | ||||
| @@ -38,6 +38,7 @@ namespace esphome { | ||||
| class Application { | ||||
|  public: | ||||
|   void pre_setup(const std::string &name, const char *compilation_time, bool name_add_mac_suffix) { | ||||
|     this->name_add_mac_suffix_ = name_add_mac_suffix; | ||||
|     if (name_add_mac_suffix) { | ||||
|       this->name_ = name + "-" + get_mac_address().substr(6); | ||||
|     } else { | ||||
| @@ -97,6 +98,8 @@ class Application { | ||||
|   /// Get the name of this Application set by set_name(). | ||||
|   const std::string &get_name() const { return this->name_; } | ||||
|  | ||||
|   bool is_name_add_mac_suffix_enabled() const { return this->name_add_mac_suffix_; } | ||||
|  | ||||
|   const std::string &get_compilation_time() const { return this->compilation_time_; } | ||||
|  | ||||
|   /** Set the target interval with which to run the loop() calls. | ||||
| @@ -245,6 +248,7 @@ class Application { | ||||
|  | ||||
|   std::string name_; | ||||
|   std::string compilation_time_; | ||||
|   bool name_add_mac_suffix_; | ||||
|   uint32_t last_loop_{0}; | ||||
|   uint32_t loop_interval_{16}; | ||||
|   int dump_config_at_{-1}; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user