[ASUS ZenFone 8] bnsmb:
During the development of my scripts to automatically install and configure the phone, the adb connection between the phone and the PC was often lost after restarting the phone for a reason unknown to me.

A workaround to re-establish the connection is to disconnect and reconnect the USB cable - but of course this is not a solution for an automatic installation of the phone.

That's why I was looking for a solution to solve this problem.

I finally found a solution: Resetting the USB port through which the phone is connected to the PC restored the adb connection.

So I wrote a small shell script for Linux that performs the reset:

reset_usb_port_for_phone.sh

The usage of the script is:

[ OmniRom 14 Dev - xtrnaw7@t15g /data/develop/android/scripts_on_linux ] $ ./reset_usb_port_for_phone.sh -h
reset_usb_port_for_phone.sh v1.0.1
Usage: reset_usb_port_for_phone [serial_number_of_the_phone] [force] [noreset|view]

Parameter:

serial_number_of_the phone the serial number of the phone for which the USB port should be reset
the default is the serial number hardcoded in the script
force reset the USB port even if it is not necessary
noreset|view only print the current status; do NOT reset the USB port
Using this parameter the script uses these return codes:
0 - there is a working adb connection to the phone
1 - the phone is in the bootloader
2 - the phone is in the fastbootd
9 - no phone with the given serial number found
>100 - an error occured

[ OmniRom 14 Dev - xtrnaw7@t15g /data/develop/android/scripts_on_linux ] $

The script uses the serial number of the phone to select the correct phone.

If the script is called without a parameter, it uses the hardcoded default serial number:

[ OmniRom 14 Dev - xtrnaw7@t15g /data/develop/android/scripts_on_linux ] $ grep ^DEFAULT_SERIAL_NUMBER ./reset_usb_port_for_phone.sh -h
DEFAULT_SERIAL_NUMBER="M6AIB760D0939LX"
[ OmniRom 14 Dev - xtrnaw7@t15g /data/develop/android/scripts_on_linux ] $

(You should of course change this value if you use the script)

The script uses a little program called usbreset to reset the USB port

You can either download the program from here, or download the source code from https://askubuntu.com/questions/645/how-do-you-reset-a-usb-device-from-the-command-line and compile it yourself:

A simple

cc usbreset.c -o usbreset

is sufficient to compile the source code; that should be possible on any Linux with an installed C compiler. The binary on my website was compiled in Fedora 39.

The binary usbreset must be available via PATH for the script.

reset_usb_port_for_phone.sh first checks whether the phone can be reached via fastboot or adb: If so, the reset is not necessary and is not carried out (use the parameter force to do a reset of the USB port anyway).

Example output of the script:

[ OmniRom 14 Dev - xtrnaw7@t15g /data/develop/android/test2 ] $ ./reset_usb_port_for_phone.sh
reset_usb_port_for_phone.sh v1.0.1
Resetting the USB port used for the phone whith the serial number "M6AIB760D0939LX" if necessary ...
Determine the USB port used for the phone ...
The USB port for the phone with the s/n "M6AIB760D0939LX" is "/dev/bus/usb/003/125" (ASUSTek Computer, Inc. Zenfone 8)
The phone with the serial number "M6AIB760D0939LX" is NOT available via adb
Now resetting the USB port "/dev/bus/usb/003/125" used for the phone ...
Resetting USB device /dev/bus/usb/003/125
Reset successful
The status of the phone is now:

M6AIB760D0939LX device

Successfully resetting the USB port for the phone
[ OmniRom 14 Dev - xtrnaw7@t15g /data/develop/android/test2 ] $

Spoiler content hidden.

Spoiler content hidden.

Spoiler content hidden.

Spoiler content hidden.

Notes

Note that the USB port used for the connection to the phone is dynamic and changes for example after rebooting the phone. In addition, the device identifier for the USB device also depends on the booted OS on the phone

Spoiler content hidden.

----