Saturday 4 November 2017

multithreading - Upload sensor data from Android device to remote host

itemprop="text">


I want to develop an
Android application that satisfies the following
specifications:




  • Record
    data from a sensor (for example the accelerometer) at an
    approximate rate of
    10-30 Hz.

  • Upload this raw data to a remote server (for
    example using TCP
    sockets).

  • The user interface
    should be minimum, just a pair of buttons to start
    or stop the recording and
    transmission of the data.


  • All the process
    should be unnoticeable for the user and keep working
    when the screen goes off
    for several hours.

  • Battery life is not critical (it
    should last several
    hours).



Vision: I would
like to analyse in quasi-real time the sensor measurements of a group of users without
their intervention (apart from starting the
application).



After some research, I could
manage to perform these tasks separately and inefficiently. I've tried classes such as
Service and IntentService, SensorEventListener, etc. I don't know if I should use
Thread, Service or IntentService for each task. Specifically, I have serious problems to
communicate them.



My questions:





  1. What class(es)
    do you recommend to use in order to solve
    this
    problem?

  2. What task should be done on each
    of them?

  3. If the tasks are performed in different entities
    (threads, services,
    intentservices, etc.), how should I intercommunicate them
    (I'm
    thinking about the recording and uploading
    tasks)?




I am
asking for the best-practice structure to solve my problem. You do not need to go into
details in terms of developing/coding the
solution.



Thank you very much and feel free to
ask if something is not clear
enough.



David



UPDATE:



After
some more research and thanks to DROIDcoder, I manage to design a skeleton for my
app:





  • Main UI:
    Activity object presenting two buttons (start/stop) that
    will launch a Service
    with the usual startService/stopService
    methods

  • Background: Service
    object




As
this is only a proposition, I add it as an edition. I will post a detailed answer when
the whole system works.




The questions
remains: can you think about a better design for the
application?



Thanks again!



Answer




Finally what I have
done:



Issue 1: record data from a sensor on the
background for a long period of time.
Solved using the class
Service to initialize the sensor and listen for
callbacks.



Issue 2: communicate the Activity
class holding the UI with the Service class.

Solved using the
startService(Intent myMessage) method from the Activity
class combined with a switch in the onStartCommand() method
from the Service class to classify the message.



Issue 3: communicate the Service class with the
Activity class.
Solved registering a custom
BroadcastReceiver in the Activity and sending
Intents from the Service. I've used it to update a progress
bar (in the Activity) during the file uploading (in the Service). An exceptional
tutorial can be found href="http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html"
rel="nofollow
noreferrer">here.



Issue 4: upload
data to a remote server.
Solved using AsyncTask
inside the Service like in href="https://stackoverflow.com/questions/18289623/how-to-use-asynctask/18289746#18289746">this
site.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...