[mono-android] JNI Help Please

digitalml matthew.lebo at stationcasinos.com
Fri Feb 3 18:51:19 UTC 2012


Jon,

First, thanks for all your help on this, its gotten me pretty far. 

I used your code to call the jni_helper and initialize the reader; however,
the manged wrapper for the onReceiveMsgCardData() is never called.  You were
right in assuming that the 'uniMagReader' instance calls the
onReceiveMsgCardData() inside the .java file.  If i could somehow have the
onReceiveMsgCardData() in the .java file call back in to .net passing the
data it received that would be perfect. I thought that's what was going on
with your code example but for the life of me I can't figure out how to get
the .net delegate to fire when the java version of onReceiveMsgCardData()
fires.  More help please! :)

Below is the .net code - 

[Register("MagTest2/MagTest2/jni_helper", DoNotGenerateAcw = true)]
    public class YourJniHelper : Java.Lang.Object
    {
        public YourJniHelper()
        {
        }

        public YourJniHelper(IntPtr handle, JniHandleOwnership transfer)
            : base(handle, transfer)
        {
        }

        static IntPtr class_ref =
JNIEnv.FindClass("MagTest2/MagTest2/jni_helper");

        protected override Type ThresholdType
        {
            get { return typeof(YourJniHelper); }
        }

        protected override IntPtr ThresholdClass
        {
            get { return class_ref; }
        }

        static IntPtr id_InitializeReader;
        public void InitializeReader(Activity act)
        {
            try
            {

                if (id_InitializeReader == IntPtr.Zero)
                    id_InitializeReader = JNIEnv.GetMethodID(class_ref,
"InitializeReader", "(Landroid/app/Activity;)V");
                JNIEnv.CallVoidMethod(Handle, id_InitializeReader, new
JValue(act));

               
            }
            catch (Exception ex)
            {
                string err = ex.StackTrace;
       
            }
        }


        static IntPtr id_SwipeCard;
        public void SwipeCard()
        {
            try
            {

                if (id_SwipeCard == IntPtr.Zero)
                    id_SwipeCard = JNIEnv.GetMethodID(class_ref,
"SwipeCard", "()V");
                JNIEnv.CallVoidMethod(Handle, id_SwipeCard);
            }
            catch (Exception ex)
            {
                string err = ex.StackTrace;

            }
        }


        static IntPtr id_onReceiveMsgCardData;

        [Register("onReceiveMsgCardData", "(B[B)V",
"Get_onReceiveMsgCardData_Handler")]
        public virtual void OnReceiveMsgCardData(sbyte flagOfCardData,
JavaArray<byte> cardData)
        {
            if (id_onReceiveMsgCardData == IntPtr.Zero)
                id_onReceiveMsgCardData = JNIEnv.GetMethodID(class_ref,
"onReceiveMsgCardData", "(B[B)V");
            if (GetType() == ThresholdType)
            {
                JNIEnv.CallVoidMethod(Handle, id_onReceiveMsgCardData,
                                new JValue(flagOfCardData),
                                new JValue(JNIEnv.ToJniHandle(cardData)));
                return;
            }
            JNIEnv.CallNonvirtualVoidMethod(Handle, ThresholdClass,
id_onReceiveMsgCardData,
                            new JValue(flagOfCardData),
                            new JValue(JNIEnv.ToJniHandle(cardData)));
        }

        static Delegate cb_onReceiveMsgCardData;
        static Delegate Get_onReceiveMsgCardData_Handler()
        {
            if (cb_onReceiveMsgCardData == null)
                cb_onReceiveMsgCardData =
JNINativeWrapper.CreateDelegate((Action<IntPtr, IntPtr, sbyte,
IntPtr>)n_OnReceiveMsgCardData);
            return cb_onReceiveMsgCardData;
        }

        static void n_OnReceiveMsgCardData(IntPtr jnienv, IntPtr lrefThis,
sbyte flagOfCardData, IntPtr n_cardData)
        {
            YourJniHelper __this =
Java.Lang.Object.GetObject<YourJniHelper>(lrefThis,
JniHandleOwnership.DoNotTransfer);
            using (var cardData = new JavaArray<byte>(n_cardData,
JniHandleOwnership.DoNotTransfer))
                __this.OnReceiveMsgCardData(flagOfCardData, cardData);
        }
    }

    public class MyJniHelper : YourJniHelper
    {
        public override void OnReceiveMsgCardData(sbyte flagOfCardData,
JavaArray<byte> cardData)
        {

        }
    }

--
View this message in context: http://mono-for-android.1047100.n5.nabble.com/JNI-Help-Please-tp5144171p5454753.html
Sent from the Mono for Android mailing list archive at Nabble.com.


More information about the Monodroid mailing list