Xamarin Forms Android binding library problem (java generic type to c#)

8 posts / 0 new
Last post
IanB
Xamarin Forms Android binding library problem (java generic type to c#)

I have a Xamarin Forms VS2019 project which includes binding library projects for Gurux.Common, .Serial and .DLMS. I built these by loading each project's source code from GitHub into Android Studio and creating .aar files for each, then using these to create binding libraries in VS. I have overcome most of the Java > C# errors from building these libraries, but one remains. Gurux.Common has a generic type: ReceiveParameters<T>, which converts to ReceiveParameters (I gather Java generics can't be converted for some reason).

My problem is that I cannot do this from the DLMS code:
ReceiveParameters<byte[]> p = new ReceiveParameters<byte[]>()
{
...
}

I have tried various things, using the binding library project's Metadata.xml file as well as using Additions, but cannot get this to work. I have even tried to create a non-generic version of ReceiveParameters which uses only 'byte[]' but cannot get past the various Java error messages I get (I am not a Java developer!).

Has anyone successfully found a solution/workaround to this problem (or one very similar to it)?
I have spent a long time on this and am very stuck!
Any help VERY much appreciated!!

Thanks,
Ian.

Kurumi
Kurumi's picture

Hi,

I did try this and I can compile it without problems. What I did is this.
I create a new project and select "Manage Nuget packets...". Then install Gurux.Net and Gurux.DLMS.
Create a simple example and it is compiled without problems.

Try to install NuGet packages. I hope it will help you.

BR,
Mikko

________________________________________
Mikko Kurunsaari
Gurux Ltd
http://www.gurux.fi

IanB

Hi Mikko,

Thanks for your reply. However, my problem is with Android, not .NET. My VS solution is actually targeting both .NET and Android, using as much common code as possible with differences only to do with the serial connection. So in my .NET project I am using the relevant NuGet (C#) packages (Gurux.Common, Gurux.Serial, Gurux.DLMS.NET) and this all works fine. For the Android project, because I am using Xamarin/Xamarin Forms, I cannot use the NuGet packages written in Java for native Android development. Therefore I created binding libraries (C#) from the related Java modules (gurux.common.android, gurux.serial.android & Gurux.DLMS.Android) which I can call from Xamarin. As I said, this is mostly working, but I have a problem with how some Java is being converted to C#.
I know this is not strictly a Gurux problem but hoped that someone else might have had a similar experience.
On a related point, can you tell me why the GitHub source code for the Java and C# versions of those 3 modules is so different? The .NET versions seem to have been modified much more recently and have many different classes inside them - I would have hoped that the C# and Java versions would be very similar (this has caused me a lot of time to work around!)

If anyone can help, please let me know.
Thanks!

Kurumi
Kurumi's picture

Hi,

I test compiling with Visual Studio 2019 and create Mobile App (Xamarin.Forms) project and it worked like expected. What is your project type, so I can test this?

The Java and C # versions are as similar as possible. Java offers some features that are not in C#. Serial port handling is causing more work in Java than in C#.

BR,
Mikko

________________________________________
Mikko Kurunsaari
Gurux Ltd
http://www.gurux.fi

IanB

Hi. Thanks for your reply.
I am using the same project type as you, which has created 2 projects: '<project>' (shared C#) and '<project>.Android' (Xamarin Android C#). In my '.Android' project I have references to the 3 Gurux binding libraries mentioned previously, so the phone can connect and talk through the phone's serial port.
When you say it worked as expected, what do you mean? Have you run the Xamarin project on an Android phone and connected to a meter? What references and/or packages does your '.Android' project have?
Apart from the way I am doing it, is there another way for Gurux code to work on an Android phone when built in a Xamarin Forms project?
Thanks again!

Kurumi
Kurumi's picture

Hi,

I tested this with a simple app that is using Gurux.Net and Gurux.DLMS libraries. I generated SNRM request and sent it over the TCP/IP connection. Everything worked like expected.

There is one thing that is important for you. There are several versions of Gurux components. When you add a component from NuGet it will check and install the correct version for your project.
You don't need to do wrapper. You can use the library without changes in your Xamarin Android C# project.

BR,
Mikko

________________________________________
Mikko Kurunsaari
Gurux Ltd
http://www.gurux.fi

IanB

Hi again,
You imply that I can add NuGet packages to my Android project and it will work. Maybe this is ok for network comms but this does not seem to be the case for a serial port connection, which as you said earlier, needs extra work to be done. Android needs special handling to access the serial port (as a USB port), as is done in the GXSerial.open() method (see https://github.com/Gurux/gurux.serial.android/blob/master/gurux.serial.j...)
For .NET the NuGet packages use the System.IO.SerialPort class which Android does not support. This was the reason I started using the Android versions of Gurux modules and importing then into my solution.
I wrote a quick test solution using NuGet packages in an Android project and got the result in the attached image.
Please tell me if I am misunderstanding something about this,as I cannot see a way to use NuGet packages directly in an Android (i.e. 'xxx.Android') project.
Thanks for your help,
Ian.

Image: 
Kurumi
Kurumi's picture

Hi,

Yes, there is no native serial port support for Android. This is not big work because we have done this already and know what to do. I'll ask if some of our clients if this is needed. If they do we can add support for Android to C# serial port component quite fast.

BR,
Mikko

________________________________________
Mikko Kurunsaari
Gurux Ltd
http://www.gurux.fi