1 package org.iotivity.service.sample.androidbundle;
\r
3 import android.content.Context;
\r
4 import android.hardware.Sensor;
\r
5 import android.hardware.SensorEvent;
\r
6 import android.hardware.SensorEventListener;
\r
7 import android.hardware.SensorManager;
\r
8 import android.util.Log;
\r
10 import org.iotivity.service.resourcecontainer.AndroidBundleResource;
\r
11 import org.iotivity.service.resourcecontainer.RcsResourceAttributes;
\r
12 import org.iotivity.service.resourcecontainer.RcsValue;
\r
15 * Created by markus.jung on 11/26/2015.
\r
17 public class LightSensor extends AndroidBundleResource implements SensorEventListener {
\r
18 private static final String LOG_TAG = AndroidLightResource.class.getSimpleName();
\r
19 private final SensorManager mSensorManager;
\r
20 private final Sensor lightSensor;
\r
22 public LightSensor(Context context){
\r
24 this.setResourceType("oic.r.lightsensor");
\r
25 this.setName("lightSensor");
\r
26 mSensorManager = (SensorManager) context.getApplicationContext().getSystemService(Context.SENSOR_SERVICE);
\r
27 lightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
\r
28 mSensorManager.registerListener(this, lightSensor, SensorManager.SENSOR_DELAY_NORMAL);
\r
32 protected void initAttributes() {
\r
33 this.m_attributes.put("intensity", 0);
\r
37 public void handleSetAttributesRequest(RcsResourceAttributes attrs) {
\r
38 Log.i(LOG_TAG, "Set Attributes called with ");
\r
39 for(String key: attrs.keySet()){
\r
40 Log.i(LOG_TAG, " " + key + ": " + attrs.get(key));
\r
45 public RcsResourceAttributes handleGetAttributesRequest() {
\r
46 Log.i(LOG_TAG, "Get Attributes called");
\r
47 Log.i(LOG_TAG, "Returning: ");
\r
48 for(String key: m_attributes.keySet()){
\r
49 Log.i(LOG_TAG, " " + key + ": " + m_attributes.get(key));
\r
51 return this.m_attributes;
\r
55 public void onSensorChanged(SensorEvent sensorEvent) {
\r
56 Log.i(LOG_TAG, "Sensor event " + sensorEvent.values[0]);
\r
58 setAttribute("intensity", new RcsValue( (int) (sensorEvent.values[0]) ) , true);
\r
62 public void onAccuracyChanged(Sensor sensor, int i) {
\r