【HarmonyOS 5】鸿蒙分布式协同应用开发详解

GeorgeGcs
• 阅读 3

##鸿蒙开发能力 ##HarmonyOS SDK应用服务##鸿蒙金融类应用 (金融理财# 一、前言 为什么需要分布式协同应用? 首先是因为当今社会,围绕电子产品生态,人们迫切希望,周边的电子设备可以协同操作。例如手机,手表,电视机,汽车,甚至是各种家电产品。 从2015年到如今,手机和pc等老牌电子产品的设备数趋于稳定,其他IoT设备稳步增长。可见人均所拥有的的电子产品的个数,在迅速增加。 IoT 设备(Internet of Things Device) 是指具备联网能力、可与其他设备或系统进行数据交互的物理设备,是构成物联网(IoT)的核心单元。这类设备通过传感器、通信模块等组件,实现对物理世界的感知、数据传输及智能响应。 设备连接步骤繁琐,设备之间能力无法聚合,设备之间的数据无法连通,协同能力低效。 因为以上业务场景的需要,应用开发的需求,也从单一的设备应用开发思路。转变为了多设备协同应用开发。华为提出“1+8+N”以手机为主,围绕建立超级虚拟终端。 二、如何建立分布式操作系统 市面操作系统调研 1、市面上的操作系统,目前只有华为提出了分布式操作系统。 2、像苹果的操作系统,手机平板和电脑也是两套。 3、安卓只有移动端操作系统。没有涉及电脑。 4、微软的电脑操作系统,多年来才集成了安卓虚拟机。 分布式操作系统的特点 1、设备间的操作系统一致 2、拥有统一的交互语言 3、完整的生态 HarmonyOS超级终端能力基座: 1、分布式任务调度 2、分布式数据管理 3、分布式软总线 4、分布式设备虚拟化 HarmonyOS系统架构 HarmonyOS是在OpenHarmony的基础上演进的操作系统。系 统架构是一致,从下面OH的操作系统架构图。我们就可以发现鸿蒙操作分布式是如何实现:

HarmonyOS ArkUI框架 通过统一的ArkUI框架,实现在多种设备上,实现相同的UI实现。降低开发者的成本。实现一套代码多端实现。

三、分布式协同应用开发步骤拆解:

  1. 权限配置与申请 module.json5配置: { "module": { "requestPermissions": [ {
     "name": "ohos.permission.DISTRIBUTED_DATASYNC",
     "reason": "$string:permission_rationale",
     "usedScene": {
       "abilities": ["MainAbility"],
       "when": "inuse"
     }
    } ] } } 动态权限申请代码: import promptAction from '@ohos.promptAction'; import { abilityAccessCtrl, common } from '@kit.AbilityKit';

