Jean Meyblum - Game Programer
Portfolio
  • Home
  • Projects
  • Scripts & Tutorials
  • About Me

Game Programer

Hi, I'm Jean Meyblum. I'm a Game Programer working mostly with UnrealEngine and Unity, and also using C++, C#, Java through my projects. That's for short. You're pleasantly invited to have a more specific look at some of my works and you can also have more information about me by clicking the button below.

Contact

Communication between an Android App and Unity

2/17/2014

48 Comments

 

Introduction

This tutorial shows how to have an android app communicating with an android unity application. It also shows how to communicate between the android java app default code and unity. I had to do such a thing a year ago for one of my teacher that wanted to process data from an external accelerometer directly into unity. He already had an custom android app he made, that can receive these data but no way to send them to a unity game.

I'll assume you already made some android app. For simplification, I won't speak about in default android app creation but just tell you what you need to know to get this done.

How to send information between two Android app

Sending App

There is a simple way to send data from a standard app to another one and it's called a Service. Basically, a service is something inside an app that can broadcast data to any other app that is configured to listen to it. It's a standard android class coming from android.app.Service that can be derived to be used in your app.

Let's have a look on how to that:
  • First of all, create a new class inside your sending app, deriving from android.app.Service. This class is the service that will send an Intent to the others apps.
  • Now you have to make your service running inside your app. To do so, you can start the service in any Activity simply by using startService(new Intent(this, MyService.class)); where MyService is the name I called my new service derived class.
  • When you start it, a new instance of the service is created and directly call its onStart method.

Here's an example of a really simple service, using an Handler and sending data each second. All instructions are commented for you to understand how it works:
  • You must now modify the AndroidManifest.xml file to let Android know this app has a service in it, otherwise it won't work. So you just have to add this line under the <application> section:

Receiving App

We will see now how to receive the data our sender sends us. As this receiver is made for Unity, it has no activity, and work as a plugin for Unity. 

That's what you must do:
  • Create a new android app without any activity
  • Make a derived class of android.content.BroadcastReceiver

Here's an example code that explains the nexts parts:
  • Compile it as a .jar file. Using Eclipse you can do it trhough Eclipse -> File -> Export -> Java -> Jar File, press next, check your project, uncheck all boxes in the right panel, uncheck jar compression, choose a destination file and press done.

Make Unity use our Java Receiver plugin

Importing the plugin

Unity has been made to be able to use external library in a very simple way.

  • Create this hierarchy of folders using the default Asset folder: Assets/Plugins/Android/ 
  • Move your .jar file in it

Now, we have to modify the AndroidManifest.xml Unity uses to add some specifications for our receiver plugin to work properly. The easiest way to get this file is to build our Unity project to run on Android. When we build it, Unity create a manifest during compilation, that can be found in the Temp folder of our project. Copy it and move it next to the .jar file in the Android folder.

  • Modify the manifest by adding these lines under the <application> section:
After <receiver android::name=... you have to add the exact and full location of your Receiver class inside your plugin package. And after <action android::name=... comes the name of the Intent you want to listen at. It must be the same as what you wrote in the Sender in the sendIntent.setAction(...) method.

Using our plugin

Everything is set up now for us to be able to receive data of an other app inside a Unity application. We just have to see now how to use it in a Unity Script:
We're done ! I hope you get all the necessary information to have this working for you. Don't hesitate to ask me if you have any questions or problems. I'll be very happy to help you !

I would like to thank Simon Philippe, a friend of mine, for having worked with me on this code.

As usual, excuse me for all my English mistakes, I'm not a native speaker.
48 Comments
Troy
2/23/2014 06:56:33 am

I was wondering if you have a second or how ever long it takes for you to help me set it up, but i need help to make my phone receive notifications from the unity app when it isn't in focus. if it is focused, there is no problem but there is when it is out of focus.

Reply
Jean Meyblum link
2/24/2014 02:45:15 pm

