Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send data to Matlab from Android/Java

I spent some time searching for a way to send data from a android application to Matlab - with no approach. I would prefer to do it with JSON via a Restful webservice.

I probably have the wrong concept in mind how this is going to work.

Matlab should be running/waiting for POST requests from my android device to receive the data, bring it into matlab form from json , progress it and send it back - than wait again for new requests.

The "RESTful web service" like "webread" seems not to wait for incoming data and go active for them.


How to let Matlab listen for incoming data with json ? or how to let Matlab receive data from Android/java based programms ? Do i need another frameworks, api's or even a server with Database to get that done ?

Can anyone give me some hints ?

like image 323
Ghali Avatar asked Nov 16 '15 17:11

Ghali


2 Answers

Approach 1:

Matlab also provides Matlab Mobile https://de.mathworks.com/products/matlab-mobile.html, which is capable of executing Matlab code from your device, however, it is not possible to send images to Matlab.

However, you can use WebCam https://play.google.com/store/apps/details?id=com.pas.webcam&hl=en and open up a Server, which is pretty straightforward. You can run the app in the background and then connect to Matlab via Matlab-Mobile, and access it via your IP address and usually Port 8080.

Approach 2:

You can use a WebSocket -Server which is implemented here:

https://de.mathworks.com/matlabcentral/fileexchange/50040-jebej-matlabwebsocket

For more information on how to get it to run you can follow the directions given on the GitHub readme, here: https://github.com/jebej/MatlabWebSocket

A WebSocket Server is on the highest layer of the 7th layer (application layer) of the OSI model https://en.wikipedia.org/wiki/OSI_model and builds op on the 4th layer (TCP). However, you do not need to specify such things as buffer size etc.

The following example code is directly taken from the example code from the GitHub project. To fulfill the desired outcome in the Android application it is the best approach to rebuild the client application on Android.

Echo Server:

classdef EchoServer < WebSocketServer
    %ECHOSERVER Summary of this class goes here
    %   Detailed explanation goes here

    properties
    end

    methods
        function obj = EchoServer(varargin)
            %Constructor
            obj@WebSocketServer(varargin{:});
        end
    end

    methods (Access = protected)
        function onOpen(obj,conn,message)
            fprintf('%s\n',message)
        end

        function onTextMessage(obj,conn,message)
            % This function sends an echo back to the client
            conn.send(message); % Echo
        end

        function onBinaryMessage(obj,conn,bytearray)
            % This function sends an echo back to the client
            conn.send(bytearray); % Echo
        end

        function onError(obj,conn,message)
            fprintf('%s\n',message)
        end

        function onClose(obj,conn,message)
            fprintf('%s\n',message)
        end
    end
end

To run it in MATLAB type:

s = EchoServer(30000);

This will then utilize the port 30000 on your local machine.

On Android simply create a WebSocket-Client and use your URI, which you can find out by using ipconfig (windows) or ifconig (Linux). In Android the uri should like the following:

ws://192.168.1.102:30000 

Where the IP address may change according to your IP address

like image 159
Kev1n91 Avatar answered Nov 17 '22 21:11

Kev1n91


Here are my 2 cents.

Your approach seems right.

Step 1: You need to run a web server using MATLAB on your device. By going through Web Server, it looks like you can use it to run webserver, and execute a .m file when a POST or GET request is made to your server.

Step 2: Lets say that your server is accepting requests on port 8080. From your Android device, if you are on same network then you can make a HTTP POST request to http://your.ip.address:8080 and extract the data and execute your code in .m file.

Note: You can also get a public URL for your local server running on the device using ngrok utility. Then make a POST request to that public URL. You need not be on the same network then in order to make a request. Here is some explanation: Accessing localhost from android over wifi.


Edit: Additional question says:

Matlab is possible of receiving data via TCP/IP client, but how does the android site need to do the POST/GET algorithm and how can response Matlab responsible to it?

Let me rephrase what I understand. Firstly, you want to know how from Android code, one can do a POST/GET request and secondly how will Matlab respond to the request?

  • In Android you can make a POST request in background thread either using AsyncTask (Android HttpURLConnection with AsyncTask Tutorial) or if you want to do it properly, you can use Retrofit library to do POST/GET calls (Using Retrofit 2.x as REST client - Tutorial ).
  • When using WebServer as mentioned in the link previously, when the .m file gets executed on the POST call, you can send the response to the POST request from there. On Android, where you initiated the call, you can receive the callback.

Hope this helps a bit.

like image 4
Shobhit Puri Avatar answered Nov 17 '22 20:11

Shobhit Puri