@Entry @Component struct DistributedDemo {

aboutToAppear() { this.requestPermission(); }

// 用户申请权限 private async reqPermissionsFromUser(): Promise<number[]> { let context = getContext() as common.UIAbilityContext; let atManager = abilityAccessCtrl.createAtManager(); let grantStatus = await atManager.requestPermissionsFromUser(context, ["ohos.permission.DISTRIBUTED_DATASYNC"]); return grantStatus.authResults; }

private async requestPermission() { let grantStatus = await this.reqPermissionsFromUser(); for (let i = 0; i < grantStatus.length; i++) { if (grantStatus[i] === 0) { // 用户授权,可以继续访问目标操作 this.initDeviceManager(); }else{ promptAction.showToast({ message: '请授予权限以使用分布式功能', duration: 2000 }); } } } } 2. 设备发现与组网 设备管理核心类: import { distributedDeviceManager, BusinessError } from '@ohos.distributedDeviceManager'; import promptAction from '@ohos.promptAction';

class DeviceManager { private dmInstance: distributedDeviceManager.DeviceManager | null = null; @State discoveredDevices: Array<distributedDeviceManager.DeviceBasicInfo> = [];

// 创建设备管理实例并注册回调 initDeviceManager() { try { this.dmInstance = distributedDeviceManager.createDeviceManager('com.example.distributedDemo');

  // 注册设备发现回调
  this.dmInstance.on('discoverSuccess', (data) => {
    console.info(`发现设备: ${JSON.stringify(data)}`);
    this.discoveredDevices.push(data);
    // 更新UI显示设备列表
    this.updateDeviceList();
  });

  this.dmInstance.on('discoverFailure', (error) => {
    console.error(`设备发现失败: ${error.code}, ${error.message}`);
    promptAction.showToast({
      message: '设备发现失败,请检查网络或蓝牙',
      duration: 2000
    });
  });

  // 注册设备状态变化回调(上下线监听)
  this.dmInstance.on('deviceStateChange', (data) => {
    const action = data.action === 1 ? '上线' : '下线';
    console.info(`设备 ${data.device.deviceName} ${action}`);
    promptAction.showToast({
      message: `${data.device.deviceName} 已${action}`,
      duration: 1500
    });
  });
} catch (error) {
  console.error(`创建设备管理器失败: ${(error as BusinessError).code}, ${(error as BusinessError).message}`);
}

}

// 开始发现周边设备 startDiscovering() { if (!this.dmInstance) return;

const discoverParam: Record<string, number> = {
  'discoverTargetType': 1  // 发现所有类型设备
};

const filterOptions: Record<string, number> = {
  'availableStatus': 0,     // 发现可用设备
  'discoverDistance': 0,    // 不限制距离
  'authenticationStatus': 0, // 发现未认证设备
  'authorizationType': 0    // 不限制授权类型
};

try {
  this.dmInstance.startDiscovering(discoverParam, filterOptions);
  console.info('开始发现设备...');
  promptAction.showToast({
    message: '正在搜索周边设备',
    duration: 2000
  });
} catch (error) {
  console.error(`设备发现接口调用失败: ${(error as BusinessError).code}, ${(error as BusinessError).message}`);
}

} } 3. 设备绑定与连接 设备绑定逻辑: // 在DeviceManager类中添加绑定方法 bindDevice(deviceId: string) { if (!this.dmInstance) return;

const bindParam: Record<string, string | number> = { 'bindType': 1, // 绑定类型(1: PIN码认证) 'targetPkgName': 'com.example.targetDevice', // 目标设备包名 'appName': '智能家居设备', 'appOperation': '绑定操作', 'customDescription': '分布式组网绑定' };

this.dmInstance.bindTarget(deviceId, bindParam, (error, data) => { if (error) { console.error(设备绑定失败: ${error.code}, ${error.message}); promptAction.showToast({ message: '设备绑定失败,请重试', duration: 2000 }); return; }

console.info(`设备绑定成功: ${data.deviceId}`);
promptAction.showToast({
  message: '设备绑定成功',
  duration: 2000
});

}); } 4. 设备信息查询与状态监听 查询可信设备列表: // 在DeviceManager类中添加查询方法 getTrustedDevices() { if (!this.dmInstance) return [];

try { const deviceList = this.dmInstance.getAvailableDeviceListSync(); console.info(查询到 ${deviceList.length} 个可信设备); return deviceList; } catch (error) { console.error(查询设备列表失败: ${(error as BusinessError).code}, ${(error as BusinessError).message}); return []; } } 5. 分布式数据交互示例 基于设备连接的数据传输: import { distributedDataManager } from '@ohos.distributedDataManager';

class DataInteraction { private context: UIContext; private deviceId: string; // 目标设备ID

constructor(context: UIContext, deviceId: string) { this.context = context; this.deviceId = deviceId; }

// 向目标设备发送控制指令(如开灯) sendControlCommand(command: string) { const dataObject = { command: command, timestamp: new Date().getTime() };

distributedDataManager.putData(
  this.context,
  'smartHomeControl', // 数据空间名称
  JSON.stringify(dataObject),
  { targetDeviceId: this.deviceId }
).then(() => {
  console.info(`指令发送成功: ${command}`);
}).catch((error) => {
  console.error(`指令发送失败: ${error}`);
});

}

// 订阅目标设备状态数据 subscribeDeviceStatus() { distributedDataManager.subscribe( this.context, 'smartHomeStatus', // 数据空间名称 { targetDeviceId: this.deviceId }, (data) => { console.info(收到设备状态更新: ${data}); // 解析数据并更新本地UI const status = JSON.parse(data); this.updateDeviceStatusUI(status); } ).then(() => { console.info('设备状态订阅成功'); }).catch((error) => { console.error(订阅失败: ${error}); }); } }