Depending on your goal here, you'll probably can't do it. If you want to the unity app to be the Service, I think adding this flag "Intent.FLAG_FROM_BACKGROUND" on your Intent will send it even if your app isn't in focus but unity game loop will stopped and there's no way I know to avoid this. You can only use OnApplicationFocus() or OnApplicationPause() to execute code when losing or getting focus...

Reply
Troy
3/1/2014 07:44:02 am

Ok I decided to just export the code and edit it in Eclipse. mind helping me make a toast be a notification in the notification bar?

cross
3/26/2014 11:27:21 am

Hi,
Thanks for the tutorial!
I need to send coordinates data continuosly from a node.js server to an android application and move objects with it. I'm wondering if the flow nodejs->android service->unity android app will be most convenient way to do it to avoid lags on the rendering or what option could be possible otherwise. Thanks for your help.

Reply
Jean Meyblum link
3/28/2014 03:06:33 am

Why not just having nodesjs -> java android -> unity without using another external app?

Reply
cross
3/29/2014 09:47:00 am

Yes, actually is nodejs -> java android(service) -> unity. I've been able to send text in JSON format following your tutorial, but now I want to parse it as a list of floats and send it to unity; I'm wondering if its better to parse the data on the unity side or the java side (and how to retrieve it on unity)

Jean Meyblum link
3/29/2014 05:07:24 pm

I think you better parse it on the unity side.

Andrew
3/30/2014 06:26:50 am

Thanks for the great tutorial Jean! I need to do something similar where I pass sensor values to unity. I am a little confused about the sending activity that starts the service, is that something running in the background? I am asking because the Unity App (receiver) would be the app that's active on the screen. Could I potentially start the service in the Unity App by extending UnityPlayerActivity? Thanks for any assistance

Reply
Jean Meyblum link
3/30/2014 07:29:42 am

I never tried to do it that way but I think there is no reason it shouldn't work. If it works please tell me, I'm curious to know.

Reply
Andrew
3/30/2014 10:05:02 am

I definitely will. I guess my confusion was how you had both the send and receive app running at the same time since the unity app is the one that has focus

Jean Meyblum link
3/30/2014 01:56:31 pm

Intents special flag : Intent.FLAG_FROM_BACKGROUND

Java Projects in chennai link
5/29/2014 10:11:54 pm

VLSA Global Services specializes in final year projects training and guidance, based on the very latest research publications in journals of international reputation like IEEE. Every year we revise our project offerings to the latest publications. We are offering projects for ME, M Tech, BE and B Tech students of ECE, EEE, E&I and other related domains based on IEEE 2013 publications

Reply
Alex Lopez link
6/12/2014 02:34:41 pm

would I be able to keep playing music in the background in unity by using this method? I'm building a music app that requires the music to keep playing even after you press the home button

Reply
nina
8/26/2014 11:06:13 am

hi , i want to do exactly the reverse of what you presented. I need to send an order to activate an android method from unity. For more detales, on pressing a light swich in unity , i need to send a socket which will call and android method responsable of turning the light on for real. I don't know how to do it . Can you help me please!

Reply
Jean Meyblum link
8/26/2014 12:05:38 pm

Hi Nina, I think you just need to use the last part of my tutorial. Use a AndroidJavaClass corresponding to your Java class file and call a static function with jc.CallStatic(..) that will turn on the light. (Static is more simple to use as you don't have to care about how to deal with Unity java object creation).

Reply
Luqeas
11/14/2014 07:23:10 pm

Hi Jean!
I have question, what is difference beetween this what you use in your code:

AndroidJavaClass jc = new AndroidJavaClass("java.lang.System");
jc.CallStatic("gc");

and this code:

Application.ExternalCall("gc");

It is only example, but I dont know where is difference or if I I can use Application.ExternalCode for call method from Unity.

PS1: Sorry for my english :)!
PS2: Sorry for funny question , but I am new in Unity !

