These days, the term “smart home” is nothing new to most people. There are many accessories which support protocols for wireless home automation. You can find automatic lightbulbs, outlets, switches and much more.
Apple, of course, has its own protocol for home automation - HAP (HomeKit Accessory Protocol) - but it hasn’t been available to everyone so far. If you wanted to produce some accessories, you needed to apply for an MFI license which was only available for companies, not for single developers.
Now, after WWDC 2017, Apple has started to share its HAP specification with everyone, so you don’t need to apply for a license anymore if you won’t produce any accessories and so, now we can show you how it works without fear that we will be breaking the regulations!
This tutorial will show you how to simulate HomeKit Accessory on your own iOS device. We will, of course, describe only the most important things about HAP because the overall specification is too big to describe every detail (you can find the full specification here https://developer.apple.com/homekit/).
First of all - you need to implement Bonjour (https://developer.apple.com/bonjour/). It may sound complicated, but it is not as bad you think. Okay, so let’s do it!
Only three lines of code? Let’s run it and see our Home App!
Hmmm? Wait a moment… Home App still doesn’t see our server? Yep, that’s because we’re still missing one thing. If you want to be discovered, creating a service with “_hap._tcp” type is not enough. You will need to set metadata via TXT record to our service before publishing. Okay, so let’s do it!
This key won’t tell you anything but you can find its description in the HAP specification on page 56, or right below.
Okay, now we’re ready to try one more time!
Achievement completed! Our service is discovered by Home App! Let’s go to the next stage.
The Pair Setup process follows the SRP (Secure Remote Password) protocol and is described on page 5 in the specification, step by step. You can find SRP libraries written in Swift and go through this process by yourself, or use the HAP ready library. For this tutorial, we will be using HAP library written by Bouke Haarsma (https://github.com/Bouke/HAP) to quickly show you how to make a simulator on your iOS device without nesting in the authentication process, which is a bit complicated.
Let’s try to use it. First what you need to do is to create a new object of Device type which stands for prototype of HomeKit device. You can add services to it (e.g. Outlet, Switch, Fan …) and more if you want the device to be a bridge. Let’s say we want to have a Fan device.
Now you must create an object of Server type which will run like a service as we wrote earlier. This server will accept and parse requests to our device.
All you need to do now is to run the server by running server.start().
If you would like to produce your own device for HomeKit, you will definitely need to apply for an MFI license, but there’s no need for that if you only want to play with HomeKit. All available characteristics, services, and protocol descriptions can be found in the HAP specification at developer.apple.com. With an MFI license, you also have access to the last version of the developer specification where you can find beta tested characteristics which may not be in the current iOS version.
Also, check HomeBridge (https://github.com/nfarina/homebridge) if you would like to connect your smart home devices which don’t support HomeKit with iOS.
Posted by: Piotr Sękara, https://github.com/piotr-sekara