- Home /
Sending String of Data through JNI to Android Plugin
I have an android plugin (java) that I am using the JNI interface to communicate to, In this plugin I must send a String in a method call and seem to be fudging something up
Code:
Setting up JNI (works for all other functions so my Class Object is valid and working)
meth_SendCommand = JNI.GetMethodID(cls_TactorPlayer,
"SendCommand", "(Ljava/lang/String;)I");
Sending the Data
String str_command = "Hello Plugin!";
IntPtr ptr = Marshal.AllocHGlobal(str_command.Length);
Marshal.Copy(str_command.ToCharArray(), 0, ptr, str_command.Length);
int returnval = JNI.CallIntMethod(TactorPlayer, meth_SendCommand,ptr);
Capturing the Data within my Java Class
public int SendCommand(String command)
{
Log.d("--UNITYJAR-- SendCommand=", String.valueOf(command));
return 0;
}
Printing out my intptr that I send shows a valid address But I am guessing the way I Create the intptr to copying the data into it may be my issue
Help is greatly appreiciated, I am running out of ideas!
Thanks
Greg
Answer by Fenyx4 · Mar 22, 2011 at 04:16 PM
I had good luck using the AndroidJNI to skip the C++ part. With it you could, in a Unity script, do something like this;
//Grab the object AndroidJavaObject javaObject = new AndroidJavaObject("com.myCompany.myStuff.myClass"); javaObject.Call("SendCommand",command);
Sorry if you need the C++ parts and this doesn't apply to you.
Your answer
Follow this Question
Related Questions
Now that Unity 4.1.2 broke the Android plugin examples, what do you use to learn them? 1 Answer
How can I browse files on Android outside of the Unity App folder? 2 Answers
Getting byte[] or ByteBuffer[] from native Java 1 Answer
Using AndroidJavaObject.CallStatic to retrieve a return value 0 Answers
[Android] Call .jar function on render thread using JNI 0 Answers