Roku ECP Programming with Swift 2
In the previous article, I went over a little bit about discovery of Roku devices on the network. Accomplished by sending out a UDP call on port 239.255.255.250 on port 1900. A response comes back with the a message with two important blocks of information, the unique device identifier and the IP address for the device on the local area network. Now, for a little bit of code to make the device do something.
Looking at the documentation, there are a number of endpoints we can hit on the device’s HTTP REST server. The initial one we will target is the keypress, since this is probably the most basic thing we want to do. We want to be able to navigate the screen. In the table where it talks about the endpoint, we can also see there is a link which takes up to how we can specify the key.
A little bit below that on the page, it shows some examples using curl. While I did this for some initial playing, the main point of interest is to be able to control the thing via code written in swift.
The code is simple. Just a quick script to run in Swift playgrounds. The request for a keypress must be sent as a POST request, so the method is set. Using string interpolation, we set the url. The full url looks like so
Using swift’s async/await, make the request and print out the response. The following is the response we get back
Not a whole lot of exciting data, the most important bit is the status code of 200 telling us everything went well.
The API provides a lot of different endpoints with my main focus being on keypress. It provides the usualy expected navigation such as left, right, up, and down. Along with an endpoint for home, back and several others.