Converting a project from C to Java - android

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.

Related

Are there any simple way to use c/c++ source code directly on my android application?

I am currently working on an android application that evaluate images in different aspects, and I found that there are lots great open source algorithms can be used.
Problem 1: Most of the algorithms are designed on c/c++/matlab languages that cannot be applied directly.
I've search that NDK is a tool that allows us develop android application by other languages, but the setup procedures are quite complicated that I stuck for days. So before I go further on it, I would like to first ask whether I can include other's c/c++ source code directly like calling java library?
Problem 2: For example, I would like to use Point Matching algorithm's source code in my application, but there are lots files inside as it's just source code but not library/plugin. What are the steps to apply the require functions in my android application?
(the most desired way is to blindly input some images to the alogrithm, and it returns the results like calling functions, which I dont have to understand the working principle of it.)
You can't directly use other C++ libraries, you have to build them for Android first using NDK. If there is a version of the library built for Android, then, of course you can use it directly by linking to it using NDK.
You have two options here. First, you create a regular Java application for Android, write a C++ wrapper for handling calls to native side and build the necessary source files using NDK. Your java application will make calls to wrapper using JNI and the wrapper will call your actual C++ functions as in Java->JNI wrapper on C++->Your actual C++ source.
Second option is going fully native, which will leave out Java, JNI calls and the wrapper. You can access your source files directly as if you were writing a desktop C++ application. In the end you will have to build using NDK but this step is required in any case.
As a suggestion, you can also take a look at OpenCV for image processing purposes. It has libraries built for Android, all you will have to do is to link them.
Short version.
Download opencv4android library. Import it in eclipse and see if everything is fine (compile errors, output, etc.).
Secondly, try to import face detection application and try to understand how it works.
It has a java part and a native part.
In order to understand how it works you must understand how java interacts with C++, more or less how NDK works.
One important aspect is to learn how to create your interfaces in C++, based on the native ones defined in java. When you got there then you can try creating your own application.
After that you can come here and ask specific questions.

Communication between an android c native application and an android application

I want to ask if someone know how to make communicate a native c android application and an classic java android application, the native c android application running in background.
Thanks.
First thing you need to do is download the android-ndk available here. It comes with it's own documentation which will be available here: [android-ndk path]/documentation.html, You can also find information in the android-ndk link above.
If you're anything like me you'll need as much help as you can get developing a JNI bridge. I had to find these resource myself but they were invaluable to me. See below for JNI information:
Table Of Contents
Methods (older resource)
Methods (newer resource)
Types
Reference
You can integrate your NDK/C++ code and debug it from Eclipse using Sequoyah and CDT (C/C++ Development Tooling)
More specifically to answer your question - You can use a service as a wrapper around your native code if you require the native code to run in the background. This will enable you to push information to the native code using the OS's intent mechanism in a fifo structure.

Android NDK limitations?

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.

How to play with devices like accelerometer and camera using C/C++?

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++.

Using a DLL with C interface in Java (for Android)

I was looking into writing an app for Android platform that would (hopefully) use a DLL with a C interface. The only way to grab information from the server is through this API. Is this even possible? If so, could anyone give me a point in the right direction?
Thanks
I was looking into writing an app for Android platform that would (hopefully) use a DLL with a C interface.
"DLL" is a Windows term. You cannot use a Windows DLL on Android. You will need C code that can work on Linux, as Android is a Linux-based operating system.
The only way to grab information from the server is through this API. Is this even possible?
Is it possible to create a C library for use on Android? Yes. See the Native Development Kit (NDK).
Is it possible to create a server that can only be accessed by some C library? Probably not without a lot of work, if that server is accessible from the Internet. Anybody can try hitting that server, or can reverse-engineer your library, or can perform packet inspection on your library-to-server communications.
Did you try Mono?
Disclaimer: I have used in on Linux and (it works) but not on android.

Categories

Resources