简体   繁体   English

我正在尝试制作一个 flutter 应用程序来控制我的飞利浦 Hue 灯,但我遇到了 hue_dart 问题

[英]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.我有飞利浦 Hue 灯,我希望能够使用 Dart 和 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.我曾尝试使用名为hue_dart的库,但是当我将其作为 Dart 文件运行时,它只能控制一盏灯,我每次都必须单击 Bridge 上的按钮。

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.每次创建需要物理授权的用户时,您都必须单击 Bridge 按钮。 Print the created username to display the randomly generated username that the Bridge has created.打印创建的用户名以显示 Bridge 创建的随机生成的用户名。

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.然后您可以摆脱 createUser 步骤,并使用该随机字符串作为用户名,因为它现在是 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 .至于控制一个灯光方面,您只需要更改从 GET 返回的第一个灯光的 state 和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)
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter - 我正在尝试循环滚动有限列表,但遇到性能问题 - Flutter - I'm trying to scroll a finite list in a loop but am having performance problems 我正在尝试制作一个测验应用程序以在 Flutter 和 Dart 中进行培训,并希望能帮助我清除以下错误 - I am trying to Make a Quiz app for training in Flutter and Dart, and would appreciate assistance in getting the following error cleared 如何让我的音量控制应用程序在颤动的系统音量变化中立即响应(飞镖) - How can I make my volume control app response instantly in system volume change in flutter(dart) 请我尝试在我的 flutter 应用程序中更新列表中的项目,但没有具有更新值的相同项目 - Please I'm trying to update an item on a list in my flutter app without having same item with updated value 尝试在 flutter 上发出获取请求时出错 - I am having an error while trying to make a get request on flutter 尽管 Flutter 中有 SingleChildScrollView,但我无法让我的应用滚动 - I am not able to make my app scrollble, despite of having SingleChildScrollView in Flutter 如何在 Flutter 中更改颜色的色调、饱和度或值? - How do I change color's hue, saturation, or value in Flutter? 我正在尝试将我的 flutter 应用程序与 firebase 连接,但它不会生成名为“Generated_plugins_registrant.dart”的文件 - I am trying to connect my flutter app with firebase but it doesn't generate the file named "Generated_plugins_registrant.dart" 我的 flutter 应用程序中有太多黄线 - I am having too many yellow lines in my flutter app 我正在尝试通过 VS 代码在模拟器上运行我的 flutter 应用程序,但收到错误消息,说它找不到我的 main.dart 文件的路径 - I'm trying to run my flutter app on an emulator from VS code and I get an error saying it cannot find the path to my main.dart file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM