[mono-android] JNI Help Please
Jonathan Pryor
jonp at xamarin.com
Fri Feb 3 19:16:38 UTC 2012
It would be handy to have the .jar file to test against (or something with the same API). That said..
On Feb 3, 2012, at 1:51 PM, digitalml wrote:
> I used your code to call the jni_helper and initialize the reader; however,
> the manged wrapper for the onReceiveMsgCardData() is never called.
Are you calling InitializeReader()? I'm sure you are, but it doesn't hurt to check...
> I can't figure out how to get the .net delegate to fire when the java version of onReceiveMsgCardData() fires.
Before n_OnReceiveMsgCardData() is invoked, Get_onReceiveMsgCardData_Handler() will be invoked, and Get_onReceiveMsgCardData_Handler() will only be invoked if:
1. There's an android callable wrapper which registers the onReceiveMsgCardData() method; the `MyJniHelper` class should do so.
2. The android callable wrapper instance is created. The android callable wrapper static constructor is responsible for registering the methods, so if you never instantiate a e.g. `MyJniHelper` instance, then the method will never be registered (and the method would never be invoked, as there's no instance to invoke it on).
Thus, at minimum your code will need to do:
var h = new MyJniHelper();
h.InitializeReader();
I would also suggest putting a trace into Get_onReceiveMsgCardData_Handler() to see if it's being invoked.
> 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;
> }
> }
1. Why are you wrapping InitializeReader() in a try/catch?
2. Why are you swallowing the exception?
If this throws, you want to know about it. :-)
The same is true for SwipeCard() -- in general, exceptions are exceptional; you want to know when they happen (and having the app die is a good way of knowing, at least during development), so that you can fix them.
Thanks,
- Jon
More information about the Monodroid
mailing list