Setting up Philips Hue with Android

Philips Hue:

Philips Hue is an intelligent light collection; it will help you control your home’s lights using a smartphone, tabs, etc. Philips Hue can welcome you home, wake you up, get you energized, help you feel safe, improve your mood and much more.

Philips Hue uses two communication protocols to control the bulbs: Wifi/Ethernet and ZigBee.

Installation Steps:

  • Connect your bridge with a wifi router and electricity plug.
  • Plugging smart bulb to bulb holder in wifi network.
  • Install the Philips application on your smartphone to connect with the hue bridge.
  • After app installation, connect with hue bridge using a smartphone and after on connect with bulbs.
  • The app is compatible with Android or iOS operating systems.

How it works:

In the below picture, you will get an exact idea of how it works.
Android Philips Hue

  • Smartphone with Android or iOS or a desktop application device, using that user controls the smart bulbs.
  • A router that gets data from devices and forwards it to the hue bridge.
  • A bridge, get data and set commands for smart bulbs.
  • Smart bulbs get commands from the hue bridge and work as per the given command.

A few days ago, I got a chance to work on this technology. It’s very interesting!!! Hope you also enjoy it.

Here I will give you one example using the Android platform. In this example, we will see how we will connect with bridges and connect with bulbs ON/OFF bulbs using mobile.

Steps To Connect With Bridge :

Need to create a “PHHueSDK” instance.
We can directly get the quick start demo of hue Philips; click here.

// Gets an instance of the Hue SDK.
PHHueSDK phHueSDK = PHHueSDK.create();

// Set the Device Name (name of your app). This will be stored in your bridge whitelist entry.
phHueSDK.setAppName(getResources().getString(R.string.app_name));
phHueSDK.setDeviceName(Build.MODEL);

// Register the PHSDKListener to receive callbacks from the bridge.
phHueSDK.getNotificationManager().registerSDKListener(listener);

Search available bridges in a network

PHWizardAlertDialog.getInstance().showProgressDialog(R.string.search_progress, PHHomeActivity.this);
PHBridgeSearchManager sm = (PHBridgeSearchManager) phHueSDK.getSDKService(PHHueSDK.SEARCH_BRIDGE);
// Start the UPNP Searching of local bridges.
sm.search(true, true);

Using PH Listeners we can get the result of search bridge methods :

public interface PHSDKListener {

void onCacheUpdated(List<Integer> cacheNotificationsList, PHBridge bridge);

void onBridgeConnected(PHBridge bridge, String userName);

void onAuthenticationRequired(PHAccessPoint accessPoint);

void onAccessPointsFound(List<PHAccessPoint> accessPoints);

void onError(int code, String message);

void onConnectionResumed(PHBridge bridge);

void onConnectionLost(PHAccessPoint accessPoint);

void onParsingErrors(List<PHHueParsingError> parsingErrors);

}

// Local SDK Listener

