As you can see in architecture diagram below android platform has been built using different layers.
Application are developed in Java
Application Framework is written using Java (according to my understanding)
Libraries are in C/C++
For some insane reason I have to play/deal with devices like accelerometer, compass and camera using C/C++ which means directly accessing them in 3rd layer i.e. Libraries. According to my understanding the Application Framework itself would be consuming Libraries for accessing these devices and then providing APIs for Applications.
I am looking for any documentation/tutorials/demo which can help me in this regard i.e how to access and use these devices like camera, accelerometer and compass from C/C++ code or in other words how to play with these devices directly from Libraries layer.
My last option would be to get the android source code and dig deep into it to find out what I am looking for but I would like some easy way in form of a documentation/demo/tutorial/anything that can make this a bit easy for me.
I am looking for any documentation/tutorials/demo which can help me in this regard i.e how to access and use these devices like camera, accelerometer and compass from C/C++ code or in other words how to play with these devices directly from Libraries layer.
You don't. You access them from Java code. Reorganize your C/C++ code to support your Java code.
For the camera, you can use opencv to access the frames with a c++ library. For the Accelerometer, I'm looking for how to access using c++.
Related
For example, I want to create a simple application based on GPS, with making waypoints, showing them on map, etc.. So, is it possible to make such an app using C++ only, without any Java sources? Would it be more difficult than making the same on Java?
So, is it possible to make such an app using C++ only, without any
Java sources?
No. If you want to receive GPS coordinates, there is no way to do this without any Java code.
You could write an app in which Java is used as a thin wrapper around native code, using JNI to exchange data between Java and C++. However...
Would it be more difficult than making the same on Java?
Yes! In addition, the app would likely end up being:
Slower.
Buggier.
Harder to understand and maintain.
For Android development, Java is just the natural, normal, default language, and C++ is for exotic special tasks, typically those which involve really intensive calculations. You use it when you need it, not because you don't "want to" write in Java or because "Java is slow".
Writing correct JNI code is also not exactly trivial. For example, it's very easy to get local references and global references wrong if you don't read the documentation, as the compiler cannot detect their incorrect usage.
As the official documentation of the Android Native Development Kit says:
Before downloading the NDK, you should understand that the NDK will
not benefit most apps. As a developer, you need to balance its
benefits against its drawbacks. Notably, using native code on Android
generally does not result in a noticable performance improvement, but
it always increases your app complexity. In general, you should only
use the NDK if it is essential to your app—never because you simply
prefer to program in C/C++.
It also says:
You cannot access features such as Services and Content Providers
natively, so if you want to use them or any other framework API, you
can still write JNI code to do so.
Yes, search for Android NDK. Apparently it's a bit of a hassle, you'll be using SO a lot!
I am a little new to all these so please bear with me if the question sounds a little dumb. I am doing a project on comparing the extends on using GPU for map visualization and spatial analysis on mobile devices (Android, basically). I have decided to leverage upon the JTS topology suite which offers a variety of analysis (Triangulation, point in polygon and the like) and have implemented these functions in Android without the use of GPU (mainly running it on CPU).
However, I would like to bring these functions onto the GPU through RenderScript, but have been unable to reference the different variables in RenderScript. These are types such as GeometryFactory, Point, Polygon, Coordinates which I want to use in the RenderScript C file.
Hence, should I download the C library version of JTS (GEOS, basically) and use it in RenderScript? And if so, how should I go about to implement it? (I am not exactly competent in C) Or is there a way to set the different variables in the RenderScript C file via Java?
Should you require any details:
I am using Android Developer Tools with Eclipse, JTS 1.13
Thank you!
As an example, I would like to do something like:
(in Java)
import jtslibrary.*;
but implement it in RenderScript so that it can recognize the variable type.
two years ago i developed an Augmented Reality framework on android-7 (Eclair). Since AR application are computationally intensive task, I developed a JNI c++ library used by a Java activity to render and register the virtual environment. The sensor readings acquired in Java are passed to the underline c++ library to compute the registration of the virtual environment. Tridimensional objects are rendered by a native draw function called from a GLSurfaceView. This results in a lot of JNI call.
Now I would like to port the application to android-15(Ice Cream Sandwich). Starting from android-9(Gingerbread) Android allows to use NativeActivity.
I would like to understand which is the better way to develop an AR application. Since every JNI calls introduce an overhead it would be much better to avoid them. Is it possible using NativeActivity? I didn't find an exaustive guide that explains how NativeActivity works but reading this document it seems that it results in a lot of JNI calls anyway. Is there any architectural document that explains how NativeActivity works? Is NativeActivity just a "JNI wrapper" to avoid java code? Concerning performances,are there any advantages using NativeActivity instead of a JNI library as I done before?
Thanks a lot.
NativeActivity will not give a performance boost to your framework. It still uses JNI to communicate with the System, only under the cover.
Moreover, there are good reasons not to use it. If I understand your purpose correctly, you want other applications to take advantage of your code. By forcing them to use NativeActivity you seriously reduce their freedom, and require that they struggle with a less familiar environment. There is a number of limitations with NativeActivity, e.g. it cannot load more than one JNI library.
Finally, I would suggest a completely different direction if you look for optimization of your AR framework: you can use the new setPreviewTexture() API.
As far as I understand it you still are bound to JNI also when using NativeActivity. This class can be used as starting point and encapsulates some functionalities for your convenience but the underlying technology to access native code has not changed and ist still JNI. So in my opinion you only can do some benchmarks to check if NativeActivity is more efficient for some reason (may be the guys at Google do know some hacks that make it faster than your solution).
I am currently messing around with Android beginning to make an app. The application connects with a bluetooth device, then sends it signals, then the other device sends back the data that was requested.
I was given documentation for the other devices API written in C, which basically stores all the specific signals needed to send the device. How can I change these from C files to java files. I have downloaded the Android NDK but don't know what to do with it. Any help would be appreciated.
You can call the C library from your Android Java code, (if i understood you correctly this is what you wanted to do).
You will need NDK and will need to use JNI to allow the java code to talk to the c library.
There are plenty of good tutorials on these just search.
For example
Marakana has a good tutorial/video on Android internals. Take a look at the part where he talks about building applications with JNI support. He uses example of a library in C that calculates fibonacci sequence but he calls that library from Java.
I have a question about the limitations of what you can do in native code on the Android platform.
Basically I have developed a library in native C code that uses UDP sockets for SIP/RTP and uses OpenAL for audio recording/playback - basically the whole application.
The idea is to have as much as possible in native C code rather than Java code. I want to do this because I am going to use it on other platforms as well.
My question then is simply - is it possible to just use the Java for the GUI and then all processing in native code?
What will happen when my native code tries to create a socket, bind it, record audio, play it, etc - since it is in native code, do I need to setup permissions for it (such as application accessing microphone and whatnot) or will it just bypass this stuff since its native code?
Can native code do pretty much anything it wants on Android like on PCs?
Sorry if its unclear; just tell and I'll try to improve it
Thanks
You can do pretty much anything you want in native code, but the only OS-level thing really supported is OpenGL, OpenSL, and some number-crunching libraries (compression, math, etc).
However, at any time you're free to use the JNI to call a Java method, so you could use the standard Android API for networking (classes like Socket, etc). Obviously, since the call is going through the Java API, all the normal Android permissions apply (like android.permission.INTERNET).
EDIT: As elaborated in the comments, the standard libraries that are part of the NDK do have support for sockets.
You still need your application to have permissions. For example, your native sockets will not work without android.permission.INTERNET in the manifest.
<manifest xlmns:android...>
...
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
Another option is to create the socket at the Java layer and pass it down. Here's an example of interacting with the socket in native land, see the method org_..._OpenSSLSocketImpl_connect():
http://www.netmite.com/android/mydroid/dalvik/libcore/x-net/src/main/native/org_apache_harmony_xnet_provider_jsse_OpenSSLSocketImpl.cpp
The Native Android API is a nice article for NDK.
is it possible to just use the Java for the GUI and then all processing in native code?
Yes. And you need to set appropriate permissions to your AndroidManifest.
record audio, play it,
You need to use OpenSL ES API for recording and playing audio in the native side. It means your application should be for Android 2.3 or later.
Or, NVIDIA provides a framework that allow we to be able to develop using C++ for Android events, sensors, audio and so on even though for Android 2.2 or earlier.
Tegra Resources - Android SDK & NDK sample applications and documentation
You may like to check out csipsimple which is a example of using the pjsip sip library (which is written in C) in a java android application.
I haven't looked into how it does the sockets communication but it should give you a more complete example of what you are trying to do.