1/* 2* Copyright (c) 2022 Huawei Device Co., Ltd. 3* Licensed under the Apache License, Version 2.0 (the "License"); 4* you may not use this file except in compliance with the License. 5* You may obtain a copy of the License at 6* 7* http://www.apache.org/licenses/LICENSE-2.0 8* 9* Unless required by applicable law or agreed to in writing, software 10* distributed under the License is distributed on an "AS IS" BASIS, 11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12* See the License for the specific language governing permissions and 13* limitations under the License. 14*/ 15 16import hardware_deviceManager from '@ohos.distributedHardware.deviceManager' 17import Logger from '../model/Logger' 18 19let SUBSCRIBE_ID: number = 100 20const RANDOM: number = 65536 21const TAG: string = 'RemoteDeviceModel' 22 23export class RemoteDeviceModel { 24 public deviceLists: Array<hardware_deviceManager.DeviceInfo> = [] 25 public discoverLists: Array<hardware_deviceManager.DeviceInfo> = [] 26 private callback: () => void = null 27 private authCallback: () => void = null 28 private deviceManager: hardware_deviceManager.DeviceManager = undefined 29 30 registerDeviceListCallback(callback) { 31 if (typeof (this.deviceManager) === 'undefined') { 32 Logger.info(TAG, 'deviceManager.createDeviceManager begin') 33 hardware_deviceManager.createDeviceManager('ohos.samples.etsdistributedmusicplayer', (error, value) => { 34 if (error) { 35 Logger.error(TAG, 'createDeviceManager failed.') 36 return 37 } 38 this.deviceManager = value 39 this.registerDeviceList(callback) 40 Logger.info(TAG, `createDeviceManager callback returned, error= ${error} value= ${value}`) 41 }) 42 Logger.info(TAG, 'deviceManager.createDeviceManager end') 43 } else { 44 this.registerDeviceList(callback) 45 } 46 } 47 48 registerDeviceList(callback) { 49 Logger.info(TAG, 'registerDeviceListCallback') 50 this.callback = callback 51 if (this.deviceManager === undefined) { 52 Logger.error(TAG, 'deviceManager has not initialized') 53 this.callback() 54 return 55 } 56 57 Logger.info(TAG, 'getTrustedDeviceListSync begin') 58 let list = this.deviceManager.getTrustedDeviceListSync() 59 Logger.info(TAG, `getTrustedDeviceListSync end, deviceLists= ${JSON.stringify(list)}`) 60 if (typeof (list) !== 'undefined' && typeof (list.length) !== 'undefined') { 61 this.deviceLists = list 62 } 63 this.callback() 64 Logger.info(TAG, 'callback finished') 65 66 this.deviceManager.on('deviceStateChange', (data) => { 67 Logger.info(TAG, `deviceStateChange data= ${JSON.stringify(data)}`) 68 switch (data.action) { 69 case hardware_deviceManager.DeviceStateChangeAction.READY: 70 this.discoverLists = [] 71 this.deviceLists.push(data.device) 72 Logger.info(TAG, `reday, updated device list= ${JSON.stringify(this.deviceLists)} `) 73 let list = this.deviceManager.getTrustedDeviceListSync(); 74 Logger.info(TAG, `getTrustedDeviceListSync end, deviceList= ${JSON.stringify(list)}`) 75 if (typeof (list) !== 'undefined' && typeof (list.length) !== 'undefined') { 76 this.deviceLists = list; 77 } 78 this.callback() 79 break 80 case hardware_deviceManager.DeviceStateChangeAction.OFFLINE: 81 if (this.deviceLists.length > 0) { 82 let list = []; 83 for (let i = 0; i < this.deviceLists.length; i++) { 84 if (this.deviceLists[i].deviceId !== data.device.deviceId) { 85 list[i] = data.device; 86 } 87 } 88 this.deviceLists = list; 89 } 90 Logger.info(TAG, `offline, updated device list= ${JSON.stringify(this.deviceLists)}`) 91 this.callback() 92 break 93 default: 94 break 95 } 96 }) 97 this.deviceManager.on('deviceFound', (data) => { 98 Logger.info(TAG, `deviceFound data= ${JSON.stringify(data)}`) 99 Logger.info(TAG, `deviceFound this.deviceLists= ${this.deviceLists}, this.deviceLists.length= ${this.deviceLists.length}`) 100 for (let i = 0;i < this.discoverLists.length; i++) { 101 if (this.discoverLists[i].deviceId === data.device.deviceId) { 102 Logger.info(TAG, 'device founded, ignored') 103 return 104 } 105 } 106 this.discoverLists[this.discoverLists.length] = data.device 107 this.callback() 108 }) 109 this.deviceManager.on('discoverFail', (data) => { 110 Logger.info(TAG, `discoverFail data= ${JSON.stringify(data)}`) 111 }) 112 this.deviceManager.on('serviceDie', () => { 113 Logger.error(TAG, 'serviceDie') 114 }) 115 116 SUBSCRIBE_ID = Math.floor(RANDOM * Math.random()) 117 let info = { 118 subscribeId: SUBSCRIBE_ID, 119 mode: 0xAA, 120 medium: 2, 121 freq: 2, 122 isSameAccount: false, 123 isWakeRemote: true, 124 capability: 0 125 } 126 Logger.info(TAG, `startDeviceDiscovery ${SUBSCRIBE_ID}`) 127 this.deviceManager.startDeviceDiscovery(info) 128 } 129 130 authDevice(device, callback) { 131 Logger.info(TAG, `authDevice ${device}`) 132 for (let i = 0; i < this.discoverLists.length; i++) { 133 if (this.discoverLists[i].deviceId === device.deviceId) { 134 Logger.info(TAG, 'device founded, ignored') 135 let extraInfo = { 136 "targetPkgName": 'ohos.samples.etsdistributedmusicplayer', 137 "appName": 'Music', 138 "appDescription": 'Music player application', 139 "business": '0' 140 } 141 let authParam = { 142 "authType": 1, 143 "appIcon": '', 144 "appThumbnail": '', 145 "extraInfo": extraInfo 146 } 147 Logger.info(TAG, `authenticateDevice ${JSON.stringify(this.discoverLists[i])}`) 148 this.deviceManager.authenticateDevice(this.discoverLists[i], authParam, (err, data) => { 149 Logger.info(TAG, `authenticateDevice succeed, data= ${JSON.stringify(data)}`) 150 this.authCallback = callback 151 }) 152 } 153 } 154 } 155 156 unregisterDeviceListCallback() { 157 Logger.info(TAG, `stopDeviceDiscovery ${SUBSCRIBE_ID}`) 158 if (this.deviceManager === undefined) { 159 return 160 } 161 this.deviceManager.stopDeviceDiscovery(SUBSCRIBE_ID) 162 this.deviceManager.off('deviceStateChange') 163 this.deviceManager.off('deviceFound') 164 this.deviceManager.off('discoverFail') 165 this.deviceManager.off('serviceDie') 166 this.deviceLists = [] 167 } 168} 169