SharpAdb / AdvancedSharpAdbClient

AdvancedSharpAdbClient is a .NET library that allows .NET, Mono and Unity applications to communicate with Android devices. It's improved version of SharpAdbClient.
https://sharpadb.github.io
Apache License 2.0
201 stars 54 forks source link

Devices Disconnect within 30 seconds #109

Closed jerry-synap closed 2 months ago

jerry-synap commented 3 months ago

Describe the bug

I connect to multiple devices using the code shown. The devices connect and are to be seen via 'adb devices' command, but they all disconnect around 15-30 seconds. I am using the devicemonitor and tracking the disconnects.

Steps to reproduce the bug

Try the following code :

namespace TestDeviceMonitor
{
    internal class Program
    {
        static int count = 0;
        static readonly AdbClient adbClient = new();
        const string SUBNET = "192.168.1.";
        static void Main(string[] args)
        {

            StartServerResult startServerResult = AdbServer.Instance.StartServer("adb.exe", true);
            Console.WriteLine(startServerResult);
            Console.WriteLine($"Current subnet is : {SUBNET}");

            var x = adbClient.GetDevices();
            Console.WriteLine(x.Count());

            using DeviceMonitor deviceMonitor = new(new AdbSocket(new IPEndPoint(IPAddress.Loopback, AdbClient.AdbServerPort)));
            deviceMonitor.DeviceConnected += DeviceMonitor_DeviceConnected;
            deviceMonitor.DeviceDisconnected += DeviceMonitor_DeviceDisconnected;
            deviceMonitor.DeviceChanged += (sender, e) => Console.WriteLine($"Device state changed: {e.Device} {e.OldState} -> {e.NewState}");
            deviceMonitor.Start();

            ScanNetworkForDevices();

            Console.Write("Press any key to exit...\n");
            Console.ReadKey(true);
            //AdbServer.Instance.RestartServer();
        }

        private static async Task ScanNetworkForDevices()
        {
            for (int i = 2; i < 254; i++)
            {
                _ = adbClient.ConnectAsync($"{SUBNET}{i}");
                Console.WriteLine($"Scanned {i}");
            }
        }

        private static void DeviceMonitor_DeviceDisconnected(object? sender, DeviceDataEventArgs e)
        {
            Console.WriteLine($"{DateTime.Now} Device Disconnected");
            if (e.Device.State == DeviceState.Offline) { Console.WriteLine(count--); }
        }

        private static void DeviceMonitor_DeviceConnected(object? sender, DeviceDataEventArgs e)
        {
            Console.WriteLine($"{DateTime.Now} Device Connected");
            //if (e.Device.State == DeviceState.Online) Console.WriteLine(count++);
        }
    }
}

Expected behavior

The devices should stay connected unless the device is unplugged, loses network or is intentionally disconnected.

Screenshots

image

NuGet package version

3.3.13

.NET Platform

.NET 8

Platform type

Windows

System version

Windows 11 Home , 23H2 OS Version : 22631.3958

IDE

Visual Studio 2022-preview, Visual Studio 2022

Additional context

I am testing the DeviceMonitor feature using a simple console app.

wherewhere commented 3 months ago

I have no idea, because it should be adb problem. Is that auto disconnect when you using adb directly?

jerry-synap commented 3 months ago

Try the same code using v2.5.2 and the devices will remain connected indefinitely, however, there is no connect-async but a normal connect. I have been trying this for a few hours and I notice most times they all disconnect around 15s.

Is that auto disconnect when you using adb directly?

wherewhere commented 3 months ago

It works on my device. image

deviceMonitor.DeviceConnected += (sender, e) => Console.WriteLine(e);
deviceMonitor.DeviceDisconnected += (sender, e) => Console.WriteLine(e);
deviceMonitor.DeviceChanged += (sender, e) => Console.WriteLine(e);
jerry-synap commented 3 months ago

Yes, I can see no disconnect after 15-30s like mine. Okay, I will dig deeper. WIll post my findings. Thanks for you trying it out and showing me it is local to me.

jerry-synap commented 3 months ago

Okay, I figured out my issue, because I am scanning the IP range from 2-254, and not all IP addresses have a device attached, during the loop, the devices that do respond become connected but the request to the first IP address of say 192.168.1.2 did not have a device attached will eventually respond between 15-18s with a failed to connect message. It is at this point all the devices are disconnected. I have tried connecting to all known "populated" IP addresses and they remain connected. To resolve my issue, I rewrote the scanning method to use "Process.Start" with a timeout of 500ms. It will scan continuously and because the AdbClient did not issue the connect command it ignores/or does not know about this failed response but I can still monitor the devices as they are connecting and disconnecting using the "DeviceMonitor" feature of the library The devices now remain connected. Thanks