简体   繁体   中英

I am trying to make a flutter app to control my Philips Hue lights and I'm having problems with hue_dart

I have Philips Hue lights and I want to be able to change the colour using Dart and Flutter. I have tried using a library called hue_dart but when I run it as a Dart file, it can only control one light and I have to click the button on the Bridge every time.

Here is my code:

import 'package:http/http.dart';
import 'package:hue_dart/hue_dart.dart';

main(List<String> arguments) async {
  final client = Client();

  final discovery = BridgeDiscovery(client);

  List<DiscoveryResult> discoverResults = await discovery.automatic();
  final discoveryResult = discoverResults.first;

  var bridge = Bridge(client, discoveryResult.ipAddress);

  final whiteListItem = await bridge.createUser('dart_hue#example');

  bridge.username = whiteListItem.username;

  List<Light> lights = await bridge.lights();

  final light = lights.first.changeColor(red: 1.0, green: 0, blue: 1.0);
  LightState state = lightStateForColorOnly(light);
  state = state.rebuild(
    (s) => s
      ..on = true
      ..brightness = 10,
  );
  await bridge.updateLightState(light.rebuild(
    (l) => l..state = state.toBuilder(),
  ));
}

How would I make a user that persists? And one that can control an array of lights, not just the first one it returns?

You have to click the Bridge button each time as you are creating a user that needs physical authorisation each time. Print the created username to display the randomly generated username that the Bridge has created.

final whiteListItem = await bridge.createUser('dart_hue#example');
print(whiteListItem);
bridge.username = whiteListItem.username;

Then you can get rid of the createUser step, and use that random string as the username as that is now an authorised user on the Bridge.

//final whiteListItem = await bridge.createUser('dart_hue#example');
bridge.username = 'the printed value'

As for the controlling one light aspect, you are only ever changing the state of the first light that is returned from the GET with lights.first . I haven't tried it but iterating through would be a good next step:

Set<Light> set = Set.from(lights);
set.forEach((element) => 
  changeState(element)
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM