Convert Java Interface To C# Interface

EvanWilsonEvanWilson USMember ✭✭
edited January 2017 in Xamarin.Android

Hello, I am trying to implement a java based sdk using Xamarin. I have successfully used other parts of the SDK, but I am not sure how to translate the following code into C#.
Example:

To perform a continuous inventory for tags which EPCs start with “1122”
RFIDReader reader = RFID.open(); reader.inventory(new RFIDCallback() { @Override public void onTagRead(Tag tag) { String epc = tag.getEPC(); double rssi = tag.getRSSI(); } }, Mask.maskEPC("1122") );

I have tried to implement this in C#

public void startBulkScan()
        {
            _thread = new Thread(() => {
                if (reader != null && !reader.IsRunning)
                {
                    string chipID = string.Empty;
                    try
                    {

                       RFIDCallback rfCallBack = new RFIDCallback();
                       rfCallBack.TagRead += RfCallBack_TagRead;
                       reader.Inventory(rfCallBack);
                    }
                    catch (ReaderException e)
                    {

                    }
                    catch (Exception e)
                    { }
                }
            });
            _thread.Start();
        }

And the Interface

    class RFIDCallback : IRFIDCallback
            {
                public IntPtr handle { get; set; }
                public event Action<AndroidReader.TagDataEventArgs> TagRead = delegate { } ; 

                public IntPtr Handle
                {
                    get
                    {
                        return handle;
                    }
                }

                public void Dispose()
                {
                    //throw new NotImplementedException();
                }

                public void OnTagRead(Tag p0)
                {
                    string epc = p0.EPC;
                    TagRead(new AndroidReader.TagDataEventArgs { EPC = epc }); 
                }
            }

But when I try running this it just throws a Unhandled Java Exception that I cannot track down or catch.

Any Assistance is appreciated

Best Answer

  • DiogoLeaoDiogoLeao US ✭✭
    edited January 2017 Accepted Answer

    should like this

    public class RFIDCallback : Java.Lang.Object, IRFIDCallback { public event Action<AndroidReader.TagDataEventArgs> TagRead; public void OnTagRead(Tag p0) { string epc = p0.EPC; TagRead?.Invoke(new AndroidReader.TagDataEventArgs { EPC = epc }); } }

    Java.Lang.Object already implement IDisposable

Answers

  • DiogoLeaoDiogoLeao USMember ✭✭

    class RFIDCallback : Java.Lang.Object, IRFIDCallback

  • EvanWilsonEvanWilson USMember ✭✭

    I just tried that and it is doing the same thing

  • DiogoLeaoDiogoLeao USMember ✭✭

    Show me IRFIDCallback class

  • EvanWilsonEvanWilson USMember ✭✭
    edited January 2017

    Here is the native Java Class

    package com.alien.rfid;
    
    public abstract interface RFIDCallback
    {
      public abstract void onTagRead(Tag paramTag);
    }
    

    The Original Start Method in Java

     public void inventory(RFIDCallback 
    throws ReaderException
     {
            inventory(callback, this.a);
     }
    

    The abstract class that Inventory is derived from

    public abstract void inventory(RFIDCallback paramRFIDCallback, Mask paramMask)
            throws ReaderException;
    
  • DiogoLeaoDiogoLeao USMember ✭✭

    Apparently your implementation is ok

    when add extension Java.Lang.Object to IRFIDCallback class you removed too?

    `public IntPtr handle { get; set; }

                public IntPtr Handle
                {
                    get
                    {
                        return handle;
                    }
                }
    
                public void Dispose()
                {
                    //throw new NotImplementedException();
                }`
    
  • EvanWilsonEvanWilson USMember ✭✭

    Visual Studio is forcing me to implement them to satisfy the interface.

  • DiogoLeaoDiogoLeao USMember ✭✭
    edited January 2017 Accepted Answer

    should like this

    public class RFIDCallback : Java.Lang.Object, IRFIDCallback { public event Action<AndroidReader.TagDataEventArgs> TagRead; public void OnTagRead(Tag p0) { string epc = p0.EPC; TagRead?.Invoke(new AndroidReader.TagDataEventArgs { EPC = epc }); } }

    Java.Lang.Object already implement IDisposable

Sign In or Register to comment.