dwaan / homebridge-adb

Homebridge script to control remote ADB enabled Android device
MIT License
74 stars 14 forks source link

Device disconnected? Trying to reconnect. #73

Closed bslatyer closed 2 years ago

bslatyer commented 2 years ago

Describe the bug The Homebridge-ADB plugin does not want to connect to the device at all and has to be manually connected by me every time.

Expected behavior The Homebridge-ADB plugin is meant to connect to the device via ADB without having to manually connect it myself

Log output [17/02/2022, 10:22:59] [HomebridgeADB] Nvidia Shield - Device disconnected? Trying to reconnect.

Device (please complete the following information):

Additional context

dwaan commented 2 years ago

I need you to try this first. When your Shield is turned off, open terminal in Config UI X and run this commands:

Does it output something like SHIELD Android TV, or error: device 'deviceip' not found.

bslatyer commented 2 years ago

Just tried the command: adb -s x.x.x.x:5555 shell "getprop ro.product.model" and it failed with error: device 'x.x.x.x:5555' not found

Magna66 commented 2 years ago

I have the same issue exactly with Sharp Android TV, after a while from switching off the TV, that what I get [homebridge-adb] Sharp - Device disconnected? Trying to reconnect. WOL and WOW are on in TV settings, while the Android TV is connected via Ethernet, I checked my router and the TV is connected to the network, but somehow it got disconnected from the plugin. I added the following to startup script:

!/bin/sh

Update apt package index

apt-get update

Install adb, required by the homebrdige-adb plugin

apt-get install -y android-tools-adb android-tools-fastboot

Fix connection issues for the homebridge-adb plugin

adb connect xxx.xxx.x.xxx,

it helps when I restart the container, but does not help reconnecting back the plugin to the TV.

I run adb -s deviceip shell "getprop ro.product.model" and I got error: device 'x.x.x.x:5555' not found. It only reconnect after switching on the TV.

dwaan commented 2 years ago

Thank you for the report. Seems like the ADB connection to the device got disconnected completely after the device is OFF, and later on after the device is ON when the plug-ins is trying to established ADB connection to the device, it got rejected.

I have a possible work around, but to make sure can someone help me to debug this step below:

  1. Make sure the adb connected properly to your device.
  2. Turn off the device, wait few minutes to make sure the ADB connection is disconnected.
  3. In Homebridge server terminal run ‘adb -s deviceip shell "getprop ro.product.model"’ this should output error since the ADB connection is disconnected
  4. Turn on the device, wait few seconds.
  5. In Homebridge server terminal run ‘adb kill-server’ and then ‘adb start-server’. This supposed to restart ADB server in Homebridge server.
  6. If there’s no error, run ‘adb connect ipaddress’ to reconnect to the device.
  7. And finally run ‘adb -s deviceip shell "getprop ro.product.model"’ again to see if the ADB shell can run properly. Does this produce the same error?
Magna66 commented 2 years ago

Thank you Sir for your reply. In fact once the device is turned on, it reconnect directly by itself, and it take a while around 20 minutes after device is switched off to disconnect from device, and that is the mystery for me, as wow and wol are on, also the device is still connected to the router, thus I don't know why it disconnect from the adb plugin in Homebridge. Of course once the device is on all the other command run ‘adb -s deviceip shell "getprop ro.product.model"’ and ‘adb connect ipaddress’ works perfectly.

bslatyer commented 2 years ago

Hi @dwaan, I had a similar workaround to your one that I'm currently using. It seems to work fine as long as I don't close the application or reboot the host system.

Is this going to be fixed in a future update?

dwaan commented 2 years ago

I did an update to the plugins yesterday. I think one of the main problem is that the plugins doesn't have timeout when running ADB command. In most case it will result in very slow error output (in some case will took few minutes) when the device is disconnected (turned off). Since the ADB commands are running in serial, once one command failed to execute which usually took minutes to fail, the rest need to wait until it finish. I updated the plugins so that every ADB command will have a timeout (1 second by default).

Since the plugins won't be able to turn on the device with ADB command once the device is turned off, as suggested, I added WOL feature. Once the device is turned on by WOL, the plugins will then hopefully be able to connect using ADB. With that I hope it can resolve this issue.

Magna66 commented 2 years ago

Hi dwaan, I solved the problem by sideloading wakelock v3 to ensure that the device won't disconnect from the LAN. I went back to adb plugin version 1.4.12, which now is working fine and more stable. Plugins starting from version 1.5 have one common problem which is the playback sensor whenever I set to none and restart the container or the homebridge, it will auto go back to yes. As for the latest version 1.5.3 wol is working fine but with one problem which is once it on the device, the device will then auto shut off, and have to on it three times exactly for the device to remain on, and that will happen again with every restart to the container or the homebridge. As if the adb connection will stabilize after three times turning on the device. Also the opposite will happen if I on the device manually, then off it from homebridge, it will take three times to off the device to remain off. BTW adb connect command works fine after each restart. Thank you for all your efforts.