Interact with C++ program from android using parcelable class - android

Basically, I have a C++ program that finds the sum of two numbers given. I need to provide the two numbers to the C++ program as input using my android app and then display the result in my android app. I guess I need to use parcelable class. Can someone please tell me the steps to be followed?
Edit: I forgot to mention that the C++ program that I intend to communicate with is an executable program (sum.exe)

To run a C++ executable on Android, you can use something like Runtime.exec("sum 1 2"). There are a lot of tutorials, e.g. https://www.mkyong.com/java/how-to-execute-shell-command-from-java/. The output (stdout and stderr) can be parsed, too. A more sophisticated way is to use ProcessBuilder, but the idea is the same.
If you want your executable to keep running in background, and send the numbers to crunch once in a while, you can either use input pipe, or some IPC protocol. Shared memory works well, see e.g. How to use Shared Memory (IPC) in Android.

You can use JNI code, take a look here:
https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo025
where you can find super simple code with C++ being called via JNI wrapper from Java.

Related

Matlab audioread/audioplay into C/C++ through Coder

I am working on a matlab project where I add effects to audio files (mp3, wav). Therefore, I load the files into arrays using the matlab function audioread(..).
Now, I want to export this to Android. I read that the best way is to use the Matlab Coder to export the matlab code to C/C++ (or Java) and then export it into android (more or less).
However, the function call audioplayer (and play) are Unsupported (that's what the code generation readiness issues says).
What can I do ? One idea was to play the sounds directly using c++ code (so after the code generation). But how to play sounds from arrays using c++ ?
Or if you guys have others ideas without touching c++ codes (so fixing the problem directly in matlab), I would be glad to hear it !
Thanks and have a good day !
Typically what I recommend in cases like this is to factor your code in two pieces:
The part that does the audio file I/O and audio playing (namely the OS-specific part)
The computational kernel for which you will generate code using MATLAB Coder. This piece usually takes numeric arrays representing the image or audio data as arguments.
I've used this approach to leverage MATLAB Coder generated code to do image filtering on Android.
To do part (1), as Navan says, you'll need to use Android APIs to read in audio files, write data back to files, and to play them as desired. Note, I haven't done extensive Android development, so doing these tasks may take some research or be difficult.
Once you have the data in a format suitable for the function(s) in (2), likely a numeric array, then you can call your generated code using JNI to add the desired effects. The generated code would return the data back to the Java code and you can then encode it, play it, or do as you please with it using the Android APIs.
Playing audio normally uses platform dependent libraries. In DSP System toolbox, there is an audio player object called dsp.AudioPlayer which supports C code generation. But I believe this uses platform dependent libraries in the generated code and it will not be straight forward to make it work in Android. You will be better off finding an audio player library for Android and hooking that in manually after generating code.

Android C++ IPC using Boost.Interprocess - should it work?

Background:
I have working C++ code on linux that uses Boost IPC to access shared memory, and I want to port it to android. I downloaded and built the Boost-for-Android project found here: https://github.com/MysticTreeGames/Boost-for-Android.
Problem:
However when I try to create a boost named mutex like this:
boost::interprocess::named_mutex named_mtx(boost::interprocess::open_or_create, "my_mutex");
I get an exception saying "no such file or directory" with a native code 2.
Additional information:
When I searched for how to use shared memory on android, it looks ashmem and Binder are popular methods, and I can't find references to them at all in the ported Boost IPC code.
Questions:
What is the reason for the "no such file or directory" error?
Can someone confirm that the Boost-for-Android IPC part works?
By default Boost does not look for a common place where to share data on Android. It was not built this way. To make it work modify the file
/boost/interprocess/detail/os_file_functions.hpp
Find the following line an add /sdcard
const char *names[]={ "/sdcard", ......
After doing that use the library and give to your application Read/Write external storage permission.
You are good to go.
PD: Please be carefull. I have problems using Mutex and conditional variables in Android because the process was taking 100% the CPU.
I followed the way provided by #user3645767 but it didn't work. But I solved it by revision the file 'interprocess/detail/shared_dir_helpers.hpp'
line 109
to change the dir_path in get_shared_dir_root()
#elif defined __ANDROID__
dir_path="/data"
#else
dir_path="/tmp"

JNI, NDK, and OpenCV

I have recently begun using the Android NDK and I have successfully implemented a few simple Android apps. I need to detect objects (squares and rectangles) from an image. My research has shown me that OpenCV is the solution for this. This is the algorithm I use to detect a square from the image.
However, I am unclear as to how should I use the squares.cpp file in my code. The OpenCV samples show how to use the cpp files in JNI format. Do I need to convert the squares.cpp file to JNI or would there be another feasible solution?
Thanks. All suggestions and feedback are welcome.
You don't have to convert the squares.cpp file to JNI.
From your Java code, you will call a JNI function (as I suppose you did in the "few simple Android apps" you have implemented) that will then call the functions in squares.cpp.
In other words, you basically only need one call to a JNI function from Java, and once you are in the C++ code, you can code in C++ as usual.

memory usage and codes in android

sorry if it's a silly question. Do comments in the java or xml file effect the memory usage of the android application? has anyone tried to monitor the memory usage of his/her application with and without the comments?
No, comments do not use any memory.
It's important to understand that, in programming in C, Java, etc. what you're writing is source code which, before being run on the computer (or, specifically, your Android device) is compiled into a machine code format. The processor does not run your source code as you see it. The source code you write typically contains lots of stuff like comments (which do NOT have any effect on the actual code) or perhaps things like compiler directives (which may control how the compiler compiles sections of your code).
(I realise it's more correct to use the term byte code in the case of Java, but trying to keep the answer simple here.)
An exception to this however would be if you're talking about the case where you insert a file (e.g. XML file) as a raw resource within your Android application. But, I think this topic is an advanced one for you to learn about later.
Comments in your code are compiled out and have no effect whatsoever on memory usage in an application.

Replace string in a function at runtime Android NDK

I have an external compiled static C++ library that I'm using in my android application. This library is reading a file. I want to know if there is a way I can "redirect" the function that's reading the file so that it reads another file.
So if it does:
fopen("myfile.txt", "rb");
I want to intercept it and to do this instead:
fopen("myotherfile.txt", "rb");
In Objective-C I use MethodSwizzling. Is there something similar I can do in C++ or the android NDK?
Short of editing the binary (with uncertain results), your best option is to use a symlink... if you're just doing it for development purposes, you could use adb shell into your test device to create the symlink.
http://developer.android.com/guide/developing/tools/adb.html#issuingcommands
How about contact the author of the library and ask them to introduce a parameter? Having hard-coded file paths is a lousy design anyway, the library will be better off.

Categories

Resources