12. Find the range of real number $a$ such that for any real number $x$ and any $\theta \in\left[0, \frac{\pi}{2}\right]$, the following inequality always holds:
$$
(x+3+2 \sin \theta \cos \theta)^{2}+(x+a \sin \theta+a \cos \theta)^{2} \geqslant \frac{1}{8} .
$$
12. Convert the original inequality to $2 x^{2}+2 x[3+2 \sin \theta \cos \theta+a(\sin \theta+\cos \theta)]+(3+2 \sin \theta \cos \theta)^{2}+$ $a^{2}(\sin \theta+\cos \theta)^{2}-\frac{1}{8} \geqslant 0$. According to the problem, we should have $\Delta=4[3+2 \sin \theta \cos \theta+a(\sin \theta+\cos \theta)]^{2}-4 \times$ $2\left[(3+2 \sin \theta \cos \theta)^{2}+a^{2}(\sin \theta+\cos \theta)^{2}-\frac{1}{8}\right] \leqslant 0$. Simplifying, we get $(3+2 \sin \theta \cos \theta-a \sin \theta-$ $a \cos \theta)^{2} \geqslant \frac{1}{4}$, which implies $3+2 \sin \theta \cos \theta-a \sin \theta-a \cos \theta \geqslant \frac{1}{2}$ or $3+2 \sin \theta \cos \theta-a \sin \theta-a \cos \theta \leqslant$ $-\frac{1}{2}$. Therefore, $a \geqslant \frac{3+2 \sin \theta \cos \theta+\frac{1}{2}}{\sin \theta+\cos \theta}$ or $a \leqslant \frac{3+2 \sin \theta \cos \theta-\frac{1}{2}}{\sin \theta+\cos \theta}, \theta \in\left[0, \frac{\pi}{2}\right]$. Let $\sin \theta+$ $\cos \theta=t$, when $\theta \in\left[0, \frac{\pi}{2}\right], t \in[1, \sqrt{2}], \sin \theta \cos \theta=\frac{1}{2}\left(t^{2}-1\right)$. Then $\frac{3+2 \sin \theta \cos \theta+\frac{1}{2}}{\sin \theta+\cos \theta}=t+\frac{5}{2 t}$, and $f(t)=t+\frac{5}{2 t}$ is a decreasing function for $t \in[1, \sqrt{2}]$, so the maximum value is $f(1)=\frac{7}{2}$ when $t=1$, hence $a \geqslant f(1)=\frac{7}{2}$. And $\frac{3+2 \sin \theta \cos \theta-\frac{1}{2}}{\sin \theta+\cos \theta}=t+\frac{3}{2 t} \geqslant 2 \sqrt{\frac{3}{2}}=\sqrt{6}$, with equality when $t=\frac{\sqrt{6}}{2}$. Therefore, $a \leqslant \sqrt{6}$, so the range of $a$ is $a \leqslant \sqrt{6}$ or $a \geqslant \frac{7}{2}$.