From 5e3d8260f160bda0e0a81635872f71797c1553f9 Mon Sep 17 00:00:00 2001 From: shamork Date: Wed, 5 Jun 2024 12:02:41 +0800 Subject: [PATCH 1/2] When connecting devices, prioritize the use of transport_id --- AdvancedSharpAdbClient.Tests/Dummys/DummyAdbSocket.cs | 5 ++++- AdvancedSharpAdbClient/AdbSocket.Async.cs | 5 ++++- AdvancedSharpAdbClient/AdbSocket.cs | 5 ++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/AdvancedSharpAdbClient.Tests/Dummys/DummyAdbSocket.cs b/AdvancedSharpAdbClient.Tests/Dummys/DummyAdbSocket.cs index d877cad..36c35db 100644 --- a/AdvancedSharpAdbClient.Tests/Dummys/DummyAdbSocket.cs +++ b/AdvancedSharpAdbClient.Tests/Dummys/DummyAdbSocket.cs @@ -146,7 +146,10 @@ public void SetDevice(DeviceData device) // to a specific device if (device != null) { - SendAdbRequest($"host:transport:{device.Serial}"); + if(!string.IsNullOrWhiteSpace(device.TransportId) && uint.TryParse(device.TransportId,out var tid)) + SendAdbRequest($"host:transport-id:{tid}"); + else + SendAdbRequest($"host:transport:{device.Serial}"); try { diff --git a/AdvancedSharpAdbClient/AdbSocket.Async.cs b/AdvancedSharpAdbClient/AdbSocket.Async.cs index acaef12..6334e7f 100644 --- a/AdvancedSharpAdbClient/AdbSocket.Async.cs +++ b/AdvancedSharpAdbClient/AdbSocket.Async.cs @@ -262,7 +262,10 @@ public async Task SetDeviceAsync(DeviceData device, CancellationToken cancellati // to a specific device if (device != null) { - await SendAdbRequestAsync($"host:transport:{device.Serial}", cancellationToken).ConfigureAwait(false); + if(!string.IsNullOrWhiteSpace(device.TransportId) && uint.TryParse(device.TransportId,out var tid)) + await SendAdbRequestAsync($"host:transport-id:{tid}", cancellationToken).ConfigureAwait(false); + else + await SendAdbRequestAsync($"host:transport:{device.Serial}", cancellationToken).ConfigureAwait(false); try { diff --git a/AdvancedSharpAdbClient/AdbSocket.cs b/AdvancedSharpAdbClient/AdbSocket.cs index a063044..b3a16dd 100644 --- a/AdvancedSharpAdbClient/AdbSocket.cs +++ b/AdvancedSharpAdbClient/AdbSocket.cs @@ -401,7 +401,10 @@ public void SetDevice(DeviceData device) // to a specific device if (device != null) { - SendAdbRequest($"host:transport:{device.Serial}"); + if(!string.IsNullOrWhiteSpace(device.TransportId) && uint.TryParse(device.TransportId,out var tid)) + SendAdbRequest($"host:transport-id:{tid}"); + else + SendAdbRequest($"host:transport:{device.Serial}"); try { From c5ab21ab9c238500bd51f7fb0e5fbece3278b03f Mon Sep 17 00:00:00 2001 From: wherewhere Date: Wed, 5 Jun 2024 15:23:43 +0800 Subject: [PATCH 2/2] Set DeviceData empty to TransportId and Serial all empty --- AdvancedSharpAdbClient.Tests/Dummys/DummyAdbSocket.cs | 6 +++++- AdvancedSharpAdbClient/AdbSocket.Async.cs | 7 +++---- AdvancedSharpAdbClient/AdbSocket.cs | 6 +++++- AdvancedSharpAdbClient/Models/DeviceData.cs | 8 ++++---- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/AdvancedSharpAdbClient.Tests/Dummys/DummyAdbSocket.cs b/AdvancedSharpAdbClient.Tests/Dummys/DummyAdbSocket.cs index 36c35db..2821b76 100644 --- a/AdvancedSharpAdbClient.Tests/Dummys/DummyAdbSocket.cs +++ b/AdvancedSharpAdbClient.Tests/Dummys/DummyAdbSocket.cs @@ -146,10 +146,14 @@ public void SetDevice(DeviceData device) // to a specific device if (device != null) { - if(!string.IsNullOrWhiteSpace(device.TransportId) && uint.TryParse(device.TransportId,out var tid)) + if (uint.TryParse(device.TransportId, out uint tid)) + { SendAdbRequest($"host:transport-id:{tid}"); + } else + { SendAdbRequest($"host:transport:{device.Serial}"); + } try { diff --git a/AdvancedSharpAdbClient/AdbSocket.Async.cs b/AdvancedSharpAdbClient/AdbSocket.Async.cs index 6334e7f..367516e 100644 --- a/AdvancedSharpAdbClient/AdbSocket.Async.cs +++ b/AdvancedSharpAdbClient/AdbSocket.Async.cs @@ -262,10 +262,9 @@ public async Task SetDeviceAsync(DeviceData device, CancellationToken cancellati // to a specific device if (device != null) { - if(!string.IsNullOrWhiteSpace(device.TransportId) && uint.TryParse(device.TransportId,out var tid)) - await SendAdbRequestAsync($"host:transport-id:{tid}", cancellationToken).ConfigureAwait(false); - else - await SendAdbRequestAsync($"host:transport:{device.Serial}", cancellationToken).ConfigureAwait(false); + await (uint.TryParse(device.TransportId, out uint tid) + ? SendAdbRequestAsync($"host:transport-id:{tid}", cancellationToken).ConfigureAwait(false) + : SendAdbRequestAsync($"host:transport:{device.Serial}", cancellationToken).ConfigureAwait(false)); try { diff --git a/AdvancedSharpAdbClient/AdbSocket.cs b/AdvancedSharpAdbClient/AdbSocket.cs index b3a16dd..2cd6368 100644 --- a/AdvancedSharpAdbClient/AdbSocket.cs +++ b/AdvancedSharpAdbClient/AdbSocket.cs @@ -401,10 +401,14 @@ public void SetDevice(DeviceData device) // to a specific device if (device != null) { - if(!string.IsNullOrWhiteSpace(device.TransportId) && uint.TryParse(device.TransportId,out var tid)) + if (uint.TryParse(device.TransportId, out uint tid)) + { SendAdbRequest($"host:transport-id:{tid}"); + } else + { SendAdbRequest($"host:transport:{device.Serial}"); + } try { diff --git a/AdvancedSharpAdbClient/Models/DeviceData.cs b/AdvancedSharpAdbClient/Models/DeviceData.cs index 3f1779d..d05fa30 100644 --- a/AdvancedSharpAdbClient/Models/DeviceData.cs +++ b/AdvancedSharpAdbClient/Models/DeviceData.cs @@ -107,7 +107,7 @@ public DeviceData(string data) : this() /// /// if does not have a valid serial number; otherwise, . /// - public bool IsEmpty => string.IsNullOrEmpty(Serial); + public bool IsEmpty => !uint.TryParse(TransportId, out _) && string.IsNullOrEmpty(Serial); /// /// Creates a new instance of the class based on @@ -208,9 +208,9 @@ public override int GetHashCode() /// public override string ToString() { - if (string.IsNullOrEmpty(Serial)) + if (IsEmpty) { - return $"An empty {GetType()} without {nameof(Serial)}"; + return $"An empty {GetType()} without {nameof(TransportId)} and {nameof(Serial)}"; } StringBuilder builder = @@ -283,7 +283,7 @@ public static ref DeviceData EnsureDevice(ref DeviceData device, [CallerArgument { if (device.IsEmpty) { - throw new ArgumentOutOfRangeException(nameof(device), "You must specific a serial number for the device"); + throw new ArgumentOutOfRangeException(nameof(device), "You must specific a transport ID or serial number for the device"); } return ref device; }