Thank you for answer :)

Jean Meyblum link
11/15/2014 01:11:54 am

Application.ExternalCall() is for calling external Javascript for web unity application. The other one is for calling a static method of a class in Java for Android unity application. There is no link between Java and Javascript. They are two completly different languages. One is used for client-side web page the other one is used in our case as the core language to create android apps.

nina
8/28/2014 07:38:41 pm

Thank you Jean, I will try it :)

Reply
Rafal
9/10/2014 10:10:28 pm

Hi, i tried your tutorial and its ALMOST working for me :) I made a stackoverflow thread for my problem, if anyone could help me with it.

https://stackoverflow.com/questions/25786922/android-sendbroadcast-to-unity-broadcastreceiver-not-working

Thanks for the tutorial, very nice :)

Reply
Tamino
10/20/2014 12:53:03 am

Hey, thanks for this tutorial.
I always get the error message "Exception: JNI: init'd AndroidJavaClass with null ptr!"
My path for the MyReceiver class should be "InterfacePlugIn/src/MyReceiver" but i still get this null pointer exeption.
Thanks for any help!

Reply
Jean Meyblum link
10/20/2014 07:49:15 am

Thank you for your comment!
It's a little bit general for an exception... Maybe you're not using static function but an object method on a non-instantiated object. It could be something else... Try to begin with a simple example if you not already have, be sure it works and add your personal feature after. Let me know if you have more precision on your problem.

Reply
pey
1/24/2016 01:29:11 am

Anybody having this issue, register the receiver on the plugin's android_manifest also(jar which we import in unity). This should resolve

songsak
1/31/2015 03:14:20 pm

hello, Jean thanks for this tutorial.
i finish to do this tutorial but i have a problem about how to implement this tutorial for send intent from unity back to android app. Could u tell me about this problem ?

Reply
Font Android link
3/17/2015 02:41:59 am

thanks for this tutorial, i tried this tutorial and its really working for me

<a href="http://www.androiddigit.com/2015/03/cara-mengubah-font-android-dengan-fontomizer.html">Font Android</a>

Reply
Android Apps Developer link
4/27/2015 05:44:53 pm

Hi this is John, I am working in an Android Apps Developer company. I have read your highly informative post. Its great... I have some queries regarding your blog... Can we talk over email or skype???

Reply
ZAxis
5/19/2015 01:02:25 am

hello, Jean thanks for this tutorial.
i finish to do this tutorial but i have a problem about how to implement this tutorial for send intent from unity back to android app. Could u tell me about this problem ?

Reply
DED
6/22/2015 10:55:59 pm

Hi, nice tutorial. Maybe it's a little bit late, but I've got a question about the class MyReceiver: why do you create a static instance of the class? Since "text" is already static, I'd say it's not necessary to create an object, since the variable would be available just having the class. I have created a test in which, from Unity, I just get the text without creating an instance of MyReceiver, and it works correctly.

Reply
Sarp
7/12/2015 10:19:06 pm

I have read your highly informative post. I applied it to my need in Unity 5 but did not work. I created another simple project by copying your codes and applying the information in post, that also gives the error message "Exception: JNI: init'd AndroidJavaClass with null ptr!". I tried so many long times. Service is working in android active page, but no success in Unity 5. Any idea? Is there any problem with Unity 5?

Reply
Nitish singla
10/7/2015 07:23:47 am

how to use it in a Unity Script: u didn't mentioned here.

Reply
SungMin Jeon link
12/30/2015 05:18:54 am

Hello? I thanks for your awesome tuto. but, I can't run my program by runtime error. When I check my program by logcat, that said No Unityplayeractivity to me...
What I want to say is how can you run this program without any unityactivity?

Reply
David Harris
2/23/2016 11:15:41 am

Hey jean thanks for the tutorial.
one question will this work between unity android app to unity android app if i turn the sending app into a .jar File and make a plugin for unity?

