A B4J Version is available here.
HUE
Author:
Version: 0.41
Requirements:
1- You need to follow the instructions here to get your "Username" which you them can use to Authenticate your app against the SDK.
Once you have the username (kind of a Unique ID) you can use this username and adapt the Examplecode which actually uses my Username. You need to change it to yours for sure.
- You Device must be connected to the same Network as your HueBridge(s)
You can change the Lightstate as this
please note how the Value is set in the Code above. The point is that we need to have both Objects. The Light object and it´s LightState object which we get from the SDK.
NOTES:
- If you do not have a username then you can use the SDK to search for AccessPoints which then will raise an event for the Bridge found. If you want to use this bridge then you probably need to authenticate (calling the method of the sdk)...
Once you are authenticated you can use the bridge.
Until you you quit your app. Once closed you need to authenticate again after a new search.
The best way is to register a username an connect to your Bridge using username and known IP. Using a valid username give your direct access to the bridge.
HAVE FUN
Some Notes about user findings (Thank you @johndb!)
To change the state of a Light/Group/Screne:
Infos about Cached Updates:
HUE
Author:
Version: 0.41
Requirements:
1- You need to follow the instructions here to get your "Username" which you them can use to Authenticate your app against the SDK.
Once you have the username (kind of a Unique ID) you can use this username and adapt the Examplecode which actually uses my Username. You need to change it to yours for sure.
- You Device must be connected to the same Network as your HueBridge(s)
B4X:
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim sdk As PHHueSDK
Dim APselected As PHAccessPoint
Dim BridgeSelected As PHBridge
' Define here the username you got using the registration with the Bridge debug tool
' See https://www.developers.meethue.com/documentation/getting-started
' to learn how to create the "Username" for you to Access the SDK
Private HUEusername = "ofmxXcuFVe0okMRIxcyaSQRMtqTFBS9ANBjfCboI"
Dim hueoverview As Map
' Only used when using the httpapi. Not needed here.
'Private bridgeURL As String = "http://192.168.192.109/api/"&HUEusername&"/"
Private lvhue As ListView
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("Layout1")
hueoverview.Initialize
' Initialize the HUESDK and then the Manager inside the SDK
sdk.Initialize("HUE","QuickStartApp")
sdk.InitManager
' Setup the AccessPoint manually here.
Dim ap As PHAccessPoint
ap.Initialize("")
ap.IpAddress = "192.168.192.109" ' IP of AccessPoint
ap.MacAddress = "00:17:88:12:CB:6E" ' Mac-Address
ap.BridgeId = "001788FFFE12CB6E" ' ID (usually the Mac without :)
ap.Username = HUEusername ' Define the Username here to gain Access to this AccessPoint
' Connect the SDK with the AccessPoint
sdk.connect(ap)
wait for HUE_onBridgeConnected(bridge As PHBridge, info As String)
Log($"Activity_Create.HUE_onBridgeConnected(${bridge},${info})"$)
BridgeSelected = bridge
'sdk.addBridge(bridge)
Dim lights As List
lights = bridge.AllLights
Dim lightnames As List = sdk.getLightNames(lights)
If lights.IsInitialized Then
If lights.Size > 0 Then
For i = 0 To lights.Size-1
Dim light As PHLight = lights.Get(i)
Dim state As PHLightState = light.LastKnownLightState
Dim name As String = lightnames.Get(i)
hueoverview.Put("LightName"&light.Identifier,name)
Log($"Light = ${name} = ${light.Identifier} / Manufacturer = ${light.ManufacturerName} Model ${light.ModelNumber} LightType = ${light.LightType}"$)
hueoverview.Put("Light"&light.Identifier,light)
hueoverview.Put("LightState"&light.Identifier,state)
Next
End If
End If
Dim groups As List
groups = bridge.AllGroups
If groups.IsInitialized Then
If groups.Size > 0 Then
For i = 0 To groups.Size-1
Dim group As PHGroup = groups.Get(i)
Log($"Group = ${group.Name} / Type = ${group.GroupType} UniqueID = ${group.UniqueId}"$)
lvhue.AddSingleLine2(group.Name,group)
Dim grlights As List = group.LightIdentifiers
If grlights.IsInitialized And grlights.Size > 0 Then
For o = 0 To grlights.Size -1
Dim light As PHLight = hueoverview.Get("Light"&grlights.Get(o))
Dim state As PHLightState = hueoverview.Get("LightState"&grlights.Get(o))
Log($"${light.Identifier}: ${hueoverview.Get("LightName"&grlights.Get(o))} ${light.ModelNumber} - ${light.LightType}"$)
lvhue.AddTwoLines2(light.Identifier&": "&hueoverview.Get("LightName"&grlights.Get(o)),$"${light.ModelNumber} - ${light.LightType}"$,Array(light,state))
Next
End If
'Log(group.LightIdentifiers)
Next
End If
End If
Dim sensors As List
sensors = bridge.AllSensors
If sensors.IsInitialized Then
If sensors.Size > 0 Then
For i = 0 To sensors.Size-1
Dim sensor As PHSensor = sensors.Get(i)
Log($"Sensor = ${sensor.Name} / Type = ${sensor.ModelId} - ${sensor.ManufacturerName} - UniqueID = ${sensor.UniqueId}"$)
Next
End If
End If
Log("Rules: "&bridge.AllRules)
Log("Scenes: "&bridge.AllScenes)
'Log("Sensors: "&bridge.AllSensors)
Log("AllShedules: "&bridge.getAllSchedules(True))
Log("AllTimers: "&bridge.getAllTimers(True))
End Sub
You can change the Lightstate as this
please note how the Value is set in the Code above. The point is that we need to have both Objects. The Light object and it´s LightState object which we get from the SDK.
B4X:
Sub lvhue_ItemClick (Position As Int, Value As Object)
Log($"Itemclick(${Position}, ${Value})"$)
Dim obj() As Object = Value
Dim light As PHLight = obj(0)
Dim state As PHLightState = obj(1)
Log("Lightstate is "&state.ON)
If state.ON Then
state.ON = False
BridgeSelected.updateLightState(light,state)
Else
state.ON = True
BridgeSelected.updateLightState(light,state)
End If
End Sub
NOTES:
- If you do not have a username then you can use the SDK to search for AccessPoints which then will raise an event for the Bridge found. If you want to use this bridge then you probably need to authenticate (calling the method of the sdk)...
Once you are authenticated you can use the bridge.
Until you you quit your app. Once closed you need to authenticate again after a new search.
The best way is to register a username an connect to your Bridge using username and known IP. Using a valid username give your direct access to the bridge.
HAVE FUN
Some Notes about user findings (Thank you @johndb!)
To change the state of a Light/Group/Screne:
It appears that you must create a new PHLightState object, set the parameters that you would like to adjust, and pass the new object to the updateLightState function. This eliminates the blinking and provides a smooth transition.
B4X:
Sub lvhue_ItemClick (Position As Int, Value As Object)
Log($"Itemclick(${Position}, ${Value})"$)
Dim obj() As Object = Value
Dim light As PHLight = obj(0)
Dim state As PHLightState = obj(1)
Dim NewLightState As PHLightState
NewLightState.Initialize("")
NewLightState.Brightness = 254 ' 125
NewLightState.X = 0.4574
NewLightState.Y = 0.41
BridgeSelected.updateLightState(light,NewLightState)
End Sub
Infos about Cached Updates:
This problem above has been solved. Here are my lessons learned to date:
I'll post more when I discover other operational anomalies as I encounter them. I hope this helps other users who would like to explore this library.
- You must create a new PHLightState object, set the parameters that you would like to adjust, and pass the new object to the updateLightState function. This eliminates the blinking and provides a smooth transition.
- You cannot use "wait for <object>_onCacheUpdated(cacheNotifications As List, bridge As PHBridge)" after your "updateLightState" function call. It may not be called. The "_onCacheUpdated" event will only be called if a particular state is different than the light's current state in one of the LightState parameters. All of the "updateLightState" calls are queued for execution in the library. The "_onCacheUpdated(cacheNotifications" event will only we called if there is a change in one of the LightState parameters.
- You can use "wait for <object>_onAccessPointsFound(.." directly after the function call "sdk.search(True,True)".
- You can use "wait for <object>_onBridgeConnected (..." directly after the function call "sdk.connect(<access point>)".
Attachments
Last edited: