Skip to content

Commit 09c5a52

Browse files
authored
Merge pull request #71 from anotherlab/new-namespace
Updated namespace
2 parents 9136065 + 5c4be9b commit 09c5a52

34 files changed

Lines changed: 2903 additions & 2865 deletions

README.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ This is a driver library to allow your Microsoft Android app to communicate with
44
available on Android 3.1+.
55

66
No root access, ADK, or special kernel drivers are required; all drivers are implemented in
7-
c#. You get a raw serial port with `Read()`, `Write()`, and other basic
7+
C#. You get a raw serial port with `Read()`, `Write()`, and other basic
88
functions for use with your own protocols. The appropriate driver is picked based on the device's Vendor ID and Product ID.
99

10-
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#.
10+
This is a 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 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

@@ -21,6 +21,11 @@ git remote set-head origin -a
2121
```
2222
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).
2323

24+
## Breaking changes
25+
I'm cleaning up the code in order to publish this as a nuget package. Someone created a nuget package based on a two year old version of this code base and published to nuget under their own name (and without credit to the original authors). I changed the root namespace from `Hoho.Android.UsbSerial` to `Anotherlab.UsbSerialForAndroid` to make it easier to find on nuget.
26+
27+
The "Hoho" namespace came from original Java library package name, [usb-serial-for-android](https://github.com/mik3y/usb-serial-for-android), and while I based this code from that library, they are separate.
28+
2429
## Structure
2530

2631
This solution contains two projects and a slnx solution file.
@@ -34,7 +39,7 @@ The original demo and the .sln format solution file are deprecated and will be r
3439
## Getting Started
3540
**1.** Reference the library to your project
3641

37-
**2.** Copy the [device_filter.axml](https://github.com/anotherlab/UsbSerialForAndroid/blob/master/UsbSerialExampleApp/Resources/xml/device_filter.xml) from the example app to your Resources/xml folder. Make sure that the Build Action is set to AndroidResource
42+
**2.** Copy the [device_filter.axml](https://github.com/anotherlab/UsbSerialForAndroid/blob/main/UsbSerialForAndroidDemo/Resources/xml/device_filter.xml) from the example app to your Resources/xml folder. Make sure that the Build Action is set to AndroidResource
3843

3944
**3.** Add the following attribute to the main activity to enable the USB Host
4045
```C#
@@ -51,16 +56,12 @@ The original demo and the .sln format solution file are deprecated and will be r
5156
[MetaData(UsbManager.ActionUsbDeviceAttached, Resource = "@xml/device_filter")]
5257
```
5358

54-
**6.** Refer to [MainActivity.cs](https://github.com/anotherlab/UsbSerialForAndroid/blob/master/UsbSerialExampleApp/MainActivity.cs) in the example app to see how connect to a serial device and read data from it.
59+
**6.** Refer to [MainActivity.cs](https://github.com/anotherlab/UsbSerialForAndroid/blob/main/UsbSerialForAndroidDemo/MainActivity.cs) in the example app to see how connect to a serial device and read data from it.
5560

5661
## Working with unrecognized devices
5762
The UsbSerialForAndroid has been compiled with the Vendor ID/Product ID pairs for many common serial devices. If you have a device that is not defined by the library, but will work with one of the drivers, you can manually add the VID/PID pair. If you have a device that is not in the GetSupportedDevices() method for that driver, you can submit a pull request that adds the vendor and product IDs to that driver.
5863

59-
UsbSerialProber is a class to help you find and instantiate compatible
60-
UsbSerialDrivers from the tree of connected UsbDevices. Normally, you will use
61-
the default prober returned by ``UsbSerialProber.getDefaultProber()``, which
62-
uses the built-in list of well-known VIDs and PIDs that are supported by our
63-
drivers.
64+
UsbSerialProber is a class to help you find and instantiate compatible UsbSerialDrivers from the tree of connected UsbDevices. Normally, you will use the default prober returned by ``UsbSerialProber.getDefaultProber()``, which uses the built-in list of well-known VIDs and PIDs that are supported by our drivers.
6465

6566
To use your own set of rules, create and use a custom prober:
6667

@@ -89,7 +90,7 @@ Of course, nothing requires you to use UsbSerialProber at all: you can instantia
8990

9091
## Additional information
9192

92-
This is a port of the usb-serial-for-android library and code examples written for it can be adapted to C# without much effort.
93+
This is a port of the [usb-serial-for-android](https://github.com/mik3y/usb-serial-for-android) library and code examples written for it can be adapted to C# without much effort.
9394

9495
For common problems, see the
9596
[Troubleshooting](https://github.com/mik3y/usb-serial-for-android/wiki/Troubleshooting)
@@ -98,14 +99,14 @@ wiki page for usb-serial-for-android library. For other help and discussion, ple
9899

99100
Pull Requests are welcome, but please include what hardware was used for testing. We do not have the hardware or the bandwidth to test the various chipsets supported by the library.
100101

101-
We will do our best to repond to reported issues. If you have a code fix or suggestion, we are only looking at changes submitted as pull requests.
102+
We will do our best to respond to reported issues. If you have a code fix or suggestion, we are only looking at changes submitted as pull requests.
102103

103-
For more information about contributing or reporting an issue, please see [](https://github.com/anotherlab/UsbSerialForAndroid/blob/main/CONTRIBUTING.md) for more information for what we are looking for and how to get started.
104+
For more information about contributing or reporting an issue, please see [CONTRIBUTING.md](https://github.com/anotherlab/UsbSerialForAndroid/blob/main/CONTRIBUTING.md) for more information for what we are looking for and how to get started.
104105

105106
## Author, License, and Copyright
106107

107108
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.
108109

109110
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.
110111

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)
112+
The icon used for the demo app was derived from [Serial to USB by Bonegolem](https://thenounproject.com/browse/icons/term/serial-to-usb/) (CC BY 3.0)

UsbSerialForAndroid/Extensions/AsyncExtensions.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,20 @@
99
using System.Threading.Tasks;
1010
using Android.Hardware.Usb;
1111

12-
using Hoho.Android.UsbSerial.Driver;
12+
using Anotherlab.UsbSerialForAndroid.Driver;
1313

14-
namespace Hoho.Android.UsbSerial.Extensions
14+
namespace Anotherlab.UsbSerialForAndroid.Extensions;
15+
16+
public static partial class AsyncExtensions
1517
{
16-
public static partial class AsyncExtensions
18+
public static Task<IList<IUsbSerialDriver>> FindAllDriversAsync(this UsbSerialProber prober, UsbManager manager)
1719
{
18-
public static Task<IList<IUsbSerialDriver>> FindAllDriversAsync(this UsbSerialProber prober, UsbManager manager)
19-
{
20-
var tcs = new TaskCompletionSource<IList<IUsbSerialDriver>>();
20+
var tcs = new TaskCompletionSource<IList<IUsbSerialDriver>>();
2121

22-
Task.Run(() =>
23-
{
24-
tcs.TrySetResult(prober.FindAllDrivers(manager));
25-
});
26-
return tcs.Task;
27-
}
22+
Task.Run(() =>
23+
{
24+
tcs.TrySetResult(prober.FindAllDrivers(manager));
25+
});
26+
return tcs.Task;
2827
}
2928
}

UsbSerialForAndroid/Extensions/BufferExtensions.cs

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,49 +10,48 @@
1010
using Android.Runtime;
1111
using Java.Nio;
1212

13-
namespace Hoho.Android.UsbSerial.Extensions
13+
namespace Anotherlab.UsbSerialForAndroid.Extensions;
14+
15+
/// <summary>
16+
/// Work around for faulty JNI wrapping in Xamarin library. Fixes a bug
17+
/// where binding for Java.Nio.ByteBuffer.Get(byte[], int, int) allocates a new temporary
18+
/// Java byte array on every call
19+
/// See https://bugzilla.xamarin.com/show_bug.cgi?id=31260
20+
/// and http://stackoverflow.com/questions/30268400/xamarin-implementation-of-bytebuffer-get-wrong
21+
/// </summary>
22+
public static class BufferExtensions
1423
{
15-
/// <summary>
16-
/// Work around for faulty JNI wrapping in Xamarin library. Fixes a bug
17-
/// where binding for Java.Nio.ByteBuffer.Get(byte[], int, int) allocates a new temporary
18-
/// Java byte array on every call
19-
/// See https://bugzilla.xamarin.com/show_bug.cgi?id=31260
20-
/// and http://stackoverflow.com/questions/30268400/xamarin-implementation-of-bytebuffer-get-wrong
21-
/// </summary>
22-
public static class BufferExtensions
23-
{
24-
static IntPtr _byteBufferClassRef;
25-
static IntPtr _byteBufferGetBii;
24+
static IntPtr _byteBufferClassRef;
25+
static IntPtr _byteBufferGetBii;
2626

27-
public static ByteBuffer Get(this ByteBuffer buffer, JavaArray<Java.Lang.Byte> dst, int dstOffset, int byteCount)
27+
public static ByteBuffer Get(this ByteBuffer buffer, JavaArray<Java.Lang.Byte> dst, int dstOffset, int byteCount)
28+
{
29+
if (_byteBufferClassRef == IntPtr.Zero)
2830
{
29-
if (_byteBufferClassRef == IntPtr.Zero)
30-
{
31-
_byteBufferClassRef = JNIEnv.FindClass("java/nio/ByteBuffer");
32-
}
33-
if (_byteBufferGetBii == IntPtr.Zero)
34-
{
35-
_byteBufferGetBii = JNIEnv.GetMethodID(_byteBufferClassRef, "get", "([BII)Ljava/nio/ByteBuffer;");
36-
}
37-
38-
return Java.Lang.Object.GetObject<ByteBuffer>(JNIEnv.CallObjectMethod(buffer.Handle, _byteBufferGetBii, new JValue[] {
39-
new JValue(dst),
40-
new JValue(dstOffset),
41-
new JValue(byteCount)
42-
}), JniHandleOwnership.TransferLocalRef);
31+
_byteBufferClassRef = JNIEnv.FindClass("java/nio/ByteBuffer");
4332
}
44-
45-
public static byte[] ToByteArray(this ByteBuffer buffer)
33+
if (_byteBufferGetBii == IntPtr.Zero)
4634
{
47-
IntPtr classHandle = JNIEnv.FindClass("java/nio/ByteBuffer");
48-
IntPtr methodId = JNIEnv.GetMethodID(classHandle, "array", "()[B");
49-
IntPtr resultHandle = JNIEnv.CallObjectMethod(buffer.Handle, methodId);
35+
_byteBufferGetBii = JNIEnv.GetMethodID(_byteBufferClassRef, "get", "([BII)Ljava/nio/ByteBuffer;");
36+
}
5037

51-
byte[] result = JNIEnv.GetArray<byte>(resultHandle);
38+
return Java.Lang.Object.GetObject<ByteBuffer>(JNIEnv.CallObjectMethod(buffer.Handle, _byteBufferGetBii, new JValue[] {
39+
new JValue(dst),
40+
new JValue(dstOffset),
41+
new JValue(byteCount)
42+
}), JniHandleOwnership.TransferLocalRef);
43+
}
5244

53-
JNIEnv.DeleteLocalRef(resultHandle);
45+
public static byte[] ToByteArray(this ByteBuffer buffer)
46+
{
47+
IntPtr classHandle = JNIEnv.FindClass("java/nio/ByteBuffer");
48+
IntPtr methodId = JNIEnv.GetMethodID(classHandle, "array", "()[B");
49+
IntPtr resultHandle = JNIEnv.CallObjectMethod(buffer.Handle, methodId);
5450

55-
return result;
56-
}
51+
byte[] result = JNIEnv.GetArray<byte>(resultHandle);
52+
53+
JNIEnv.DeleteLocalRef(resultHandle);
54+
55+
return result;
5756
}
5857
}

UsbSerialForAndroid/Extensions/EventHandlerExtensions.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,17 @@
88
using System;
99
using System.Threading;
1010

11-
namespace Hoho.Android.UsbSerial.Extensions
11+
namespace Anotherlab.UsbSerialForAndroid.Extensions;
12+
13+
static class EventHandlerExtensions
1214
{
13-
static class EventHandlerExtensions
15+
public static void Raise(this EventHandler handler, object sender, EventArgs e)
1416
{
15-
public static void Raise(this EventHandler handler, object sender, EventArgs e)
16-
{
17-
Volatile.Read(ref handler)?.Invoke(sender, e);
18-
}
17+
Volatile.Read(ref handler)?.Invoke(sender, e);
18+
}
1919

20-
public static void Raise<T>(this EventHandler<T> handler, object sender, T e) where T : EventArgs
21-
{
22-
Volatile.Read(ref handler)?.Invoke(sender, e);
23-
}
20+
public static void Raise<T>(this EventHandler<T> handler, object sender, T e) where T : EventArgs
21+
{
22+
Volatile.Read(ref handler)?.Invoke(sender, e);
2423
}
2524
}

UsbSerialForAndroid/Extensions/SerialDataReceivedArgs.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77

88
using System;
99

10-
namespace Hoho.Android.UsbSerial.Extensions
10+
namespace Anotherlab.UsbSerialForAndroid.Extensions;
11+
12+
public class SerialDataReceivedArgs : EventArgs
1113
{
12-
public class SerialDataReceivedArgs : EventArgs
14+
public SerialDataReceivedArgs(byte[] data)
1315
{
14-
public SerialDataReceivedArgs(byte[] data)
15-
{
16-
Data = data;
17-
}
18-
19-
public byte[] Data { get; private set; }
16+
Data = data;
2017
}
18+
19+
public byte[] Data { get; private set; }
2120
}

UsbSerialForAndroid/Extensions/SerialInputOutputManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
using Android.Hardware.Usb;
1111
using Android.Util;
1212
using System.Threading.Tasks;
13-
using Hoho.Android.UsbSerial.Driver;
13+
using Anotherlab.UsbSerialForAndroid.Driver;
1414

15-
namespace Hoho.Android.UsbSerial.Extensions
15+
namespace Anotherlab.UsbSerialForAndroid.Extensions
1616
{
1717
public class SerialInputOutputManager : IDisposable
1818
{

0 commit comments

Comments
 (0)