Example $\mathbf{3}$ Given the quadratic equation in $x$, $a(1+\mathrm{i}) x^{2}+\left(1+a^{2} \mathrm{i}\right) x+a^{2}+\mathrm{i}=0$, has real roots, find the value of the real number $a$.

The equation is $a(1+\mathrm{i}) x^{2}+\left(1+a^{2} \mathrm{i}\right) x+a^{2}+\mathrm{i}=0$.
Analysis: Let the real root of the equation be $x_{0}$, substitute it into the original equation, and according to the necessary and sufficient conditions for the equality of complex numbers, transform it into a real number problem to solve.

Solution: Let the real root of the equation be $x_{0}$, then $a(1+\mathrm{i}) x_{0}^{2}+\left(1+a^{2} \mathrm{i}\right) x_{0}+a^{2}+\mathrm{i}=0$, that is,
$$
\left(a x_{0}^{2}+x_{0}+a^{2}\right)+\left(a x_{0}^{2}+a^{2} x_{0}+1\right) \mathrm{i}=0 .
$$

According to the necessary and sufficient conditions for the equality of complex numbers, we get $\left\{\begin{array}{l}a x_{0}^{2}+x_{0}+a^{2}=0 \\ a x_{0}^{2}+a^{2} x_{0}+1=0\end{array}\right.$
(2) - (1), and rearrange, we get $\left(x_{0}-1\right)\left(a^{2}-1\right)=0$. Therefore, $x_{0}=1$ or $a^{2}=1$.

When $x_{0}=1$, substituting it into (1), we have $a^{2}+a+1=0$,
Clearly, this equation has no real roots, which contradicts $a \in \mathbf{R}$. Hence, $x_{0} \neq 1$.
When $a^{2}=1$, we have $a= \pm 1$. Similarly, $a=1$ is also not suitable. Therefore, $a=-1$, at this time $x_{0}=\frac{1 \pm \sqrt{5}}{2}$.
In summary, $a=-1$.