完整DEMO流程代码 本DEMO实现智能家居设备的分布式组网,模拟智能灯泡、空调等设备的发现、绑定、连接及数据交互过程,基于HarmonyOS的Distributed Service Kit能力开发。至少2台HarmonyOS设备(如手机、平板),连接同一局域网或开启蓝牙 。

  1. 分布式组网流程:
    ○ 设备发现:通过Wi-Fi/蓝牙扫描周边设备,支持按类型、距离筛选
    ○ 设备绑定:通过PIN码等方式建立可信连接,确保数据交互安全
    ○ 状态监听:实时感知设备上下线,动态更新组网拓扑
  2. 数据交互能力:
    ○ 设备控制:向目标设备发送指令(如开关灯、调节温度)
    ○ 状态同步:订阅设备数据变化,实时更新本地界面显示
  3. 安全机制:
    ○ 权限控制:必须申请分布式数据同步权限
    ○ 可信认证:绑定流程确保设备身份可信

import promptAction from '@ohos.promptAction'; import { abilityAccessCtrl, common } from '@kit.AbilityKit'; import { distributedDeviceManager } from '@kit.DistributedServiceKit'; import { BusinessError } from '@kit.BasicServicesKit';

class Data { device: distributedDeviceManager.DeviceBasicInfo = { deviceId: '', deviceName: '', deviceType: '', networkId: '' }; }

class ErrorData { reason: number = 0; }

@Entry @Component struct DistributedPage {

private dmInstance: distributedDeviceManager.DeviceManager | null = null; @State discoveredDevices: Array<distributedDeviceManager.DeviceBasicInfo> = []; @State trustedDevices: Array<distributedDeviceManager.DeviceBasicInfo> = [];

aboutToAppear() { this.requestPermission(); }

// 用户申请权限 private async reqPermissionsFromUser(): Promise<number[]> { let context = getContext() as common.UIAbilityContext; let atManager = abilityAccessCtrl.createAtManager(); let grantStatus = await atManager.requestPermissionsFromUser(context, ["ohos.permission.DISTRIBUTED_DATASYNC"]); return grantStatus.authResults; }

private async requestPermission() { let grantStatus = await this.reqPermissionsFromUser(); for (let i = 0; i < grantStatus.length; i++) { if (grantStatus[i] === 0) { // 用户授权,可以继续访问目标操作 this.initDeviceManager(); }else{ promptAction.showToast({ message: '请授予权限以使用分布式功能', duration: 2000 }); } } }

// 初始化设备管理器 initDeviceManager() { try { this.dmInstance = distributedDeviceManager.createDeviceManager('com.example.distributedDemo');

  // 注册设备发现回调
  this.dmInstance.on('discoverSuccess', (data: Data) => {
    this.discoveredDevices.push(data?.device);
  });

  this.dmInstance.on('discoverFailure', (error: ErrorData) => {
    console.error(`发现失败: ${error.reason}`);
    promptAction.showToast({ message: '设备发现失败', duration: 2000 });
  });

  // 注册设备状态变化回调
  this.dmInstance.on('deviceStateChange', (data) => {
    const action = data.action === 1 ? '上线' : '下线';
    promptAction.showToast({ message: `${data.device.deviceName} ${action}`, duration: 1500 });
    this.queryTrustedDevices();
  });

  // 开始发现设备
  this.startDiscovering();
} catch (error) {
  console.error(`创建设备管理器失败: ${(error as BusinessError).code}, ${(error as BusinessError).message}`);
}

}

// 开始发现设备 startDiscovering() { const discoverParam: Record<string, number> = { 'discoverTargetType': 1 }; const filterOptions: Record<string, number> = { 'availableStatus': 0 };

try {
  this.dmInstance?.startDiscovering(discoverParam, filterOptions);
  promptAction.showToast({ message: '正在搜索设备...', duration: 2000 });
} catch (error) {
  console.error(`发现接口调用失败: ${(error as BusinessError).code}, ${(error as BusinessError).message}`);
}

}

// 绑定设备 bindDevice(deviceId: string) { const bindParam: Record<string, string | number> = { 'bindType': 1, 'targetPkgName': 'com.example.device', 'appName': '智能家居', 'appOperation': '绑定', 'customDescription': '分布式组网' };

this.dmInstance?.bindTarget(deviceId, bindParam, (error, data) => {
  if (error) {
    console.error(`绑定失败: ${error.code}, ${error.message}`);
    promptAction.showToast({ message: '绑定失败,请重试', duration: 2000 });
    return;
  }

  promptAction.showToast({ message: '设备绑定成功', duration: 2000 });
  this.queryTrustedDevices();
});

}

