-
Notifications
You must be signed in to change notification settings - Fork 54
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
Comments
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(); |
@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..... 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);
} |
I don't know. Are you connected device by USB? Why connected by localhost IP? Screenbits.2023-11-10_145214.mp4 |
@wherewhere I used the localhost ip because this is the example you provided...in the github home page.... 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);
} |
Remove the |
What device are you using an emulator or a real device? |
@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? 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 |
@yungd1plomat @wherewhere hey guys, thanks for your response. 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 |
Mybe you need to know what's |
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.... :( |
So why you disposed the monitor? |
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:
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?
The text was updated successfully, but these errors were encountered: