Flutter智能家居控制插件miio_new的使用
Flutter智能家居控制插件miio_new的使用
MiIO
** 这是一个来自原始dartlang miio包的复制品,仅为了使其符合dart 3.x版本。**
Dart实现用于MiIO LAN协议。
该协议是一种基于UDP(端口54321)的加密二进制协议,用于配置和控制小米生态系统制造的智能家居设备。
CLI
该包包含一个基于miio构建的简单命令行程序。
安装
- 从Pub激活:
pub global activate miio
- 从GitHub Actions下载预构建的二进制文件
示例
# 发送到广播IP的发现数据包。
miio discover --ip 192.168.1.255
# 发送到设备的数据包。
miio send --ip 192.168.1.100 --token ffffffffffffffffffffffffffffffff --payload '{"id": 1, "method": "miIO.info", "params": []}'
# 或使用设备API。
# 传统方式:
miio device --ip 192.168.1.100 --token ffffffffffffffffffffffffffffffff props -p power
miio device --ip 192.168.1.100 --token ffffffffffffffffffffffffffffffff call -m set_power -p on
# MIoT规范:
miio device --ip 192.168.1.100 --token ffffffffffffffffffffffffffffffff property -s 2 -p 1
miio device --ip 192.168.1.100 --token ffffffffffffffffffffffffffffffff property -s 2 -p 1 -v true
协议
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Magic Number = 0x2131 | Packet Length (incl. header) |
|-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-|
| Unknown |
|-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-|
| Device ID ("did") |
|-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-|
| Stamp |
|-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-|
| MD5 Checksum |
| ... or Device Token in response to the "Hello" packet |
|-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-|
| Optional variable-sized payload (encrypted) |
|...............................................................|
Packet Length: 16 bits unsigned int
Length in bytes of the whole packet, including header(0x20 bytes).
Unknown: 32 bits
This value is always 0.
0xFFFFFFFF in "Hello" packet.
Device ID: 32 bits
Unique number. Possibly derived from the MAC address.
0xFFFFFFFF in "Hello" packet.
Stamp: 32 bit unsigned int
Continously increasing counter.
Number of seconds since device startup.
MD5 Checksum:
Calculated for the whole packet including the MD5 field itself,
which must be initialized with token.
In "Hello" packet,
this field contains the 128-bit 0xFF.
In the response to the first "Hello" packet,
this field contains the 128-bit device token.
Optional variable-sized payload:
Payload encrypted with AES-128-CBC (PKCS#7 padding).
Key = MD5(Token)
IV = MD5(Key + Token)
Flutter示例代码
以下是一个完整的Flutter应用示例,展示如何使用miio_new
插件来控制智能家居设备。
首先,确保在pubspec.yaml
文件中添加miio_new
依赖项:
dependencies:
flutter:
sdk: flutter
miio_new:
git:
url: https://github.com/your-repo/miio_new.git
接下来,在你的Flutter应用中导入并使用miio_new
插件:
import 'package:flutter/material.dart';
import 'package:miio_new/miio_new.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('MiIO控制示例'),
),
body: Center(
child: ElevatedButton(
onPressed: () async {
// 初始化MiIO客户端
final client = MiioClient(
ipAddress: '192.168.1.100',
token: 'ffffffffffffffffffffffffffffffff',
);
try {
// 获取设备信息
final info = await client.getInfo();
print('设备信息: $info');
// 控制设备开关
await client.setPower(true);
print('设备已开启');
} catch (e) {
print('发生错误: $e');
}
},
child: Text('控制设备'),
),
),
),
);
}
}
更多关于Flutter智能家居控制插件miio_new的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter智能家居控制插件miio_new的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
miio_new
是一个用于控制小米智能家居设备的 Flutter 插件。它基于 miio
协议,允许开发者通过 Flutter 应用程序与小米智能设备进行交互。以下是如何使用 miio_new
插件的基本指南。
1. 添加依赖项
首先,你需要在 pubspec.yaml
文件中添加 miio_new
插件的依赖项:
dependencies:
flutter:
sdk: flutter
miio_new: ^0.0.1 # 请确保使用最新的版本
然后运行 flutter pub get
来获取依赖项。
2. 导入包
在你的 Dart 文件中导入 miio_new
包:
import 'package:miio_new/miio_new.dart';
3. 发现设备
你可以使用 Miio.discover()
方法来发现同一网络中的小米智能设备。这个方法会返回一个 Stream
,你可以监听它来获取发现的设备。
void discoverDevices() async {
Miio.discover().listen((MiioDevice device) {
print('发现设备: ${device.name}, IP: ${device.ip}, Token: ${device.token}');
});
}
4. 连接到设备
要控制一个设备,首先需要连接到它。你需要设备的 IP 地址和 token。
void connectToDevice() async {
String ip = '192.168.1.100'; // 设备的IP地址
String token = 'your_device_token'; // 设备的token
MiioDevice device = MiioDevice(ip: ip, token: token);
await device.connect();
print('设备已连接');
}
5. 发送命令
一旦设备连接成功,你可以发送命令来控制设备。以下是一个简单的例子,控制小米智能灯泡的开关:
void toggleLight(MiioDevice device) async {
await device.sendCommand('set_power', ['on']); // 打开灯
// await device.sendCommand('set_power', ['off']); // 关闭灯
print('灯已打开');
}
6. 处理响应
你可以处理设备返回的响应。sendCommand
方法会返回一个 Future
,你可以通过它来获取设备的响应。
void getDeviceStatus(MiioDevice device) async {
var response = await device.sendCommand('get_prop', ['power']);
print('设备状态: $response');
}
7. 断开连接
完成操作后,记得断开与设备的连接。
void disconnectDevice(MiioDevice device) async {
await device.disconnect();
print('设备已断开连接');
}
8. 示例代码
以下是一个完整的示例代码,展示了如何使用 miio_new
插件发现、连接、控制小米智能灯泡:
import 'package:flutter/material.dart';
import 'package:miio_new/miio_new.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: MiioExample(),
);
}
}
class MiioExample extends StatefulWidget {
[@override](/user/override)
_MiioExampleState createState() => _MiioExampleState();
}
class _MiioExampleState extends State<MiioExample> {
MiioDevice? _device;
[@override](/user/override)
void initState() {
super.initState();
discoverDevices();
}
void discoverDevices() async {
Miio.discover().listen((MiioDevice device) {
print('发现设备: ${device.name}, IP: ${device.ip}, Token: ${device.token}');
if (device.name.contains('Yeelight')) {
connectToDevice(device);
}
});
}
void connectToDevice(MiioDevice device) async {
await device.connect();
setState(() {
_device = device;
});
print('设备已连接');
}
void toggleLight() async {
if (_device != null) {
await _device!.sendCommand('set_power', ['on']);
print('灯已打开');
}
}
void getDeviceStatus() async {
if (_device != null) {
var response = await _device!.sendCommand('get_prop', ['power']);
print('设备状态: $response');
}
}
void disconnectDevice() async {
if (_device != null) {
await _device!.disconnect();
setState(() {
_device = null;
});
print('设备已断开连接');
}
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Miio New Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: toggleLight,
child: Text('打开灯'),
),
ElevatedButton(
onPressed: getDeviceStatus,
child: Text('获取设备状态'),
),
ElevatedButton(
onPressed: disconnectDevice,
child: Text('断开连接'),
),
],
),
),
);
}
}