Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async listen to client #83

Open
davidlip123 opened this issue Nov 9, 2023 · 12 comments
Open

Async listen to client #83

davidlip123 opened this issue Nov 9, 2023 · 12 comments
Labels
help wanted Extra attention is needed

Comments

@davidlip123
Copy link

davidlip123 commented Nov 9, 2023

What can we do for you?

Hi, thanks for your aweomse library!
I need some help in async listening to the device connection... this is my code:

public MainWindow()
{
    InitializeComponent();

    StartAdbServer();

    var deviceMonitor = new DeviceMonitor(new AdbSocket("127.0.0.1", 62001));
    deviceMonitor.DeviceConnected += OnDeviceConnected;
    deviceMonitor.Start();
}

private void StartAdbServer()
{
    if (!AdbServer.Instance.GetStatus().IsRunning)
    {
        AdbServer server = new AdbServer();
        StartServerResult result = server.StartServer(@"C:\Users\Itamar\AppData\Local\Android\Sdk\platform-tools\adb.exe", false);
        if (result != StartServerResult.Started)
        {
            MessageBox.Show("Can't start adb server");
        }
        else
        {
            MessageBox.Show("started adb server");
        }
    }
}

But I'm getting error with that....
No connection could be made because the target machine actively refused it.

And there is device connected indeed...can you give me a clue about what I need to do?

@davidlip123 davidlip123 added the help wanted Extra attention is needed label Nov 9, 2023
@wherewhere
Copy link
Member

This is my code

using AdvancedSharpAdbClient;
using System;
using System.Collections.Generic;
using System.Net;

StartServerResult startServerResult = await AdbServer.Instance.StartServerAsync(@"C:\Program Files (x86)\Android\android-sdk\platform-tools\adb.exe", false, default);
Console.WriteLine(startServerResult);

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

Console.Write("Press any key to exit...");
Console.ReadKey(true);

await AdbServer.Instance.StopServerAsync(default);

And synchronous one

using AdvancedSharpAdbClient;
using System;
using System.Collections.Generic;
using System.Net;

StartServerResult startServerResult = AdbServer.Instance.StartServer(@"C:\Program Files (x86)\Android\android-sdk\platform-tools\adb.exe", false);
Console.WriteLine(startServerResult);

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

Console.Write("Press any key to exit...");
Console.ReadKey(true);

AdbServer.Instance.StopServer();

@davidlip123
Copy link
Author

davidlip123 commented Nov 9, 2023

@wherewhere thank you so much for response man ;)

I'm using this code:

public MainWindow()
{
    InitializeComponent();

    Start();
}

public async Task Start()
{
    StartServerResult startServerResult = await AdbServer.Instance.StartServerAsync(@"C:\Users\Itamar\AppData\Local\Android\Sdk\platform-tools\adb.exe", false, CancellationToken.None);
    Trace.WriteLine(startServerResult);

    await using DeviceMonitor deviceMonitor = new(new AdbSocket(new IPEndPoint(IPAddress.Loopback, AdbClient.AdbServerPort)));
    deviceMonitor.DeviceConnected += (sender, e) => Trace.WriteLine($"Device connected: {e.Device}"); // never reaches here
    deviceMonitor.DeviceDisconnected += (sender, e) => Trace.WriteLine($"Device disconnected: {e.Device}");
    deviceMonitor.DeviceChanged += (sender, e) => Trace.WriteLine($"Device state changed: {e.Device} {e.OldState} -> {e.NewState}");
    await deviceMonitor.StartAsync();

    // await AdbServer.Instance.StopServerAsync(default);
}

The server indeed starts,but it never triggers the DeviceConnected method..... tried few times...and nothing.....
any idea what's wrong here?

Using this sample,indeed works...but as I said I need to listen in the background and handle incoming connections...:

public static void getConnectedDevice()
{
    client = new AdbClient();
    client.ConnectAsync("127.0.0.1:62001");
    device = client.GetDevices().FirstOrDefault();

    Trace.WriteLine(device.Model);
}

@wherewhere
Copy link
Member

wherewhere commented Nov 10, 2023

I don't know. Are you connected device by USB? Why connected by localhost IP?

Screenbits.2023-11-10_145214.mp4

@davidlip123
Copy link
Author

davidlip123 commented Nov 10, 2023

@wherewhere I used the localhost ip because this is the example you provided...in the github home page....
of course I'm using usb.
Look,i've used nuget pm to install the AdvancedSharpAdbClient,maybe I need to clone the project and add it to my project,since there were changes from the last release?maybe it will solve the problem...?

edit:can you confirm that this code work for you, and the events indeed trigers....?

public MainWindow()
{
    InitializeComponent();

    Start();
}

public async Task Start()
{
    StartServerResult startServerResult = await AdbServer.Instance.StartServerAsync(@"C:\Users\Itamar\AppData\Local\Android\Sdk\platform-tools\adb.exe", false, CancellationToken.None);
    Trace.WriteLine(startServerResult); // result is already running

    await using DeviceMonitor deviceMonitor = new(new AdbSocket(new IPEndPoint(IPAddress.Loopback, AdbClient.AdbServerPort)));
    deviceMonitor.DeviceConnected += (sender, e) => Trace.WriteLine($"Device connected: {e.Device}"); // never reaches here
    deviceMonitor.DeviceDisconnected += (sender, e) => Trace.WriteLine($"Device disconnected: {e.Device}");
    deviceMonitor.DeviceChanged += (sender, e) => Trace.WriteLine($"Device state changed: {e.Device} {e.OldState} -> {e.NewState}");
    await deviceMonitor.StartAsync();

    // await AdbServer.Instance.StopServerAsync(default);
}

