androidphilips-hue

Using Philips Hue with Port Forwarding


I am working on Android project that utilises that Philips Hue SDK.

I've implemented some of the functionality so that I can successfully control the lights but I am also trying to make the app control the lights when the user is away from home.

I have set up port forwarding so that I can connect to my public IP address through port 80 and it forwards it to the bridge.

The idea is that the user connects to the bridge and authenticates with the bridge on its local ip addresss. The user is then able to enter what the external address is.

I have a function that when connecting to the bridge, I check to see if the user connected Wifi and are they on their own home wifi I then set the IP address of the PHAccessPoint of the users local bridge bridge address.

If the user is not on wifi, or they are but on the home wifi, then the PHAccessPoint IP address is updated to what the user entered as they're external address.

When I do this, I keep getting either Bridge not responding, not connected or bridge already connected and this keeps happening rapidly. Even though in the error handler, if I get bridge not responding or not connected, I ensure I disconnect the PHAccessPoint and attempt to re-connect again.

I've tried disabling the Heartbeat Interval or increasing it and it makes no difference.

If however I am on the wifi but still connect via my public IP address it works, I only get this problem if going through mobile data.

Are there any tricks to make this work I've seen other apps do something similar but its not open source so can't see how they've implemented it.

I have signed up to Philips Hue Remote API, but I've had no response from them.


Solution

  • After lots of trial and error and Android Studio being able to decompile the SDK so I can see some of the code I think its a limitation of how the official SDK is implemented.

    Basically I think the problem is that you create a PHAccessPoint object and set your external ip e.g. 86.85.84.83, when you connect to the bridge, you successfully connect and get the bridge details, and it stores the bridge configuration as a PHBridge object. This object contains the bridge's local network IP e.g. 192.168.x.x. When you then come to do anything else the connected bridge object and the PHAccessPoint object don't match and therefore a no connection error is returned, upon trying to handle that and re-connect, because the SDK has a bridge set up as connected the SDK returns that you are already connected and you end up getting stuck in a loop.

    I think the only way around this would be to either have a server that is exposed via port forwarding and the messages are passed on from the server to the local bridge and then back out though the server (so the actual bridge stays local) or to roll your own implementation of the SDK using the RESTful API