// 查询可信设备 queryTrustedDevices() { try { this.trustedDevices = this.dmInstance?.getAvailableDeviceListSync() ?? []; } catch (error) { console.error(查询失败: ${(error as BusinessError).code}, ${(error as BusinessError).message}); } }

// 数据交互示例:向目标设备发送指令 sendDataToDevice(deviceId: string) { // 实际项目中可集成distributedDataManager或remoteService等接口 promptAction.showToast({ message: 向 ${deviceId} 发送控制指令, duration: 1500 }); console.info(模拟向设备 ${deviceId} 发送数据); }

build() { Column() { Text('分布式设备管理DEMO').fontSize(24).fontWeight(FontWeight.Bold).margin({ top: 20 });

  // 操作按钮
  Row() {
    Button('开始发现设备').onClick(() => this.startDiscovering())
    Button('查询可信设备').onClick(() => this.queryTrustedDevices())
  }.margin({ top: 20, bottom: 10 })

  // 发现的设备列表
  Text('发现的设备:').fontSize(18).margin({ top: 10, bottom: 5 })
  List({ space: 5 }) {
    ForEach(this.discoveredDevices, (device: distributedDeviceManager.DeviceBasicInfo) => {
      ListItem() {
        Row() {
          Text(device.deviceName).fontSize(16)
          Text(device.deviceId.substr(0, 8) + '...').fontSize(14).margin({ left: 10 })
          Button('绑定').onClick(() => this.bindDevice(device.deviceId))
            .margin({ left: 'auto' }).width(80)
        }.padding(10).backgroundColor('#F0F0F0')
      }
    })
  }

  // 可信设备列表
  Text('可信设备:').fontSize(18).margin({ top: 20, bottom: 5 })
  List({ space: 5 }) {
    ForEach(this.trustedDevices, (device: distributedDeviceManager.DeviceBasicInfo) => {
      ListItem() {
        Row() {
          Text(device.deviceName).fontSize(16)
          Text(device.deviceId.substr(0, 8) + '...').fontSize(14).margin({ left: 10 })
          Button('控制').onClick(() => this.sendDataToDevice(device.deviceId))
            .margin({ left: 'auto' }).width(80)
        }.padding(10).backgroundColor('#E0F0FF')
      }
    })
  }
}.padding(20).width('100%')

} } 记得配置分布式同步权限 "requestPermissions": [ { "name": "ohos.permission.DISTRIBUTED_DATASYNC", "reason": "$string:app_name", "usedScene": { "abilities": [ "EntryAbility" ], "when":"inuse" } } ]

