Screen Off Sensor Off

Hi Everyone,

This my code at the following. I want to send data over TCP to the server every 1 second. When screen off, sensor callback stops but TCP connection is still active. When the screen on , then send data is continuing. How can I always on the sensors permanently.

#include <tizen.h>
#include <service_app.h>
#include <privacy_privilege_manager.h>
#include "service.h"
#include "sensor.h"
#include "power.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#define SA struct sockaddr
int sockfd, connfd;
struct sockaddr_in servaddr, cli;


#define PORT     	11090
#define MAX 		80

int error;
bool supported;

sensor_h sensor;
sensor_listener_h listener;

void connectToPort(){

	// socket create and varification
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if (sockfd == -1) {
		dlog_print(DLOG_DEBUG, LOG_TAG, "socket creation failed...\n");
	}
	else
		dlog_print(DLOG_DEBUG, LOG_TAG, "Socket successfully created..\n");
	bzero(&servaddr, sizeof(servaddr));

	// assign IP, PORT
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = inet_addr("192.168.43.1");
	servaddr.sin_port = htons(PORT);

	// connect the client socket to server socket
	if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {
		dlog_print(DLOG_DEBUG, LOG_TAG, "connection with the server failed...\n");
	}
	else
		dlog_print(DLOG_DEBUG, LOG_TAG, "connected to the server..\n");
}

void func(int sockfd)
{
    char buff[MAX];
    int n;
    for (;;) {
        bzero(buff, sizeof(buff));
        dlog_print(DLOG_DEBUG, LOG_TAG, "Enter the string : ");
        n = 0;
        while ((buff[n++] = getchar()) != '\n')
            ;
        write(sockfd, buff, sizeof(buff));
        bzero(buff, sizeof(buff));
        read(sockfd, buff, sizeof(buff));
        dlog_print(DLOG_DEBUG, LOG_TAG, "From Server : %s", buff);
        if ((strncmp(buff, "exit", 4)) == 0) {
        	dlog_print(DLOG_DEBUG, LOG_TAG, "Client Exit...\n");
            break;
        }
    }
}

void on_sensor_event(sensor_h sensor, sensor_event_s *event, void *user_data)
{
	char temp[100];
   sensor_type_e type;
   sensor_get_type(sensor, &type);

   if( type ==  SENSOR_ACCELEROMETER)
   {
	   dlog_print(DLOG_DEBUG, LOG_TAG, "ax:%f  ay:%f  az:%f\r\n", event->values[0], event->values[1], event->values[2]);
   }
   else if( type ==  SENSOR_HRM)
   {
	  dlog_print(DLOG_DEBUG, LOG_TAG, "HR : %f\r\n", (event->values[0]));
   }
   else if( type ==  SENSOR_HUMAN_PEDOMETER)
   {
	   dlog_print(DLOG_DEBUG, LOG_TAG, "Step : %d   Distance : %d\r\n", (int)(event->values[0]), (int)(event->values[3]) );
   }

   sprintf(temp, "HR : %f\r\n", (event->values[0]));


  if( write(sockfd, temp, strlen(temp)) == -1 ){

   connectToPort();

  }


}

bool service_app_create(void *data)
{
	sensor_type_e type = SENSOR_ACCELEROMETER;

	error = sensor_get_default_sensor(type, &sensor);
	error = sensor_create_listener (sensor, &listener);
	error = sensor_listener_set_event_cb(listener, 1000, on_sensor_event, NULL);
	error = sensor_listener_set_option(listener, SENSOR_OPTION_ALWAYS_ON);
	error = sensor_listener_start(listener);

	type = SENSOR_HRM;

	error = sensor_get_default_sensor(type, &sensor);
	error = sensor_create_listener (sensor, &listener);

	error = sensor_listener_set_event_cb(listener, 500, on_sensor_event, NULL);
	error = sensor_listener_set_attribute_int(listener, SENSOR_ATTRIBUTE_PAUSE_POLICY, SENSOR_PAUSE_NONE);
	error = sensor_listener_set_option(listener, SENSOR_OPTION_ALWAYS_ON);
	error = sensor_listener_start(listener);

	type = SENSOR_HUMAN_PEDOMETER;

	error = sensor_get_default_sensor(type, &sensor);
	error = sensor_create_listener (sensor, &listener);
	error = sensor_listener_set_event_cb(listener, 1000, on_sensor_event, NULL);
	error = sensor_listener_set_option(listener, SENSOR_OPTION_ALWAYS_ON);
	error = sensor_listener_start(listener);

    // Todo: add your code here.
    return true;
}

void service_app_terminate(void *data)
{
    // Todo: add your code here.
    return;
}

void service_app_control(app_control_h app_control, void *data)
{
	app_control_set_operation(app_control, APP_CONTROL_OPERATION_MAIN);
    // Todo: add your code here.
    return;
}

static void
service_app_lang_changed(app_event_info_h event_info, void *user_data)
{
	/*APP_EVENT_LANGUAGE_CHANGED*/
	return;
}

static void
service_app_region_changed(app_event_info_h event_info, void *user_data)
{
	/*APP_EVENT_REGION_FORMAT_CHANGED*/
}

static void
service_app_low_battery(app_event_info_h event_info, void *user_data)
{
	/*APP_EVENT_LOW_BATTERY*/
}

static void
service_app_low_memory(app_event_info_h event_info, void *user_data)
{
	/*APP_EVENT_LOW_MEMORY*/
}

int main(int argc, char* argv[])
{
    char ad[50] = {0,};
	service_app_lifecycle_callback_s event_callback;
	app_event_handler_h handlers[5] = {NULL, };

	event_callback.create = service_app_create;
	event_callback.terminate = service_app_terminate;
	event_callback.app_control = service_app_control;



	service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
	service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, service_app_low_battery, &ad);
	service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, service_app_low_memory, &ad);
	service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &ad);
	service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &ad);



	return service_app_main(argc, argv, &event_callback, ad);
}

Hi,
You may try with alarm API.
Check below link with similar discussion -

It is very informative and helpful to me. I admire the message valuable information you provided here.

1 Like