private PHSDKListener listener = new PHSDKListener() {
   @Override

   public void onAccessPointsFound(List<PHAccessPoint> accessPoint) {

       Log.w(TAG, "Access Points Found. " + accessPoint.size());

       PHWizardAlertDialog.getInstance().closeProgressDialog();

       if (accessPoint != null && accessPoint.size() > 0) {

               phHueSDK.getAccessPointsFound().clear();

               phHueSDK.getAccessPointsFound().addAll(accessPoint);

               runOnUiThread(new Runnable() {

                   @Override

                   public void run() {

                       adapter.updateData(phHueSDK.getAccessPointsFound());

                  }

              });

       }

   }

   @Override

   public void onCacheUpdated(List<Integer> arg0, PHBridge bridge) {

       Log.w(TAG, "On CacheUpdated");

   }

   @Override

   public void onBridgeConnected(PHBridge b, String username) {

       phHueSDK.setSelectedBridge(b);

       phHueSDK.enableHeartbeat(b, PHHueSDK.HB_INTERVAL);

       phHueSDK.getLastHeartbeat().put(b.getResourceCache().getBridgeConfiguration() .getIpAddress(), System.currentTimeMillis());   prefs.setLastConnectedIPAddress(b.getResourceCache().getBridgeConfiguration().getIpAddress());

       prefs.setUsername(username);

       PHWizardAlertDialog.getInstance().closeProgressDialog();

       startMainActivity(); }

   @Override

   public void onAuthenticationRequired(PHAccessPoint accessPoint) {

       Log.w(TAG, "Authentication Required.");

       phHueSDK.startPushlinkAuthentication(accessPoint);

       startActivity(new Intent(PHHomeActivity.this, PHPushlinkActivity.class));

}
   @Override

   public void onConnectionResumed(PHBridge bridge) {

       if (PHHomeActivity.this.isFinishing())

           Return

       Log.v(TAG, "onConnectionResumed" + bridge.getResourceCache().getBridgeConfiguration().getIpAddress());    phHueSDK.getLastHeartbeat().put(bridge.getResourceCache().getBridgeConfiguration().getIpAddress(),  System.currentTimeMillis());

       for (int i = 0; i < phHueSDK.getDisconnectedAccessPoint().size(); i++) {

           if (phHueSDK.getDisconnectedAccessPoint().get(i).getIpAddress().equals(bridge.getResourceCache().getBridgeConfiguration().getIpAddress())) {

               phHueSDK.getDisconnectedAccessPoint().remove(i);

           }

       }

   }

   @Override

   public void onConnectionLost(PHAccessPoint accessPoint) {

       Log.v(TAG, "onConnectionLost : " + accessPoint.getIpAddress());

       if (!phHueSDK.getDisconnectedAccessPoint().contains(accessPoint)) {

phHueSDK.getDisconnectedAccessPoint().add(accessPoint);

       }

   }

   @Override

public void onError(int code, final String message) {

       Log.e(TAG, "on Error Called : " + code + ":" + message)

           }

       }

   }

   @Override

   public void onParsingErrors(List<PHHueParsingError> parsingErrorsList) {

       for (PHHueParsingError parsingError: parsingErrorsList) {

           Log.e(TAG, "ParsingError : " + parsingError.getMessage());

       }     

   }

};

After getting the list of available bridges, select bridge and connect with a bridge using “PHBrideg”.

PHAccessPoint accessPoint = (PHAccessPoint) adapter.getItem(position);
PHBridge connectedBridge = phHueSDK.getSelectedBridge();
if (connectedBridge != null) {
String connectedIP = connectedBridge.getResourceCache().getBridgeConfiguration().getIpAddress();
if (connectedIP != null) { // We are already connected here:-
phHueSDK.disableHeartbeat(connectedBridge);
phHueSDK.disconnect(connectedBridge);
}
}
PHWizardAlertDialog.getInstance().showProgressDialog(R.string.connecting, PHHomeActivity.this);
phHueSDK.connect(accessPoint);

Now select the bulbs and perform operations on selected bulbs.

Here we can switch ON/OFF lights, set brightness, set color of lights using “PHLightState”

List<PHLight> allLights = bridge.getResourceCache().getAllLights();
PHLightState lightState = new PHLightState();
lightState.setOn(isOn);
lightstate.setBrightness(40)
bridge.updateLightState(allLights.get(position), lightState, listener);

Make sure brightness must be in between 0 – 254. 

Here we just learn basic functionalities of Philips hue. We can do so many things using Philips hue SDK.

The smart lights concept is becoming very popular nowadays; I enjoyed it a lot while learning about Philips hue development; I hope you guys will also enjoy it.

Hope it will be helpful to you!!!😊

Priyanka M

Senior Software Engineer

Priyanka is an android developer with around 3.5 years of experience developing Android applications with core functionality. She has in-depth knowledge of Java and React Native. Priyanka likes to learn new things and share.

Keep Reading

Keep Reading

  • Service
  • Career
  • Let's create something together!

  • We’re looking for the best. Are you in?