点赞
收藏
评论区
推荐文章
GeorgeGcs GeorgeGcs
16小时前
【 HarmonyOS 5 入门系列 】鸿蒙HarmonyOS示例项目讲解
【HarmonyOS5入门系列】鸿蒙HarmonyOS示例项目讲解\鸿蒙开发能力HarmonyOSSDK应用服务鸿蒙金融类应用(金融理财一、前言:移动开发声明式UI框架的技术变革在移动操作系统的发展历程中,UI开发模式经历了从命令式到声明式的重大变革。根据
GeorgeGcs GeorgeGcs
16小时前
【HarmonyOS 5】AttributeModifier和AttributeUpdater区别详解
【HarmonyOS5】AttributeModifier和AttributeUpdater区别详解\鸿蒙开发能力HarmonyOSSDK应用服务鸿蒙金融类应用(金融理财一、AttributeModifier和AttributeUpdater的定义和作用1
GeorgeGcs GeorgeGcs
9小时前
从“备胎”到领航者,鸿蒙操作系统的传奇进化
鸿蒙开发能力HarmonyOSSDK应用服务鸿蒙金融类应用(金融理财【HarmonyOS5】2019年,在全球科技产业的风云变幻中,华为正式推出了鸿蒙操作系统(HarmonyOS),这一消息如同一颗重磅炸弹,瞬间吸引了全世界的目光。彼时,外界对鸿蒙的诞生背
GeorgeGcs GeorgeGcs
9小时前
【HarmonyOS 5】鸿蒙跨平台开发方案详解(一)
鸿蒙开发能力HarmonyOSSDK应用服务鸿蒙金融类应用(金融理财一、为什么需要鸿蒙跨平台开发方案?2025年是鸿蒙生态迎来关键发展期。根据前几天的2025HDC数据显示,鸿蒙原生应用数量已从2024年的2000款增长至5000款,微信鸿蒙版安装量突破1
GeorgeGcs GeorgeGcs
7小时前
【HarmonyOS 5】鸿蒙中Stage模型与FA模型详解
鸿蒙开发能力HarmonyOSSDK应用服务鸿蒙金融类应用(金融理财一、前言在HarmonyOS5的应用开发模型中,featureAbility是旧版FA模型(FeatureAbility)的用法,Stage模型已采用全新的应用架构,推荐使用组件化的上下文
GeorgeGcs GeorgeGcs
7小时前
【HarmonyOS 5】鸿蒙星闪NearLink详解
鸿蒙开发能力HarmonyOSSDK应用服务鸿蒙金融类应用(金融理财一、前言鸿蒙星闪NearLinkKit是HarmonyOS提供的短距离通信服务,支持星闪设备间的连接、数据交互。例如,手机可作为中心设备与外围设备(如鼠标、手写笔、智能家电、车钥匙等)通过
GeorgeGcs GeorgeGcs
7小时前
【HarmonyOS 5】金融应用开发鸿蒙组件实践
鸿蒙开发能力HarmonyOSSDK应用服务鸿蒙金融类应用(金融理财一、鸿蒙生态观察2024年1月18日:发布原生鸿蒙操作系统星河版,面向开发者开放申请,余承东宣布鸿蒙生态设备数达8亿台;建设银行、邮储银行等完成鸿蒙原生应用Beta版本开发。2024年10
GeorgeGcs GeorgeGcs
7小时前
【HarmonyOS 5】鸿蒙中Stage模型与FA模型详解
鸿蒙开发能力HarmonyOSSDK应用服务鸿蒙金融类应用(金融理财一、前言在HarmonyOS5的应用开发模型中,featureAbility是旧版FA模型(FeatureAbility)的用法,Stage模型已采用全新的应用架构,推荐使用组件化的上下文
GeorgeGcs GeorgeGcs
7小时前
鸿蒙 6.0 引爆 AI 智能体革命:从交互重构到全场景智能觉醒,未来已至
【HarmonyOS5】鸿蒙开发能力HarmonyOSSDK应用服务鸿蒙金融类应用(金融理财一、前言今天的华为开发者大会(2025HDC),全程看完,我只想说,震撼对于用户来说,一个未来场景的手机操作系统,正在诞生,从文本交互的操作转向自然语言,未来手机用
GeorgeGcs GeorgeGcs
5小时前
【HarmonyOS 5】鸿蒙应用数据安全详解
鸿蒙开发能力HarmonyOSSDK应用服务鸿蒙金融类应用(金融理财一、前言大家平时用手机、智能手表的时候,最担心什么?肯定是自己的隐私数据会不会泄露!今天就和大家唠唠HarmonyOS是怎么把应用安全这块“盾牌”打造得明明白白的,从里到外保护我们的信息。
GeorgeGcs
GeorgeGcs
Lv1
男 · 金融头部企业 · 鸿蒙应用架构师
HarmonyOS认证创作先锋,华为HDE专家,鸿蒙讲师,作者。目前任职鸿蒙应用架构师。 历经腾讯,宝马,研究所,金融。 待过私企,外企,央企。 深耕大应用开发领域十年。 AAE,Harmony(OpenHarmony\HarmonyOS),MAE(Android\IOS),FE(H5\Vue\RN)。
文章
56
粉丝
1
获赞
2