@wherewhere
Copy link
Member

Remove the using of DeviceMonitor. You just disposed it when Start method is done.

@yungd1plomat
Copy link
Member

What device are you using an emulator or a real device?

@davidlip123
Copy link
Author

davidlip123 commented Nov 11, 2023

@wherewhere @yungd1plomat ,I'm using real usb connected device...tried on few devices....different chipsets and android versions....and none....

I tried to create a console app in visual studio , and your code indeed works....but When I try to add it to my wpf app....there are no results at all..... what the hell is wrong here?
And another question,When I try to get the device name/ model when in the console app when the DeviceConnected event triggers...I'm getting blank output.... why is it?

deviceMonitor.DeviceConnected += (sender, e) => Trace.WriteLine($"Device connected: {e.Device.Name}");

@yungd1plomat
Copy link
Member

yungd1plomat commented Nov 11, 2023

@wherewhere @yungd1plomat ,I'm using real usb connected device...tried on few devices....different chipsets and android versions....and none....

I tried to create a console app in visual studio , and your code indeed works....but When I try to add it to my wpf app....there are no results at all..... what the hell is wrong here? And another question,When I try to get the device name/ model when in the console app when the DeviceConnected event triggers...I'm getting blank output.... why is it?

deviceMonitor.DeviceConnected += (sender, e) => Trace.WriteLine($"Device connected: {e.Device.Name}");

Because track-devices only sends the device serial. If you want to get the device model and name you can use:

var client = new AdbClient();
await using DeviceMonitor deviceMonitor = new(
    new AdbSocket(new IPEndPoint(IPAddress.Loopback, AdbClient.AdbServerPort)));
deviceMonitor.DeviceConnected += async (s, deviceData) =>
{
    var devices = await client.GetDevicesAsync();
    var device = devices.First(d => d.Serial == deviceData.Device.Serial);
    Trace.WriteLine($"Device connected: {device.Model}");
};
await deviceMonitor.StartAsync();

Or you can directly monitor when device connected like this:

var client = new AdbClient();
var devices = await client.GetDevicesAsync();
while (!devices.Any(x => x.State == DeviceState.Online)) 
{
    Trace.WriteLine("Waiting for device connection..");
    devices = await client.GetDevicesAsync();
    await Task.Delay(1000);
}
var device = devices.FirstOrDefault();
Trace.WriteLine($"Connected {device.Name}");

and you don't need to use client.ConnectAsync("127.0.0.1:62001"); because the real device is connected automatically

@davidlip123
Copy link
Author

davidlip123 commented Nov 29, 2023

@yungd1plomat @wherewhere hey guys, thanks for your response.
This is the current and the only working solution for me:

public async Task Start()
{
    StartAdbServer();
    var client = new AdbClient();
    var devices = await client.GetDevicesAsync();
    while (!devices.Any(x => x.State == DeviceState.Online))
    {
         Trace.WriteLine("Waiting for device connection..");
         devices = await client.GetDevicesAsync();
         await Task.Delay(600);      
    }
    var device = devices.FirstOrDefault();
    String model = device.Model;
}

@yungd1plomat your first example, using the deviceMonitor.DeviceConnected event didn't work for me, and that event never triggers, feel free to try it yourself, in and wpf app (not in a console).
I need to register a DeviceDisconnected event.... is there a solution for me? (Again, in wpf app :))
Thank you so so much for your kind response sir!

@wherewhere
Copy link
Member

Mybe you need to know what's using and what's await...

@davidlip123
Copy link
Author

Mybe you need to know what's using and what's await...

I know sir...I'm developing few years in c#...I know await uses for async functions,and using is for resources managment.... it doesn't answer my question..... you are invited to try it yourself and see that I'm right.... :(

@wherewhere
Copy link
Member

wherewhere commented Nov 29, 2023

public async Task Start()
{
    StartServerResult startServerResult = await AdbServer.Instance.StartServerAsync(@"C:\Users\Itamar\AppData\Local\Android\Sdk\platform-tools\adb.exe", false, CancellationToken.None);
    Trace.WriteLine(startServerResult); // result is already running

    await using DeviceMonitor deviceMonitor = new(new AdbSocket(new IPEndPoint(IPAddress.Loopback, AdbClient.AdbServerPort)));
    deviceMonitor.DeviceConnected += (sender, e) => Trace.WriteLine($"Device connected: {e.Device}"); // never reaches here
    deviceMonitor.DeviceDisconnected += (sender, e) => Trace.WriteLine($"Device disconnected: {e.Device}");
    deviceMonitor.DeviceChanged += (sender, e) => Trace.WriteLine($"Device state changed: {e.Device} {e.OldState} -> {e.NewState}");
    await deviceMonitor.StartAsync();

    // await AdbServer.Instance.StopServerAsync(default);
}

So why you disposed the monitor?

This was unlinked from pull requests Dec 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants