A while back I published a post on how to create a Xamarin.Forms app to control your Philips Hue lights. I figured I would see what it takes to do the same with .NET MAUI. Turns out the steps are pretty similar, but I wanted to provide a specific guide as a reference along with some sample code.
This guide will assume that you already have one or more Philips Hue lights installed in your home and that you have configured your Philips Hue bridge.
Configuring your Philips Hue Bridge
Before we get started, we need to find two things: the IP of your Philips Hue bridge and an app ID. The bridge IP can be retrieved using this link. Make sure that you are connected to the same network as your bridge when you access the link. The app key you’ll need to create yourself. Follow Step 3 from the Getting Started guide to get this (referred to as “username” in the guide).
File -> New project
As of writing, MAUI is in Release Candidate 2. So you can either use Visual Studio 2022 Preview or the dotnet CLI to create a new .NET MAUI project. Once that’s done, add the NuGet package Q42.HueApi to your project. Now open the
MainPage.xaml.cs and edit the existing button event handler there to look like this:
The variable bridgeIP should correspond to your bridge’s IP and the variable appKey should correspond to your app key / username generated in the previous step. Use these to initialize a new client to communicate with the bridge. You are now ready to light ’em up!
Let there be light!
Depending on what kind of light bulb you have, you can send commands to alter different settings for your light bulb. The most basic ones you can turn on or off, some of them you can dim or send alerts to (light bulb flashes) and the most advanced ones you can set the color for. For this tutorial we’ll send an alert command that flashes the lights.
We’ll add two new lines inside the event handler:
Now when you click the button, all your Philips Hue lights should blink once. Easy! You can also select which lights you want to control by passing in a list of IDs. Check the GitHub page for the NuGet package on how to do this.
I realize that a lot of these steps are very much the same for Xamarin.Forms and .NET MAUI, but I wanted to test it out just to make sure that you don’t need to do anything specific in MAUI to get this working. I’m also hoping this will serve as a reference when people Google if this is possible.
I’ve provided a simple sample on GitHub if you want to check that out.