Languages

Menu
Sites
Language
How can I run my code in background by using service app? (Samsung Gear S2)

Hello.

I'm very confused about my service app.

I'm using HRM sensor in my service app.

but if Gear S2 screen is turn off, my service app doesn't work on.

static int real_cnt = 0;
static int cnt = 0;
void _HRM_value(sensor_h sensor, sensor_event_s *sensor_data, void
    	*user_data)
{
	char buf[PATH_MAX];
	appdata_s *ad = (appdata_s *) user_data;
	real_cnt++;

	if(sensor_data->values[0] > 10) {
		cnt++;
		ad->heart_rate += sensor_data->values[0];
	}
	if(real_cnt ==5 ) {
		sprintf(buf, "%0.1f", ((ad->heart_rate) / cnt));
		send_data(buf);
		cnt = 0;
		real_cnt = 0;
		ad->heart_rate = 0;
	}
}
void
start_heartrate_sensor(appdata_s *ad)
{
    //start_acceleration_sensor(ad);
	if (sensor_get_default_sensor(SENSOR_HRM, &(ad->sensor)) == SENSOR_ERROR_NONE)
	{
		if (sensor_create_listener(ad->sensor, &(ad->listener)) == SENSOR_ERROR_NONE
				&& sensor_listener_set_event_cb(ad->listener, 1000, _HRM_value, ad) == SENSOR_ERROR_NONE
				&& sensor_listener_set_option(ad->listener, SENSOR_OPTION_ALWAYS_ON) == SENSOR_ERROR_NONE)
		{
			//sensor_listener_set_interval(ad->listener, 3000);
			if (sensor_listener_start(ad->listener) == SENSOR_ERROR_NONE)
			{
				//LOGI("Sensor listener started.");
			}
		}
	}
}

This is my code in service app.

When Gear S2 Screen off, this code does not work well.

void _HRM_value(sensor_h sensor, sensor_event_s *sensor_data, void
    	*user_data)
{
	char buf[PATH_MAX];
	appdata_s *ad = (appdata_s *) user_data;

        sprintf(buf, "%0.1f", sensor_data->values[0]);
	send_data(buf);	
}
void
start_heartrate_sensor(appdata_s *ad)
{
    //start_acceleration_sensor(ad);
	if (sensor_get_default_sensor(SENSOR_HRM, &(ad->sensor)) == SENSOR_ERROR_NONE)
	{
		if (sensor_create_listener(ad->sensor, &(ad->listener)) == SENSOR_ERROR_NONE
				&& sensor_listener_set_event_cb(ad->listener, 3000, _HRM_value, ad) == SENSOR_ERROR_NONE
				&& sensor_listener_set_option(ad->listener, SENSOR_OPTION_ALWAYS_ON) == SENSOR_ERROR_NONE)
		{
			//sensor_listener_set_interval(ad->listener, 3000);
			if (sensor_listener_start(ad->listener) == SENSOR_ERROR_NONE)
			{
				//LOGI("Sensor listener started.");
			}
		}
        }
}

But If I use this code, my service app run fine when Gear S2's screen off.

What should I do ?..

View Selected Answer

Responses

1 Replies
Mark as answer
Shaswati Saha

Hello,
You can try CPU lock. So far I know, it means display can be off but not the cpu. Your app can even run in the screen off state.

int error;
error = device_power_request_lock(POWER_LOCK_CPU, 0);

// do you job

error = device_power_release_lock(POWER_LOCK_CPU);

Also please go through the link below:

https://developer.tizen.org/ko/development/api-references/native-application?redirect=/dev-guide/2.3.1/org.tizen.native.mobile.apireference/group__CAPI__SYSTEM__DEVICE__POWER__MODULE.html&langredirect=1

Thanks.