Back to project page HueWatch.
The source code is released under:
Apache License
If you think the Android project HueWatch listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.philips.lighting.quickstart; //from w w w . j av a 2 s . c o m import java.util.Hashtable; import java.util.List; import java.util.Random; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import com.philips.lighting.hue.listener.PHLightListener; import com.philips.lighting.hue.sdk.PHHueSDK; import com.philips.lighting.model.PHBridge; import com.philips.lighting.model.PHHueError; import com.philips.lighting.model.PHLight; import com.philips.lighting.model.PHLightState; /** * MyApplicationActivity - The starting point for creating your own Hue App. * Currently contains a simple view with a button to change your lights to random colours. Remove this and add your own app implementation here! Have fun! * * @author SteveyO * */ public class MyApplicationActivity extends Activity implements SensorEventListener { private float mInitX, mInitY, mInitZ; private PHHueSDK phHueSDK; private static final int MAX_HUE=65535; private static final int MAX_SATURATION=254; private static final int MAX_BRIGHTNESS=254; public static final String TAG = "QuickStart"; private boolean mInitialized; private SensorManager mSensorManager; private Sensor mOrientation; private Sensor mGravity; private final float NOISE = (float) 2.0; private Boolean changeLights = false; private float prevBrightness = 0; private int hue = MAX_HUE; private int saturation = MAX_SATURATION; private int brightness = MAX_BRIGHTNESS; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTitle(R.string.app_name); setContentView(R.layout.activity_main); phHueSDK = PHHueSDK.create(); Button randomButton; randomButton = (Button) findViewById(R.id.buttonRand); randomButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { enableChange(); } }); // Too many requests makes the Hue slow to respond. Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { public void run() { updateHue(); } }, 500, 350); mInitialized = false; mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mOrientation = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR); mGravity = mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY); mSensorManager.registerListener(this, mOrientation, SensorManager.SENSOR_DELAY_NORMAL); mSensorManager.registerListener(this, mGravity, SensorManager.SENSOR_DELAY_NORMAL); } public void updateHue() { if (!changeLights) { return; } PHBridge bridge = phHueSDK.getSelectedBridge(); List<PHLight> allLights = bridge.getResourceCache().getAllLights(); for (PHLight light : allLights) { PHLightState lightState = new PHLightState(); System.out.println(String.format("Updating light, hue: %d, sat: %d, bright: %d", hue, saturation, brightness)); lightState.setHue(hue); lightState.setSaturation(saturation); lightState.setBrightness(brightness); // To validate your lightstate is valid (before sending to the bridge) you can use: // String validState = lightState.validateState(); // bridge.updateLightState(light, lightState, listener); bridge.updateLightState(light, lightState); // If no bridge response is required then use this simpler form. } } public void enableChange() { changeLights = !changeLights; } @Override protected void onDestroy() { PHBridge bridge = phHueSDK.getSelectedBridge(); if (bridge != null) { if (phHueSDK.isHeartbeatEnabled(bridge)) { phHueSDK.disableHeartbeat(bridge); } phHueSDK.disconnect(bridge); super.onDestroy(); } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // can be safely ignored for this demo } protected void onResume() { super.onResume(); mSensorManager.registerListener(this, mOrientation, SensorManager.SENSOR_DELAY_NORMAL); } protected void onPause() { super.onPause(); mSensorManager.unregisterListener(this); } @Override public void onSensorChanged(SensorEvent event) { if (!changeLights) { return; } // System.out.println(event.sensor); // System.out.println(event.sensor == mGravity); if (event.sensor == mOrientation) { rotationChange(event); } else if (event.sensor == mGravity) { gravityChange(event); } } public void rotationChange(SensorEvent event) { // float z = event.values[0]; float y = event.values[1]; // System.out.println(String.format("sensor change X: %.2f", z)); System.out.println(String.format("sensor change Y: %.2f", y)); hue = (int)((MAX_HUE / 2) + ((MAX_HUE / 2) * (y * 2))); // int saturation = (int)(MAX_SATURATION + ((MAX_SATURATION / 2) * z)); } public void gravityChange(SensorEvent event) { float x = event.values[0]; float y = event.values[1]; float z = event.values[2]; // System.out.println(String.format("Gravity X: %.2f Y: %.2f Z: %.2f", x, y, z)); // brightness = (int)(MAX_HUE + ((MAX_HUE / 2) * z)); float nextBrightness = prevBrightness + (float)z; if (Math.abs(nextBrightness - prevBrightness) < 1) { return; } if (nextBrightness > 254) { nextBrightness = 254; } else if (nextBrightness < 0) { nextBrightness = 0; } // System.out.println(nextBrightness); prevBrightness = nextBrightness; } }