Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Warren 1 · May 04, 2011 at 06:40 PM · androidarrayreturnandroidjavaobjectjni

Returning byte array from Java/Android

I have several functions that make use of AndroidJavaObject.Call() to run Java code in my Unity project.

AndroidJavaObject androidUtility = new AndroidJavaObject("com.my.java.library");

androidUtility.Call("myFunctionA", data1, data2);

androidUtility.Call<bool>("myFunctionB", data1, data2);

androidUtility.Call<byte[]>("myFunctionC", data1, data2);

The functions all get called. The call to myFunctionB returns a boolean. But the call to myFunctionC returns an empty array no matter what I put in the Java code.

Does AndroidJavaObject.Call() not work when returning arrays? Is there some special processing required?

Thanks for your help.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tamas · Mar 07, 2012 at 10:50 AM 0
Share

bump! :) Does not work for us either. All we get is a warning and a crash, when we try to get arrays, lists from java code: JNI WARNING: received null array (Check_GetArrayLength)

3 Replies

· Add your reply
  • Sort: 
avatar image
14
Best Answer

Answer by supernat · Aug 10, 2013 at 09:00 PM

This seems to be an issue with little knowledge, so I thought I would reply even though this thread is quite old.

The problem is that an array is not a native type, so you can't do what you are trying in the example. Arrays, as far as I can tell, must always be transferred in the form of an Object. The Unity JNIHelper class offers a method (ConvertFromJNIArray()) to change an AndroidJavaObject into a .NET array. The array has to be an array of native types from what I can tell (except string, which isn't native, but it still works). There is also a ConvertToJNIArray that you can use to send an array to the Java side. Hopefully the code below will shed some light on it.

 C# code:
     AndroidJavaClass cls = new AndroidJavaClass("com.mycompany.myproduct.myclass");
     AndroidJavaObject obj = cls.CallStatic<AndroidJavaObject>("getStringArray");
     if (obj.GetRawObject().ToInt32() != 0)
     {
       // String[] returned with some data!
       String[] result = AndroidJNIHelper.ConvertFromJNIArray<String[]>
                             (obj.GetRawObject());
       foreach (String str in result)
       {
         // Do something with the strings
       }
     }
     else
     {
       // null String[] returned
     }
     obj.Dispose();
     cls.Dispose();
 
 java code:
 public static String[] getStringArray()
 {
   String[] strs = { "Test1", "Test2" };
   return strs;   // Or alternatively, return null;
 }

Pay special attention to checking for null by using the obj.GetRawObject().ToInt32() method and testing it for 0. obj itself will not be null as it is a valid object, but if you call ConvertFromJNIArray(obj) with the obj's Raw Object value of 0, it will crash.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by tamas · Mar 07, 2012 at 12:14 PM

Also here is an example from the (docs):

 // Create a java.lang.String object holding the string "some string",
 // and retrieve it's hash code.
 function Start() {
     var jo = new AndroidJavaObject("java.lang.String", "some string");
     var hash = jo.Call.<int>("hashCode");
 }

Modified version, to return an array:

 function Start() {
     var jc = new AndroidJavaClass ("com.test.test");
     var doubleArary = jc.Call.<double[]>("getDoubleArray");
 }

From logcat:

 03-07 11:43:35.143: I/Unity(23708): getMethodID("com.test.test", "getDoubleArray", "()[D", non-static)
 03-07 11:43:35.143: I/Unity(23708): FOUND double[] test.getDoubleArray();
 03-07 11:43:35.153: W/dalvikvm(23708): JNI WARNING: received null array (Check_GetArrayLength)

The called java method:

 public double[] getDoubleArray() {
     return new double[] { 1, 2, 3 };
 }


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tamas · Mar 07, 2012 at 04:35 PM 0
Share

Also..help would be appreciated, cause our current workaround, is reading the list items one-by-one, and it's really slow when you have 1000+ items.

avatar image
0

Answer by bhavinatmattel · Jul 17, 2018 at 06:45 PM

Using Supernat's answer
1. you can feed bytes to POJO

public class ReadData {
    public byte[] Buffer;
    public ReadData(byte[] buffer) {
        Buffer=buffer;
    }
  }

2. Call your method in Android (Java) code
callback.DidReceiveDataCallback(new ReadData(data));


3. Render the bytes in Unity
public void DidReceiveDataCallback(AndroidJavaObject bytesObj)
        {
            Debug.Log("Unity callbacks: Receive Data ");
            AndroidJavaObject bufferObject = bytesObj.Get
  
   ("Buffer");
            byte[] buffer = AndroidJNIHelper.ConvertFromJNIArray
   
    (bufferObject.GetRawObject());
           // Do something with bytes
        }

   
  

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

AndroidJavaObject.Call fails with 'method not found' 1 Answer

What is the difference between AndroidJavaClass.Call and AndroidJavaObject.Call 2 Answers

Getting native Android String constants via JNI broken in Unity 2019.2.0? 3 Answers

Count every row and column of multidemensional array (c# unity) 1 Answer

JNI Exception using Android Plugin 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges