Reputation: 75
I'm trying philips hue with Swift. I want to turn on the light using HTTP request. I'm using Net for HTTPRequest https://github.com/nghialv/Net
let net = Net()
let params = ["on":true]
let puturl = "http://[IP Address]/api/[username]/lights/1/state"
net.PUT(absoluteUrl: puturl, params: params, successHandler: { (responseData) -> () in
println("success")
}) { (err) -> () in
println("err")
}
Xcode print "success" but the light can't turn on. Please let me know why the reason.
Upvotes: 1
Views: 2345
Reputation: 3071
You need to authenticate ("push link") with the bridge first. You probably did not do that, which is why the lights won't respond.
Also I suggest that you first start with the official Philips Hue SDK. It clearly has its issues, but it fundamentally works and is a good start before you re-implement all the requests on your own (which is quite a lot if you want to use all the Hue features).
Since Swift and Objective-C are bridge, the fact that the Hue SDK is written in Objective-C is not an issue. You only need to create a bridging header (AppName-Bridging-Header.h) with the line
#import <HueSDK_iOS/HueSDK.h>
and then you can use the Hue SDK from your Swift files.
Apple has a great book on how to use Objective-C code from Swift files.
Upvotes: 1
Reputation: 53572
With the Hue SDK comes a demo project. Have you tried that? It's not written in Swift (but Obj-C), however that shouldn't make a big difference. It works fine for me.
Also, did you authenticate your app with the bridge?
Upvotes: 0