Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by blacq_shadow · Sep 19, 2017 at 12:22 AM · androidscripting problemscripting beginnerjavapositioning

How to implement listeners in unity3d android plugin?

I am trying to build a unity 3d android plugin. Although there are a few tutorials on accomplishing this task (including the unity documentation). I could not find/ didn't understand how to implement java event handlers into unity as I am quite new to unity/ android development.

For instance: Obvious things like an AndroidJavaClass Object in Unity can only be used to call static functions took me a while to figure out.

I haven't been able to solve the following issue.

Android Library

This is an indoor-positioning system (Indoor Atlas), determines the users' position without the use of GPS

 import com.indooratlas.android.sdk.IALocation;
 import com.indooratlas.android.sdk.IALocationListener;
 import com.indooratlas.android.sdk.IALocationManager;
 import com.indooratlas.android.sdk.IALocationRequest;
 import com.indooratlas.android.sdk.IARegion;
 
 public class UnityLocation
         implements IALocationListener, IARegion.Listener{
 
     // Location Manager requests location updates
     private IALocationManager mIALocationManager;
     private Context currentContext;
 
 // These two function will be called from unity to start location services
     public void setContext(Context context)
     {
         currentContext = context;
     }
     public void startLocationServices()
     {
         Log.d("TESTING", "Start Location services function called");
         mIALocationManager = IALocationManager.create(currentContext);
         final IARegion region = IARegion.floorPlan("1e7d3247-9aab-4de9-903e-aa0fcccd3b45");
         mIALocationManager.setLocation(IALocation.from(region));
         IALocationRequest request = IALocationRequest.create();
         mIALocationManager.requestLocationUpdates(request,this);
     }
 
 // This is the function where the coordinates are retrieved listener
     @Override
     public void onLocationChanged(IALocation iaLocation) {
 
         // Log the received coordinates to the console.
         Log.d("LOCATION UPDATE", "LATITUDE: " + iaLocation.getLatitude());
         Log.d("LOCATION UPDATE", "LONGITUDE: " + iaLocation.getLongitude());
     }
 }

If I call the two functions setContext() and startLocationServices() from any activity in android studio, the coordinates are printed to the console and are updated as I move around the room.

But if I try to call the same two function from a unity script, even though the object is initialised and I can call functions, nothing is printed to the console.

Unity Script

 jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); 
         if(jc == null)
             Debug.Log("unable to get unity activity class");
         
         jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
 
 
         if (jo == null)
             Debug.Log ("unable to get current activity");
         else {
             Debug.Log ("Current activity read");
         }
         //display.text.text = "UNITY ACTIVITY";
         //location = new double[2] { 0, 0 };
 
         plugin = new AndroidJavaObject ("edu.deakin.sp9.indooratlaslibrary.UnityLocation");
         if (plugin == null)
             Debug.Log ("unable to get plugin class");
         else
             Debug.Log ("Plugin class retrieved");
 
         //display.text.text = "Code reached plugin class";
         plugin.Call ("setContext", jo);
         plugin.Call("startLocationServices");


My guess is that the listener in this case "onLocationChanged" needs to be manually implemented in unity, but I have no idea how this can be done, reading the unity documentation of AndroidJavaProxy was no help. Any help would be greatly appreciated.

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

190 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Player Prefs Dosen't work on android 0 Answers

Unity Editor Freezes- Help! Urgent! 1 Answer

Working with 2 scripts 2 Answers

How to get jvm in untiy script or Android NDK Plugin 0 Answers

[Unity 5 / Android] How to set Non-fullscreen by Default? 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