Skip to content

Commit 29984b2

Browse files
authored
Merge pull request #69 from anotherlab/docs
Update to .NET 10
2 parents 9cc3ee8 + ff72403 commit 29984b2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1136
-284
lines changed

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# UsbSerialForAndroid
22

3-
This is a driver library to allow your Xamarin Android or Microsoft Android app to communicate with many common USB serial hardware. It uses the [Android USB Host API](http://developer.android.com/guide/topics/connectivity/usb/host.html)
3+
This is a driver library to allow your Microsoft Android app to communicate with many common USB serial hardware. It uses the [Android USB Host API](http://developer.android.com/guide/topics/connectivity/usb/host.html)
44
available on Android 3.1+.
55

66
No root access, ADK, or special kernel drivers are required; all drivers are implemented in
@@ -9,7 +9,7 @@ functions for use with your own protocols. The appropriate driver is picked base
99

1010
This is a Xamarin C# port of Mike Wakerly's Java [usb-serial-for-android](https://github.com/mik3y/usb-serial-for-android) library. It followed that library very closely when it was ported. The main changes were to make the method names follow C# standard naming conventions. Some Java specific data types were replaced with .NET types and the reflection code is .NET specific. Code examples written for the Java version of the library should translate more or less faithfully to C#.
1111

12-
It also includes code derived from a portion of LusoVU's [XamarinUsbSerial](https://bitbucket.org/lusovu/xamarinusbserial) library. XamarinUsbSerial was a C# wrapper for the Java usb-serial-for-android. It used an older version of the usb-serial-for-android .jar file. Only the the C# code was used, the Java library is not referenced.
12+
It also includes code derived from a portion of LusoVU's [XamarinUsbSerial](https://bitbucket.org/lusovu/xamarinusbserial) library. XamarinUsbSerial was a C# wrapper for the Java usb-serial-for-android. It used an older version of the usb-serial-for-android .jar file. Only the C# code was used, the Java library is not referenced.
1313

1414
The default branch has been renamed from master to main. if you have a local clone, you can run the following commands to update the name of the default branch
1515

@@ -19,17 +19,17 @@ git fetch origin
1919
git branch -u origin/main main
2020
git remote set-head origin -a
2121
```
22-
23-
This library currently supports Xamarin.Android, .NET 6, .NET 7, and .NET 8. The demo app currently targets .NET 8, but the code was written for Xamarin.Android. The code works with .NET 9 and an upcoming update will update the demo projects to .NET 9.
24-
25-
Support for Xamarin will be dropped in the next update. Microsoft has ended support for Xamarin and you can no longer support apps to the Google Play Store that were built with Xamarin.
22+
This library supports .NET 10 and Microsoft Android. Support for Xamarin Android and previous versions of .NET have been dropped. Sample application has been replaced with a new app demo. If you need the old demo or want to support older versions of .NET, please use [verson 1.1.1](https://github.com/anotherlab/UsbSerialForAndroid/releases/tag/v1.1.1).
2623

2724
## Structure
2825

29-
This solution contains two projects.
26+
This solution contains two projects and a slnx solution file.
3027

3128
* UsbSerialForAndroid - A port of the Java library usb-serial-for-android
32-
* UsbSerialExampleApp - A Xamarin version of the example app that comes with usb-serial-for-android
29+
* UsbSerialForAndroidDemo - A Microsoft Android version of the example app that comes with usb-serial-for-android
30+
31+
32+
The original demo and the .sln format solution file are deprecated and will be removed in a subsequent update.
3333

3434
## Getting Started
3535
**1.** Reference the library to your project
@@ -107,3 +107,5 @@ For more information about contributing or reporting an issue, please see [](htt
107107
This library is licensed under the MIT License. Please see [LICENSE.txt](https://github.com/anotherlab/UsbSerialForAndroid/blob/main/LICENSE.txt) for the complete license.
108108

109109
Copyright 2017, Tyler Technologies. All Rights Reserved. Portions of this library are based on the [usb-serial-for-android](https://github.com/mik3y/usb-serial-for-android) and [XamarinUsbSerial](https://bitbucket.org/lusovu/xamarinusbserial) libraries. Their rights remain intact.
110+
111+
The icon used for the demo app was derived from [Serial to USB by Bonegolem](https://thenounproject.com/browse/icons/term/serial-to-usb/) from <a href="https://thenounproject.com/browse/icons/term/serial-to-usb/" (CC BY 3.0)

UsbSerialExampleApp/MainActivity.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,21 @@ public UsbDeviceDetachedReceiver(MainActivity activity)
206206

207207
public async override void OnReceive(Context context, Intent intent)
208208
{
209-
var device = intent.GetParcelableExtra(UsbManager.ExtraDevice) as UsbDevice;
209+
UsbDevice device;
210+
211+
if (Build.VERSION.SdkInt >= BuildVersionCodes.Tiramisu) // Android 33
212+
{
213+
#pragma warning disable CA1416 // Suppress warning to validate platform compatibility
214+
device = intent.GetParcelableExtra(UsbManager.ExtraDevice, Java.Lang.Class.FromType(typeof(UsbDevice))) as UsbDevice;
215+
#pragma warning restore CA1416
216+
}
217+
else
218+
{
219+
#pragma warning disable CA1422 // Suppress warning for obsolete API usage
220+
device = intent.GetParcelableExtra(UsbManager.ExtraDevice) as UsbDevice;
221+
#pragma warning restore CA1422
222+
}
223+
210224

211225
Log.Info(TAG, "USB device detached: " + device.DeviceName);
212226

UsbSerialExampleApp/SerialConsoleActivity.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,21 @@ protected async override void OnResume()
116116

117117
base.OnResume();
118118

119-
var portInfo = Intent.GetParcelableExtra(EXTRA_TAG) as UsbSerialPortInfo;
119+
UsbSerialPortInfo portInfo;
120+
121+
if (Build.VERSION.SdkInt >= BuildVersionCodes.Tiramisu) // Android 33
122+
{
123+
#pragma warning disable CA1416 // Suppress warning to validate platform compatibility
124+
portInfo = Intent.GetParcelableExtra(EXTRA_TAG, Java.Lang.Class.FromType(typeof(UsbSerialPortInfo))) as UsbSerialPortInfo;
125+
#pragma warning restore CA1416
126+
}
127+
else
128+
{
129+
#pragma warning disable CA1422 // Suppress warning for obsolete API usage
130+
portInfo = Intent.GetParcelableExtra(EXTRA_TAG) as UsbSerialPortInfo;
131+
#pragma warning restore CA1422
132+
}
133+
120134
int vendorId = portInfo.VendorId;
121135
int deviceId = portInfo.DeviceId;
122136
int portNumber = portInfo.PortNumber;
Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>net6.0-android</TargetFramework>
3+
<!-- Target .NET 10 Android -->
4+
<TargetFramework>net10.0-android</TargetFramework>
5+
6+
<!-- Minimum supported Android API level -->
47
<SupportedOSPlatformVersion>21</SupportedOSPlatformVersion>
8+
9+
<!-- Output type: Android app -->
510
<OutputType>Exe</OutputType>
11+
12+
<!-- Project settings -->
613
<Nullable>disable</Nullable>
714
<ImplicitUsings>disable</ImplicitUsings>
15+
16+
<!-- Android app metadata -->
817
<ApplicationId>UsbSerialExampleApp.UsbSerialExampleApp</ApplicationId>
918
<ApplicationVersion>1</ApplicationVersion>
1019
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
20+
21+
<!-- Assembly metadata -->
1122
<AssemblyName>UsbSerialExampleApp</AssemblyName>
1223
<RootNamespace>UsbSerialExampleApp</RootNamespace>
1324
</PropertyGroup>
25+
26+
<!-- Release build settings -->
1427
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
1528
<AndroidLinkTool>r8</AndroidLinkTool>
1629
<AndroidPackageFormat>apk</AndroidPackageFormat>
1730
</PropertyGroup>
18-
<ItemGroup />
31+
32+
<!-- Project references -->
1933
<ItemGroup>
2034
<ProjectReference Include="..\UsbSerialForAndroid\UsbSerialForAndroid.csproj" />
2135
</ItemGroup>
22-
<ItemGroup />
23-
</Project>
36+
</Project>

UsbSerialForAndroid.slnx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<Solution>
2+
<Project Path="UsbSerialForAndroid/UsbSerialForAndroid.csproj" />
3+
<Project Path="UsbSerialForAndroidDemo/UsbSerialForAndroidDemo.csproj" Id="3816aadb-ed3d-4da2-b8dd-8ea2a10a9ebb">
4+
<Deploy />
5+
</Project>
6+
</Solution>

UsbSerialForAndroid/Extensions/UsbManagerExtensions.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,9 @@ public static Task<bool> RequestPermissionAsync(this UsbManager manager, UsbDevi
3939
}
4040

4141
// Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
42-
#if NET6_0_OR_GREATER
42+
#pragma warning disable CA1416
4343
PendingIntentFlags pendingIntentFlags = Build.VERSION.SdkInt >= BuildVersionCodes.S ? PendingIntentFlags.Mutable : 0;
44-
#else
45-
PendingIntentFlags pendingIntentFlags = Build.VERSION.SdkInt >= (BuildVersionCodes)31 ? (PendingIntentFlags)33554432 : 0;
46-
#endif
44+
#pragma warning restore CA1416
4745

4846
var intent = new Intent(ACTION_USB_PERMISSION);
4947
if (Build.VERSION.SdkInt >= BuildVersionCodes.UpsideDownCake)
@@ -68,7 +66,20 @@ public UsbPermissionReceiver(TaskCompletionSource<bool> completionSource)
6866

6967
public override void OnReceive(Context context, Intent intent)
7068
{
71-
var device = intent.GetParcelableExtra(UsbManager.ExtraDevice) as UsbDevice;
69+
if (Build.VERSION.SdkInt >= BuildVersionCodes.Tiramisu)
70+
{
71+
#pragma warning disable CA1416
72+
var device = intent.GetParcelableExtra(UsbManager.ExtraDevice, Java.Lang.Class.FromType(typeof(UsbDevice)));
73+
#pragma warning restore CA1416
74+
}
75+
else
76+
{
77+
#pragma warning disable CA1422
78+
var device = intent.GetParcelableExtra(UsbManager.ExtraDevice) as UsbDevice;
79+
#pragma warning restore CA1422
80+
}
81+
82+
7283
var permissionGranted = intent.GetBooleanExtra(UsbManager.ExtraPermissionGranted, false);
7384
context.UnregisterReceiver(this);
7485
completionSource.TrySetResult(permissionGranted);

UsbSerialForAndroid/Extensions/UsbSerialPortInfo.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ public UsbSerialPortInfo(UsbSerialPort port)
3131
VendorId = device.VendorId;
3232
DeviceId = device.DeviceId;
3333
PortNumber = port.PortNumber;
34+
DeviceName = device.DeviceName;
3435
}
3536

3637
private UsbSerialPortInfo(Parcel parcel)
3738
{
3839
VendorId = parcel.ReadInt();
3940
DeviceId = parcel.ReadInt();
4041
PortNumber = parcel.ReadInt();
42+
DeviceName = parcel.ReadString();
4143
}
4244

4345
public int VendorId { get; set; }
@@ -46,6 +48,8 @@ private UsbSerialPortInfo(Parcel parcel)
4648

4749
public int PortNumber { get; set; }
4850

51+
public string DeviceName { get; set; }
52+
4953
#region IParcelable implementation
5054

5155
public int DescribeContents()
@@ -58,6 +62,7 @@ public void WriteToParcel(Parcel dest, ParcelableWriteFlags flags)
5862
dest.WriteInt(VendorId);
5963
dest.WriteInt(DeviceId);
6064
dest.WriteInt(PortNumber);
65+
dest.WriteString(DeviceName);
6166
}
6267

6368
#endregion
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
<Project Sdk="Xamarin.Legacy.Sdk/0.2.0-alpha4">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>net6.0-android;net7.0-android;monoandroid5.0</TargetFrameworks>
3+
<!-- Target .NET 10 Android -->
4+
<TargetFramework>net10.0-android</TargetFramework>
5+
6+
<!-- Minimum supported Android API level -->
47
<SupportedOSPlatformVersion>21</SupportedOSPlatformVersion>
8+
9+
<!-- Optional project settings -->
510
<Nullable>disable</Nullable>
611
<ImplicitUsings>disable</ImplicitUsings>
12+
13+
<!-- Assembly metadata -->
714
<AssemblyName>Android.UsbSerial</AssemblyName>
815
<RootNamespace>Hoho.Android.UsbSerial</RootNamespace>
916
</PropertyGroup>
10-
</Project>
17+
</Project>

UsbSerialForAndroid/Util/HexDump.cs

Lines changed: 14 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -12,135 +12,31 @@ namespace Hoho.Android.UsbSerial.Util
1212
{
1313
public class HexDump
1414
{
15-
private static char[] HEX_DIGITS = {
16-
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
17-
};
18-
19-
public static string DumpHexString(byte[] array)
20-
{
21-
return DumpHexString(array, 0, array.Length);
22-
}
23-
public static string DumpHexString(byte[] array, int offset, int length)
15+
public static string ByteArrayToHexString(byte[] bytes)
2416
{
25-
StringBuilder result = new StringBuilder();
26-
27-
byte[] line = new byte[16];
28-
int lineIndex = 0;
29-
30-
result.Append("\n0x");
31-
result.Append(ToHexString(offset));
32-
33-
for (int i = offset; i < offset + length; i++)
34-
{
35-
if (lineIndex == 16)
36-
{
37-
result.Append(" ");
38-
39-
for (int j = 0; j < 16; j++)
40-
{
41-
if (line[j] > ' ' && line[j] < '~')
42-
{
43-
result.Append(System.Text.Encoding.Default.GetString(line).Substring(j, 1));
44-
}
45-
else
46-
{
47-
result.Append(".");
48-
}
49-
}
50-
51-
result.Append("\n0x");
52-
result.Append(ToHexString(i));
53-
lineIndex = 0;
54-
}
55-
56-
byte b = array[i];
57-
result.Append(" ");
58-
result.Append(HEX_DIGITS[(b >> 4) & 0x0F]);
59-
result.Append(HEX_DIGITS[b & 0x0F]);
17+
if (bytes == null || bytes.Length == 0)
18+
return string.Empty;
6019

61-
line[lineIndex++] = b;
62-
}
63-
64-
if (lineIndex != 16)
65-
{
66-
int count = (16 - lineIndex) * 3;
67-
count++;
68-
for (int i = 0; i < count; i++)
69-
{
70-
result.Append(" ");
71-
}
72-
73-
for (int i = 0; i < lineIndex; i++)
74-
{
75-
if (line[i] > ' ' && line[i] < '~')
76-
{
77-
result.Append(System.Text.Encoding.Default.GetString(line).Substring(i, 1));
78-
}
79-
else
80-
{
81-
result.Append(".");
82-
}
83-
}
84-
}
85-
86-
return result.ToString();
20+
return BitConverter.ToString(bytes).Replace("-", " ");
8721
}
8822

89-
public static string ToHexString(byte[] byteArray)
23+
public static string ByteArrayToHexString(byte[] bytes, int maxLength)
9024
{
91-
return BitConverter.ToString(byteArray).Replace("-", "");
92-
}
25+
if (bytes == null || bytes.Length == 0 || maxLength <= 0)
26+
return string.Empty;
9327

94-
public static string ToHexString(byte[] byteArray, int offset, int length)
95-
{
96-
StringBuilder hex = new StringBuilder(length*2);
28+
int length = Math.Min(bytes.Length, maxLength);
29+
var sb = new StringBuilder(length * 3);
9730

98-
while ((offset < byteArray.Length) && (length > 0))
31+
for (int i = 0; i < length; i++)
9932
{
100-
hex.AppendFormat("{0:x2}", byteArray[offset]);
33+
sb.Append(bytes[i].ToString("X2"));
10134

102-
offset++;
103-
length--;
35+
if (i < length - 1)
36+
sb.Append(' ');
10437
}
105-
return hex.ToString();
106-
}
10738

108-
public static string ToHexString(int i)
109-
{
110-
return ToHexString(ToByteArray(i));
111-
}
112-
113-
public static string ToHexString(short i)
114-
{
115-
return ToHexString(ToByteArray(i));
116-
}
117-
118-
public static byte[] ToByteArray(byte b)
119-
{
120-
return new byte[] {b};
121-
}
122-
123-
public static byte[] ToByteArray(int i)
124-
{
125-
byte[] array = new byte[4];
126-
127-
array[3] = (byte) (i & 0xFF);
128-
array[2] = (byte) ((i >> 8) & 0xFF);
129-
array[1] = (byte) ((i >> 16) & 0xFF);
130-
array[0] = (byte) ((i >> 24) & 0xFF);
131-
132-
return array;
39+
return sb.ToString();
13340
}
134-
135-
public static byte[] ToByteArray(short i)
136-
{
137-
byte[] array = new byte[2];
138-
139-
array[1] = (byte) (i & 0xFF);
140-
array[0] = (byte) ((i >> 8) & 0xFF);
141-
142-
return array;
143-
}
144-
14541
}
14642
}

0 commit comments

Comments
 (0)