I am migrating from a classical java android application through xamarin. My Problem is related using a Service Connection through a Android System Service, when passing the iBinder object through JNI for usage in native c++ code. More specific i am trying to connect to the legacy Tango Service on my Asus Zenfone AR. When accessing the jobject in c++ i get a huge stacktrace with the following exception:
art/runtime/java_vm_ext.cc:470] JNI DETECTED ERROR IN APPLICATION: IsInstanceOf received NULL jclass
art/runtime/java_vm_ext.cc:470] in call to IsInstanceOf
art/runtime/java_vm_ext.cc:470] from void md50f4250d048afa4418b536451c1ad07e8.TangoServiceConnection.n_onServiceConnected(android.content.ComponentName, android.os.IBinder)
My code includes a BroadcastReceiver for controlling the service lifecycle as well as the service connection itself.
public class TangoServiceConnectionReceiver : BroadcastReceiver { public TangoServiceConnection mTangoServiceConnection = new TangoServiceConnection();
public override void OnReceive(Context context, Intent intent)
{
if (intent.Action.Equals(MainActivity.TANGO_SERVICE_CONNECT))
{
JNITangoWrapper.BindTangoService(context.ApplicationContext, mTangoServiceConnection);
}
if (intent.Action.Equals(MainActivity.TANGO_SERVICE_DISCONNECT))
{
context.ApplicationContext.UnbindService(mTangoServiceConnection);
JNITangoNative.OnTangoServiceDisconnected();
}
}
};
public class TangoServiceConnection : Java.Lang.Object, IServiceConnection
{
public void OnServiceConnected(ComponentName name, IBinder service)
{
JNITangoNative.OnTangoServiceConnected(JNIEnv.Handle, System.IntPtr.Zero, service.Handle);
}
public void OnServiceDisconnected(ComponentName name)
{
}
}
The JNITangoNative wrapper passes the handles through JNI
[DllImport("library", EntryPoint = "Java_com_SOMEPACKAGE_TangoJNINative_onTangoServiceConnected")]
public static extern void OnTangoServiceConnected(IntPtr jniEnv, IntPtr thiz, IntPtr nativeTangoServiceBinder);
JNIEXPORT void JNICALL
Java_com_SOMEPACKAGE_TangoJNINative_onTangoServiceConnected(
JNIEnv *env, jobject /*caller_object*/, jobject iBinder) {
app.OnTangoServiceConnected(env, iBinder);
}
In app.OnTangoServiceConnected
void App::OnTangoServiceConnected(JNIEnv *env, jobject iBinder) {
TangoErrorType ret = TangoService_setBinder(env, iBinder);
if (ret != TANGO_SUCCESS) {
LOGE("App: Failed to set Tango binder with error code: %d", ret);
std::exit(EXIT_SUCCESS);
}
The app crashes with the already provided exception, when TangoService_setBinder(env, iBinder); is called. I tested a lot my self and the jClass of the iBinder jObject is android.os.BinderProxy the same as in the working java android app.
I should mention again, that the exact same application flow is working in my java android prototype.
I know this is a legacy service, but i need it to develop my application in a certain way. I would be happy if anyone can provide additional information or tips and feel free to ask for more detailed information if needed.
Here is some additional output from the Visual Studio log:
Time Device Name Type PID Tag Message
Asus _A002 Info 12143 TangoIHelper Success! Using default/libtango_client_api.
Asus _A002 Info 6338 TangoCore-UninstallReceiver Tango permissions revoked for Mono.Android.Platform.ApiLevel_24
Asus _A002 Info 6338 TangoCore-UninstallReceiver Uninstall detected.
Asus _A002 Info 6338 TangoCore-UninstallReceiver Tango permissions revoked for com.companyname.Xamarin
Asus _A002 Info 6338 TangoCore-UninstallReceiver Uninstall detected.
Asus _A002 Info 6338 TangoCore-UninstallReceiver Tango permissions revoked for com.companyname.Xamarin
Asus _A002 Info 6338 TangoCore-UninstallReceiver Tango permissions revoked for Mono.Android.DebugRuntime