Appearance
智能家居生态
1. 鸿蒙智能家居生态概述
1.1 鸿蒙 IoT 架构
鸿蒙 IoT 生态架构:
┌───────────────────────────────────────────┐
│ 智能家居生态层 │
│ ├── 智能家居设备(灯/空调/窗帘/锁/音箱等) │
│ ├── 智能音箱(小艺音箱) │
│ ├── 智慧屏(电视/显示器) │
│ └── 全屋智能解决方案 │
├───────────────────────────────────────────┤
│ 鸿蒙分布式能力层 │
│ ├── 分布式软总线(设备发现/连接/通信) │
│ ├── 分布式数据(跨设备数据同步) │
│ ├── 分布式 UI(跨设备 UI 迁移) │
│ └── 分布式任务调度(跨设备任务执行) │
├───────────────────────────────────────────┤
│ 鸿蒙系统层 │
│ ├── HongMeng Kernel(轻量内核) │
│ ├── ArkTS/ArkUI(跨设备统一开发) │
│ └── HarmonyOS Connect(设备接入标准) │
└───────────────────────────────────────────┘1.2 鸿蒙 IoT 设备分类
| 类别 | 设备类型 | 示例 |
|---|---|---|
| 核心设备 | 手机/平板/智慧屏/音箱 | Mate 60 / MatePad / Vision |
| 环境设备 | 灯/空调/窗帘/净化器 | 智能灯/智能空调/智能窗帘 |
| 安防设备 | 摄像头/门锁/传感器 | 智能摄像头/智能门锁/门窗传感器 |
| 厨电设备 | 冰箱/烤箱/洗碗机 | 智能冰箱/智能烤箱 |
| 个人设备 | 手表/手环/耳机 | Watch GT / FreeBuds |
2. 分布式协同
2.1 分布式软总线
分布式软总线核心能力:
┌─────────────────────────────────────────────────────┐
│ 设备发现 │
│ ├── mDNS 广播发现 │
│ ├── 蓝牙 LE 辅助发现 │
│ └── 主动扫描发现 │
├─────────────────────────────────────────────────────┤
│ 设备连接 │
│ ├── P2P 直连(低延迟) │
│ ├── WiFi Direct 连接 │
│ └── 蓝牙连接(低功耗) │
├─────────────────────────────────────────────────────┤
│ 统一通信管道 │
│ ├── 消息路由(设备间自动路由) │
│ ├── 传输优化(自适应带宽/延迟) │
│ └── 安全加密(端到端加密) │
└─────────────────────────────────────────────────────┘2.2 分布式 UI 迁移
场景:手机上看视频 → 智慧屏播放
┌──────────────────────────────────────────────┐
│ 1. 用户点击"投屏"按钮 │
│ → 检测到附近的智慧屏 │
│ │
│ 2. 分布式 UI 迁移 │
│ → 应用窗口从手机迁移到智慧屏 │
│ → 视频播放状态保持(进度/音量) │
│ → 音频自动切换到智慧屏扬声器 │
│ │
│ 3. 跨设备控制 │
│ → 手机变为遥控器 │
│ → 视频继续在智慧屏播放 │
└─────────────────────────────────────────────────┘arkts
// 分布式 UI 迁移示例
import { distributedAbility } from '@kit.DistributedKit';
async function castToScreen() {
// 1. 发现智慧屏
const screens = await distributedAbility.getDevices({
capability: 'display',
filter: { brand: 'huawei' }
});
if (screens.length === 0) return;
// 2. 迁移应用窗口
const migration = await distributedAbility.castAbility({
fromApp: 'com.example.myapp',
toDevice: screens[0].deviceId,
type: distributedAbility.CastType.VIDEO,
state: {
playPosition: this.videoPosition,
volume: this.videoVolume
}
});
if (migration.success) {
console.log('投屏成功');
}
}2.3 分布式数据同步
分布式数据同步场景:
┌─────────────────────────────────────────────────────┐
│ 跨设备数据同步 │
│ ├── 手机拍照 → 平板查看 │
│ ├── 手机编辑文档 → 电脑继续 │
│ ├── 手表心率 → 手机健康面板 │
│ ├── 门锁状态 → 手机通知 │
│ └── 空调设置 → 手机同步 │
└─────────────────────────────────────────────────────┘arkts
// 分布式 KV-Store 数据同步
import { distributedData } from '@kit.DistributedKit';
// 1. 创建分布式 KV-Store
const store = await distributedData.createKVStore({
appId: 'com.example.smartHome',
config: {
syncPolicy: distributedData.SyncPolicy.SYNC_POLICY_ALL,
autoSync: true
}
});
// 2. 写入数据(自动同步到所有设备)
await store.put('livingRoom.light', 'on');
await store.put('livingRoom.brightness', '80');
await store.put('livingRoom.temperature', '24');
// 3. 读取数据(本地优先,自动同步)
const lightState = await store.get('livingRoom.light');
if (lightState?.value === 'on') {
this.turnOnLight();
}3. 智能家居设备接入
3.1 HarmonyOS Connect
HarmonyOS Connect 是鸿蒙的设备接入标准:
┌─────────────────────────────────────────────────────┐
│ 设备接入流程 │
│ ├── 1. 设备认证(设备证书 + 产品 ID) │
│ ├── 2. 设备配网(配网模式 + 手机引导) │
│ ├── 3. 设备发现(mDNS + 蓝牙辅助) │
│ ├── 4. 服务注册(设备能力描述注册) │
│ └── 5. 设备联动(场景编排 + 自动化规则) │
└─────────────────────────────────────────────────────┘3.2 设备能力描述
json5
// device_capability.json
{
"device": {
"product_id": "smart_light_001",
"product_name": "智能灯",
"capabilities": [
{
"id": "light",
"type": "switch",
"name": "开关",
"access": "read_write"
},
{
"id": "brightness",
"type": "number",
"name": "亮度",
"range": { "min": 1, "max": 100, "step": 1 }
},
{
"id": "color_temp",
"type": "number",
"name": "色温",
"range": { "min": 2700, "max": 6500, "step": 100 }
},
{
"id": "scene",
"type": "enum",
"name": "场景模式",
"values": ["reading", "movie", "sleep", "party"]
}
]
}
}3.3 设备控制示例
arkts
// 控制智能家居设备
import { deviceManager } from '@kit.DistributedKit';
class SmartHomeController {
// 1. 搜索设备
async searchDevices(): Promise<deviceManager.DeviceInfo[]> {
const devices = await deviceManager.scanDevices({
serviceType: 'smart_light'
});
return devices;
}
// 2. 控制设备
async setLight(deviceId: string, brightness: number) {
await deviceManager.invokeService(deviceId, 'setBrightness', {
brightness: brightness
});
}
// 3. 场景联动
async setScene(deviceIds: string[], scene: string) {
for (const deviceId of deviceIds) {
await deviceManager.invokeService(deviceId, 'setScene', {
scene: scene
});
}
}
// 4. 自动化规则
async createAutomation(condition: string, action: string) {
await deviceManager.createAutomationRule({
condition: condition, // "when light < 30"
action: action // "then turn on lamp"
});
}
}4. 全屋智能方案
4.1 全屋智能架构
全屋智能方案:
┌─────────────────────────────────────────────────────┐
│ 控制中心(手机/音箱/智慧屏) │
│ ├── 场景编排(回家/离家/睡眠/观影) │
│ ├── 设备管理(添加/删除/分组) │
│ └── 数据统计(能耗/使用习惯) │
├─────────────────────────────────────────────────────┤
│ 中枢网关(家庭中心) │
│ ├── 设备接入(Zigbee/BLE/Wi-Fi/星闪) │
│ ├── 场景执行(自动化规则) │
│ ├── 数据同步(跨设备数据同步) │
│ └── 语音交互(小艺) │
├─────────────────────────────────────────────────────┤
│ 设备层 │
│ ├── 照明(智能灯/灯带/吸顶灯) │
│ ├── 安防(摄像头/门锁/传感器) │
│ ├── 环境(空调/新风/净化器) │
│ ├── 窗帘(电动窗帘/遮阳帘) │
│ └── 娱乐(音箱/电视/投影) │
└─────────────────────────────────────────────────────┘4.2 场景编排
arkts
// 场景编排
import { sceneManager } from '@kit.DistributedKit';
// 1. 回家场景
async function homeScene() {
await sceneManager.executeScene('home', {
livingRoom: { light: 'on', brightness: 80 },
bedroom: { light: 'on', brightness: 60 },
airConditioner: { temp: 24, mode: 'cool' },
curtain: { open: true }
});
}
// 2. 离家场景
async function leaveScene() {
await sceneManager.executeScene('leave', {
lights: 'off',
airConditioner: 'off',
curtain: 'close',
camera: 'start',
alarm: 'enable'
});
}
// 3. 睡眠场景
async function sleepScene() {
await sceneManager.executeScene('sleep', {
livingRoom: { light: 'off' },
bedroom: { light: 'on', brightness: 20, color: 'warm' },
airConditioner: { temp: 26, mode: 'sleep' },
curtain: 'close',
nightLight: 'on'
});
}
// 4. 自定义场景
async function createCustomScene() {
const scene = await sceneManager.createScene({
name: 'Movie Night',
trigger: 'voice',
actions: [
{ device: 'livingRoom.light', action: 'setBrightness', value: 10 },
{ device: 'livingRoom.light', action: 'setColor', value: 'blue' },
{ device: 'television', action: 'turnOn' },
{ device: 'curtain', action: 'close' },
{ device: 'airConditioner', action: 'setTemp', value: 25 }
]
});
}5. 智能家居开发
5.1 智能家居应用架构
智能家居应用架构:
┌─────────────────────────────────────────────────────┐
│ 应用层 │
│ ├── 设备控制面板 │
│ ├── 场景编排界面 │
│ ├── 数据统计面板 │
│ └── 语音控制界面 │
├─────────────────────────────────────────────────────┤
│ 服务层 │
│ ├── 设备管理服务 │
│ ├── 场景管理服务 │
│ ├── 自动化规则管理 │
│ └── 数据统计服务 │
├─────────────────────────────────────────────────────┤
│ 分布式层 │
│ ├── 设备发现/连接 │
│ ├── 数据同步 │
│ └── 远程指令下发 │
└─────────────────────────────────────────────────────┘6. 🎯 面试高频考点
Q1: 鸿蒙分布式软总线的工作原理?
答要点:
- 设备发现:mDNS 广播 + 蓝牙 LE 辅助
- 设备连接:P2P 直连 + WiFi Direct
- 统一通信管道:自动路由 + 传输优化
- 安全加密:端到端加密
- 跨设备通信延迟 < 20ms
- 支持百设备并发
Q2: 分布式 UI 迁移的原理?
答要点:
- 设备发现后建立连接
- 应用窗口状态序列化
- 跨设备迁移窗口状态
- 音频/视频/输入自动切换
- 操作连续性保持
- 手机变为遥控器
Q3: 全屋智能的核心技术?
答要点:
- 设备接入:HarmonyOS Connect 标准
- 场景编排:可视化场景模板 + 自定义
- 自动化规则:条件触发 + 动作执行
- 语音控制:小艺语音识别 + NLU
- 数据同步:分布式 KV-Store 跨设备同步
- 多屏协同:手机/智慧屏/音箱联动
💡 面试提示:智能家居是鸿蒙 IoT 生态的核心。重点掌握 分布式软总线、分布式 UI 迁移、HarmonyOS Connect 设备接入、场景编排。强调鸿蒙在 IoT 领域的统一架构优势。