From 3460722bd1fe5accad3b7f71670568310d77e60e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9F=A9=E6=9D=A8?= Date: Thu, 20 Oct 2022 19:56:33 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E5=A4=87=E7=AE=A1=E7=90=86=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 韩杨 --- entry/src/main/ets/Model/RemoteDeviceModel.ts | 182 ++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 entry/src/main/ets/Model/RemoteDeviceModel.ts diff --git a/entry/src/main/ets/Model/RemoteDeviceModel.ts b/entry/src/main/ets/Model/RemoteDeviceModel.ts new file mode 100644 index 0000000..e57ff4c --- /dev/null +++ b/entry/src/main/ets/Model/RemoteDeviceModel.ts @@ -0,0 +1,182 @@ +/* +* Copyright (c) 2022 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import hardware_deviceManager from '@ohos.distributedHardware.deviceManager' +import Logger from '../model/Logger' + +let SUBSCRIBE_ID: number = 100 +const RANDOM: number = 65536 +const TAG: string = 'RemoteDeviceModel' + +export class RemoteDeviceModel { + public deviceLists: Array = [] //受信任设备列表 + public discoverLists: Array = [] //在线设备列表 + private callback: () => void = null + private authCallback: (device:hardware_deviceManager.DeviceInfo) => void = null + private deviceManager: hardware_deviceManager.DeviceManager = undefined + + registerDeviceListCallback(callback) { + if (typeof (this.deviceManager) === 'undefined') { + Logger.info(TAG, 'deviceManager.createDeviceManager begin') + //创建一个设备管理器实例。 param1(指示应用程序的包名。) param2(回调函数,用来返回deviceManager对象) + hardware_deviceManager.createDeviceManager('ohos.samples.etsdistributedmusicplayer', (error, value) => { + if (error) { + Logger.error(TAG, 'createDeviceManager failed.') + return + } + this.deviceManager = value + this.registerDeviceList(callback) + Logger.info(TAG, `createDeviceManager callback returned, error= ${error} value= ${value}`) + }) + Logger.info(TAG, 'deviceManager.createDeviceManager end') + } else { + this.registerDeviceList(callback) + } + } + + registerDeviceList(callback) { + Logger.info(TAG, 'registerDeviceListCallback') + this.callback = callback + if (this.deviceManager === undefined) { + Logger.error(TAG, 'deviceManager has not initialized') + this.callback() + return + } + + Logger.info(TAG, 'getTrustedDeviceListSync begin') + let list = this.deviceManager.getTrustedDeviceListSync()//同步获取所有可信设备列表。可信设备 + Logger.info(TAG, `getTrustedDeviceListSync end, deviceLists= ${JSON.stringify(list)}`) + if (typeof (list) !== 'undefined' && typeof (list.length) !== 'undefined') { + this.deviceLists = list + } + this.callback() + Logger.info(TAG, 'callback finished') + + this.deviceManager.on('deviceStateChange', (data) => { //注册设备状态回调。(这个监听函数应该是点击连接设备的时候被调用) + Logger.info(TAG, `deviceStateChange data= ${JSON.stringify(data)}`) + switch (data.action) { + case 0: //设备上线。 + this.deviceLists[this.deviceLists.length] = data.device//能进到这个方法里面就说明设备已经被认证过了 + Logger.info(TAG, `online, updated device list= ${JSON.stringify(this.deviceLists)}`) + this.callback() + if (this.authCallback !== null) { + Logger.info(TAG,'123456789') + this.authCallback(data.device) //这里是不是要加个参数进去呀,因为Player的第257行的回调函数里面需要传个参数进去 + this.authCallback = null + } + break + case 1: //设备就绪,设备信息同步已完成。(设备就绪时,为什么把当前设备给移除出去了? 似乎就像连接wifi一样,当wifi连接上以后,当前wifi就从wifi列表中移除了) + if (this.deviceLists.length > 0) { + let list = [] + for (let i = 0; i < this.deviceLists.length; i++) { + if (this.deviceLists[i].deviceId !== data.device.deviceId) { + list[i] = data.device + } + } + this.deviceLists = list + } + Logger.info(TAG, `offline, updated device list= ${JSON.stringify(data.device)} `) + this.callback() + break + case 2: //设备下线。 ????? + if (this.deviceLists.length > 0) { + for (let i = 0; i < this.deviceLists.length; i++) { + //这个部分很奇怪,如果说这个if能进去,那就说明这个设备本来就在deviceLists,那接下来为什么还要再赋值一次,if里的代码不就没用了吗? + if (this.deviceLists[i].deviceId === data.device.deviceId) { + this.deviceLists[i] = data.device + break + } + } + } + Logger.info(TAG, `change, updated device list= ${JSON.stringify(this.deviceLists)}`) + this.callback() + break + default: + break + } + }) + this.deviceManager.on('deviceFound', (data) => { //(搜索在线设备)注册一个找到设备的回调,以便在找到设备时通知应用程序 + Logger.info(TAG, `deviceFound data= ${JSON.stringify(data)}`) + Logger.info(TAG, `deviceFound this.deviceLists= ${this.deviceLists}, this.deviceLists.length= ${this.deviceLists.length}`) + for (let i = 0;i < this.discoverLists.length; i++) {//循环检查此设备有没有被添加过 + if (this.discoverLists[i].deviceId === data.device.deviceId) { + Logger.info(TAG, 'device founded, ignored') //设备已存在 + return + } + } + this.discoverLists[this.discoverLists.length] = data.device + this.callback() + }) + this.deviceManager.on('discoverFail', (data) => { //注册一个设备发现结果回调,以便在设备发现失败时通知应用程序 + Logger.info(TAG, `discoverFail data= ${JSON.stringify(data)}`) + }) + this.deviceManager.on('serviceDie', () => { //注册一个serviceError回调,以便在devicemanager服务死亡时通知应用程序 + Logger.error(TAG, 'serviceDie') + }) + + SUBSCRIBE_ID = Math.floor(RANDOM * Math.random()) + let info = { + subscribeId: SUBSCRIBE_ID, //服务订阅ID【0~65535】,对每个发现进程来说应该是唯一的 + mode: 0xAA, //设备发现模式 0xAA主动,0x55被动 + medium: 2, //服务订阅的媒介 0(auto) 1(BLE蓝牙) 2(COAP Wi-Fi) 3(USB) + freq: 2, //设备发现频率 2(High) + isSameAccount: false, //只找账号相同的设备 + isWakeRemote: true, //是否发现睡眠设备 + capability: 0 //设备发现能力(不知道具体表现在什么方面) + } + Logger.info(TAG, `startDeviceDiscovery ${SUBSCRIBE_ID}`) + this.deviceManager.startDeviceDiscovery(info) //开始发现设备(通过SUBSCRIBE_ID搜索分布式组网内的远端设备) + } + + authDevice(device, callback) {//认证(在Player的第283行被调用) 在这里的结果就是挂起回调函数,等待有缘人(在设备上线的监听中被调用)去调用 + Logger.info(TAG, `authDevice ${device}`) + for (let i = 0; i < this.discoverLists.length; i++) { + if (this.discoverLists[i].deviceId === device.deviceId) {// 在线设备中存在此设备,创建扩展信息 + Logger.info(TAG, 'device founded, ignored') + let extraInfo = { //扩展信息 + "targetPkgName": 'ohos.samples.etsdistributevideoplayer', + "appName": 'DistributeVideo', + "appDescription": 'DistributeVideo player application', + "business": '0' + } + let authParam = { //设备验证参数 + "authType": 1, //认证类型,1为pin码。(PIN码认证) + "appIcon": '', + "appThumbnail": '', + "extraInfo": extraInfo + } + Logger.info(TAG, `authenticateDevice ${JSON.stringify(this.discoverLists[i])}`) + this.deviceManager.authenticateDevice(this.discoverLists[i], authParam, (err, data) => { //验证指定设备 + Logger.info(TAG, `authenticateDevice succeed, data= ${JSON.stringify(data)}`) + this.authCallback = callback + callback(device)//这里看情况要不要注释掉 + }) + } + } + } + + unregisterDeviceListCallback() { + Logger.info(TAG, `stopDeviceDiscovery ${SUBSCRIBE_ID}`) + if (this.deviceManager === undefined) { + return + } + this.deviceManager.stopDeviceDiscovery(SUBSCRIBE_ID) + this.deviceManager.off('deviceStateChange') + this.deviceManager.off('deviceFound') + this.deviceManager.off('discoverFail') + this.deviceManager.off('serviceDie') + this.deviceLists = [] + } +} -- Gitee