- Home /
Unity on android controlling arduino
Hello! I want to build a gui interface for my phone with which i could controll an arduino connected to the phone via USB and i was wondering if that is possible in unity ? all i found was for PC and i was wondering if its possible with android
why USB ? you can use Bluetooth : Android & $$anonymous$$icrocontrollers / BLuetooth
Answer by supernat · Apr 07, 2014 at 01:45 AM
The only 2 kickers might be with utilizing USB and having to pay for Unity Pro, but it most likely is possible.
You can create your own Android Java classes, drop them into the Plugins folder under Android, and access them in your JS or C# scripts using the JNIHelper and other JNI classes that Unity provides.
Additionally, you can drop other libraries that you didn't write into the same folder (assuming they are very modular and don't require anything beyond Unity's limitations, but generally speaking the only limitation I know of is the minimum supported SDK.)
Lastly, you can get plentiful access to the contexts and main activity through specific Unity function calls on the Java side, and Unity provides an API to send messages back from Java into your Unity scripts.
JNIHelper allows you to allocate instances of Java classes, call methods of those classes, and call methods of static classes (no allocation needed). I generally keep everything static, or at least as much as I can, to keep it simple.
For instance, if you have Arduino jars and want to call the methods to them, you can drop those jars into the Plugins/Android directory and use the JNIHelper class to directly communicate with them. Note, I believe you need Unity Pro (or mobile pro?) to use plugins, so this may be a limiting factor in cost for you.
Also, the JNI layer is very slow (it won't keep you from running at 30 hz or anything) and very small. You can only send small numbers of strings synchronously (like 512 by default I think) before the JNI overflows and crashes your app. This means even if you sent only 511 strings in an array once each frame, you're in trouble, because garbage collection hasn't had time to clean the JNI stack. So then you have to build in some delay between sending large string arrays (or anything else). Just keep this in mind in your design.
If you're just sending small numbers of bytes each frame, you should be fine.
Learning Android is not too difficult (easier than learning JNI), and I would say that if you don't need cross-platform support, you should probably just do the app in native android code (use Android Studio IMO), unless you want more than just UI (i.e. pretty graphs, cool 3D animations, etc), then use Unity if you can.
Answer by device2014 · Jul 06, 2014 at 04:06 PM
why USB ? , there's a plugin that allow you to add Bluetooth connection between your Android device and Arduino. Android & Microcontrollers / Bluetooth
isn't any other plugin allow to access arduino bluetooth via iOS?
Your answer
Follow this Question
Related Questions
Arduino - Unity3d communication via wifi 1 Answer
Send data from Android to Arduino via RPi 3 WiFi 0 Answers
How to use BLE pluging 1 Answer
How to use open accessory of android in unity? 0 Answers
Android Bluetooth 0 Answers