Developing a small monitoring system. The server should send the sensor data to the client (Android 4.2) every 3 seconds via the Internet (on the 3G client).

How to organize a communication channel? There are options:

  1. Permanent socket connection.
  2. GET requests from phone to server.

The phone should not go to sleep. The approximate work time is 8 hours / day (the PowerBank will be connected).

What is better in terms of power consumption? And is there something else more effective?

  • Will the phone display the received data? - Mikhail Vaysman
  • Can use ordinary pus for data transfer? - temq

2 answers 2

Once every 3 seconds - this is very often, almost continuously. The only energy optimization that you can talk about in this case is to extinguish the screen.

Further read about WakeLock , Doze Mode and the request to the "white list". Short:

Intent intent = new Intent(); String packageName = context.getPackageName(); PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); if (pm.isIgnoringBatteryOptimizations(packageName)) intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS); else { intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); intent.setData(Uri.parse("package:" + packageName)); } context.startActivity(intent); 

Then, once every 3 seconds it may fail, IP does not guarantee speed and delivery. Immediately think about what should be with your data, if there was a delivery delay of 30 seconds? If the server was unavailable for 2 minutes? The choice of protocol depends on it. In the simplest case, you can use either your own protocol on top of TCP, or a ready-made WebSocket implementation. HTTP for such a request rate is a bad idea.

    I recommend POST + JSON . I would use these technologies. Send by event (depending on your project). On energy consumption, I think that the picture will not be very different. That is, with different options ( GET , POST , Socket ) in time of discharge, I think that the picture will be approximately the same. It even affects not so much the connection as the launch of the 3G Internet connection services. That is, the most economical picture of energy consumption can be seen with a minimum number of requests (for example, if you send a request every 5 seconds or 10, the difference will be significant). That is, if the Internet service (3G) went to sleep (for example), we very much save the battery. For significant energy savings, it is necessary to proceed from the change in the sensor value (if possible), but this depends on the type of sensor. It is possible, depending on the sensor changes in the response itself from the JSON server (for example) to insert a variable after how many seconds to send the next request , this will make it possible to minimize the number of requests and create significant battery savings (if the sensor parameters have not changed for three minutes, then why do polling every three seconds). That is, to reduce the amount of launch of the Internet service Android .

    If the type of sensor and the project itself makes it possible to reduce the number of requests (and the use of the Android Internet service), then this will significantly increase the battery life. If you just need to transfer one variable every strictly 3 seconds, then you will not see a significant difference with different connections, for an android system, it’s still the same as you launch the Internet (it matters a lot how many times per minute or hour, in terms of power consumption).