Reply
Saints Row 3 Cheats link
5/14/2016 11:56:49 pm

I'll assume you already made some android app. For simplification, I won't speak about in default android app creation but just tell you what you need to know to get this done.

Reply
Android App Development link
12/19/2016 11:54:46 pm

I am always searching online for articles that can help me. There is obviously a lot to know about this. I think you made some good points in Features also. Keep working, great job

Reply
Malik Zulqurnain
12/29/2016 03:17:28 am

Thanks for the tutorial Can you please help me how can i get notification from my unity game I am searching for it from 15 days.
here is the question from where i got your link.

http://stackoverflow.com/questions/41375612/continue-running-time-in-background-unity-and-pop-notification?noredirect=1#comment69954606_41375612

Reply
Musab
1/12/2017 11:47:48 am

Hi, thanks for tutorial. it is really helpful in some way.
is this communication possible in my case like i'm going to send data through kinect(kinect to unity or direct) to android? i don't think so. i'm have tried it but e'm a bit in trouble..
so if you have any solution just let me know...

Reply
Hargobind Gupta
3/8/2017 11:47:16 pm

have you answered below question anywhare,

How to send data from Unity and recieve it in Android app.?

Reply
Muhammad
7/19/2017 11:58:50 pm

I can't see images now in this tutorial. I was able to see yesterday. Does anybody has the unity class? I have created the jar. Now need to connect to unity

Reply
Albert
11/16/2017 03:29:58 pm

This doesn't work anymore

Reply
James
11/21/2017 01:01:00 pm

Is this working with current Unity version? Trying to make it work but new AndroidJavaClass is always returning null.

Reply
sandeepsmartest
1/16/2018 09:35:21 am

Great Tutorial!!is there by any chance to push a notification, if unity app is minimised. I have tcp sockets listening to server for data in my unity android app, though unity app minimised. also i could trigger some code(adding data to separate lists ) in unity.
Basically its a turn based game if server tells unity app about his turn ,i have to push a local notification indicating user's turn.

Reply
Rastislav
5/9/2018 03:41:14 am

Good day,
I did everything according to the tutorial but after overwritten the manifest in unity the application does not start.
Can you help me pleasy?

Reply
peter
6/30/2018 07:30:51 am

hi Jean
i was very happy when I have seen your tutorial
"Communication between an Android App and Unity" and How to send information between two Android app
but I am very new i programming in Android
so it was unfortunnelly to diffecoult for me to find out where to put in the different code parts and so - du you have the sending and the recieving parts on github so I can use them directly or can you guide a lithe hiw to de
Thank you very much
/peter

Reply
Ben
7/9/2018 10:36:13 pm

everything seems to work but the onReceive() is never called. I believe that i have set up my manifests correctly but it still does not work? Anyone had any luck recently?

Reply
A.
3/29/2019 02:48:28 am

Hi! Thanks four your tuto!! I haven't tried it yet, but, is there any possibility to make calls betweent two UNITY ANDROID apps¿?

Thanks in advance!!

Reply
Asad Ali link
7/17/2019 05:31:35 am

That was useful. thanks

Reply
joseph link
7/18/2019 04:42:16 am


Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care.As always, we appreciate your confidence

Reply
Jeeth Bopaiah
1/7/2020 10:45:53 pm

I have used this resource to implement the communication, I have no problem communicating from unity but if I want to communicate event after starting a unity intent can I do it using this way or is it only possible when starting the intent of unity as shown in this project.
Any way to send messages from native android event after starting the activity using this method.

Reply
Himanshu singh
5/4/2020 08:27:37 pm

what is IntentToUnity in the sendIntent.setAction("com.test.sendintent.IntentToUnity");?

Reply



Leave a Reply.

    Categories

    Tous
    Unity
    UnrealScript

    Archives

    Mars 2014
    Février 2014

    RSS Feed


Powered by Create your own unique website with customizable templates.