You can get the IotDevice from the IotDeviceController.getIotDevices()
.
Since SDK 1.3.1 all functions in the IotDeviceController returning LiveData
are deprecated . Use either the suspending functions with Coroutines or one of the *WithCallback()
alternative functions.
The IotDevice
offers you a function newRequest()
. It gives you a StateChangeRequest.Builder.
val stateChangeRequest: StateChangeRequest = iotDevice.newRequest().apply {
addState(property, value)
}
The StateChangeRequest can then be used as a parameter given to the execute()
function of the IotDeviceController interface: iotDeviceController.execute(stateChangeRequest)
val cancelable: Cancelable = iotDeviceController.executeWithCallback(stateChangeRequest) { wasSuccessful: Boolean ->
// check if call was successful
}
// you must call cancel when you leave your ViewModel/Fragment/Activity to not leak memory!
cancelable.cancel();
coroutineScope.launch {
val wasSuccessful: Boolean = iotDeviceController.execute(stateChangeRequest)
// check if call was successful
}
StateChangeRequest.Builder builder = iotDevice.newRequest();
// builder.addState() // set new numeric or binary state
StateChangeRequest stateChangeRequest = builder.build();
Cancelable cancelable = iotDeviceController.executeWithCallback(stateChangeRequest, new Function1<Boolean, Unit>() {
@Override
public Unit invoke(Boolean isSuccessful) {
// check if request was successful
return null;
}
});
// you must call cancel when you leave your ViewModel/Fragment/Activity to not leak memory!
cancelable.cancel();
Do not forget calling cancel()
on the CoroutineScope or the Cancelable or you will leak memory!