The easiest solution would probably be a shared monarch, with a shared currency. The ministries of foreign affairs, defence and finance would be under the King as well. A shared monarchy where each kingdom retains a separate parliament, which would send a committee to renegotiate the terms of the union periodically, this of course would be overseen by the monarch. Despite a common currency, fiscally each kingdom remains independent, under a customs union.
Of course this would require a stronger monarch than Joao VI, perhaps he is poisoned earlier, as many believe he was poisoned by arsenic. With a strongman like Pedro I/IV at the helm, he could probably make a union of some sorts work.