### Documentation of Changes and Features

Below is a summary of all modifications made to the Xiaozhi firmware, organized by functionality and affected files.

#### 1. Bluetooth Support

**Objective:** Add Bluetooth Low Energy (BLE) communication capability and an MCP to manage it.

**Modified/Created Files:**

*   **`C:/xiaozhi_firmware/xiaozhi-esp32/sdkconfig.defaults`**
    *   **Change:** Added `CONFIG_BT_ENABLED=y` and `CONFIG_BT_BLE_ENABLED=y` to enable Bluetooth support in the build configuration.
    *   **Purpose:** Activate Bluetooth functionalities at the system level.

*   **`C:/xiaozhi_firmware/xiaozhi-esp32/main/bluetooth_service.h`** (New file)
    *   **Change:** Declaration of the `BluetoothService` class with methods to initialize Bluetooth, start/stop scanning, get scanned devices, connect, and disconnect.
    *   **Purpose:** Encapsulate low-level Bluetooth logic to keep the code modular.

*   **`C:/xiaozhi_firmware/xiaozhi-esp32/main/bluetooth_service.cc`** (New file)
    *   **Change:** Implementation of the `BluetoothService` class, using ESP-IDF Bluetooth APIs (e.g., `esp_bt_controller_init`, `esp_bluedroid_init`, `esp_ble_gap_register_callback`). Includes a basic GAP event handler for scan results.
    *   **Purpose:** Provide the functional implementation of the Bluetooth service.

*   **`C:/xiaozhi_firmware/xiaozhi-esp32/main/CMakeLists.txt`**
    *   **Change:** Added `bluetooth_service.cc` to the list of sources to ensure its compilation.
    *   **Purpose:** Integrate the new service file into the firmware build process.

*   **`C:/xiaozhi_firmware/xiaozhi-esp32/main/application.cc`**
    *   **Change:** Added the call to `BluetoothService::GetInstance().Init()` in the `Application::Start()` function to initialize the Bluetooth service at application startup.
    *   **Purpose:** Ensure that the Bluetooth service is correctly initialized when the device boots.

*   **`C:/xiaozhi_firmware/xiaozhi-esp32/main/mcp_server.cc`**
    *   **Change:** Added the following MCPs:
        *   `self.bluetooth.start_scan`: Initiates BLE device scanning.
        *   `self.bluetooth.stop_scan`: Stops BLE device scanning.
        *   `self.bluetooth.get_scanned_devices`: Returns a JSON list of scanned BLE devices.
        *   `self.bluetooth.connect_device`: Connects to a BLE device by its address.
        *   `self.bluetooth.disconnect_device`: Disconnects from a BLE device by its address.
    *   **Purpose:** Expose Bluetooth functionalities to the MCP system, allowing AI models or other components to interact with Bluetooth.

#### Provided Functionality

Bluetooth integration allows the device to:

*   **Enable/Disable the Bluetooth module:** Through build configuration.
*   **Scan for BLE devices:** Discover Bluetooth Low Energy devices in the environment.
*   **Get a list of scanned devices:** Retrieve MAC addresses of detected devices.
*   **Start/Stop BLE connections:** Connect and disconnect from specific BLE devices (current implementation is basic and would need to be extended to handle complex GATT profiles).

#### Current Limitations and Considerations

*   **Basic BLE functionality:** The current implementation focuses on scanning and connection management at a fundamental level. It does not include the implementation of specific GATT (Generic Attribute Profile) profiles, such as services and characteristics for data communication. To interact with BLE devices that require custom profiles (e.g., sensor readings, actuator control), `BluetoothService` would need to be extended to implement the corresponding GATT client.
*   **Error handling:** Although basic error checks are used for ESP-IDF APIs, a robust production application would require more extensive error handling and recovery mechanisms for unexpected failures (e.g., hardware failures, data corruption).
*   **Power consumption:** Continuous BLE scanning can consume a significant amount of power. The current implementation initiates a 10-second scan; for low-power applications, more efficient scanning strategies would be needed.
