Thank you very much for your help.
I tried to use adb shell pidof <package_name> and it worked out perfectly to monitor if the game program exits or not, now I can achieve automatic restoration of the default configuration after exit.
At the moment I still need to work out how to automatically connect to adb after a device reboot and about how Android devices can enable the mDNS service to read ports automatically, this will take some time and I'm looking for github projects like this one
Translated with DeepL.com (free version)