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 */ 15import deviceManager from '@ohos.distributedHardware.deviceManager' 16import { BUNDLE } from '../model/Const' 17import Logger from '../model/Logger' 18 19let SUBSCRIBE_ID: number = 100 20const TAG: string = 'RemoteDeviceModel' 21 22class RemoteDeviceModel { 23 public devices: Array<deviceManager.DeviceInfo> = [] 24 public discoverDevices: Array<deviceManager.DeviceInfo> = [] 25 private stateChangeCallback?: () => void 26 private authCallback?: (device: deviceManager.DeviceInfo) => void 27 private deviceManager?: deviceManager.DeviceManager 28 29 registerDeviceListCallback(stateChangeCallback: () => void) { 30 if (typeof (this.deviceManager) !== 'undefined') { 31 this.registerDeviceListCallbackImplement(stateChangeCallback) 32 return 33 } 34 Logger.info(TAG, 'deviceManager.createDeviceManager begin') 35 deviceManager.createDeviceManager(BUNDLE, (error, value) => { 36 if (error) { 37 Logger.error(TAG, 'createDeviceManager failed.') 38 return 39 } 40 this.deviceManager = value 41 this.registerDeviceListCallbackImplement(stateChangeCallback) 42 Logger.info(TAG, `createDeviceManager callback returned,value=${value}`) 43 }) 44 Logger.info(TAG, 'deviceManager.createDeviceManager end') 45 } 46 47 onDeviceStateChangeActionOnline(device) { 48 this.devices[this.devices.length] = device 49 Logger.info(TAG, `online, device list=${JSON.stringify(this.devices)}`) 50 if (this.authCallback !== null) { 51 this.authCallback(device) 52 this.authCallback = null 53 } 54 } 55 56 onDeviceStateChangeActionReady(device) { 57 if (this.devices.length <= 0) { 58 this.stateChangeCallback() 59 return 60 } 61 62 let list = this.devices.filter((value) => { 63 return value !== device.deviceId 64 }) 65 66 this.devices = list 67 Logger.info(TAG, `ready, device list=${JSON.stringify(this.devices)}`) 68 this.stateChangeCallback() 69 } 70 71 getLocalDevice() { 72 Logger.info(TAG, `getLocalDevice`) 73 let deviceInfo: deviceManager.DeviceInfo = this.deviceManager.getLocalDeviceInfoSync() 74 Logger.info(TAG, `local deviceInfo=${JSON.stringify(deviceInfo)}`) 75 return deviceInfo.deviceId 76 } 77 78 registerDeviceListCallbackImplement(stateChangeCallback: () => void) { 79 Logger.info(TAG, 'registerDeviceListCallback') 80 this.stateChangeCallback = stateChangeCallback 81 if (this.deviceManager === undefined) { 82 Logger.error(TAG, 'deviceManager has not initialized') 83 this.stateChangeCallback() 84 return 85 } 86 Logger.info(TAG, 'getTrustedDeviceListSync begin') 87 let list = this.deviceManager.getTrustedDeviceListSync() 88 Logger.info(TAG, `getTrustedDeviceListSync end, devices=${JSON.stringify(list)}`) 89 if (typeof (list) !== 'undefined' && typeof (list.length) !== 'undefined') { 90 this.devices = list 91 } 92 this.stateChangeCallback() 93 Logger.info(TAG, 'callback finished') 94 this.deviceManager.on('deviceStateChange', (data) => { 95 if (data === null) { 96 return 97 } 98 Logger.info(TAG, `deviceStateChange data = ${JSON.stringify(data)}`) 99 switch (data.action) { 100 case deviceManager.DeviceStateChangeAction.READY: 101 this.discoverDevices = [] 102 this.devices.push(data.device) 103 this.stateChangeCallback() 104 let list = this.deviceManager.getTrustedDeviceListSync() 105 if (typeof (list) !== 'undefined' && typeof (list.length) !== 'undefined') { 106 this.devices = list 107 } 108 this.stateChangeCallback() 109 break 110 default: 111 break 112 } 113 }) 114 this.deviceManager.on('deviceFound', (data) => { 115 if (data === null) { 116 return 117 } 118 Logger.info(TAG, `deviceFound data=${JSON.stringify(data)}`) 119 this.onDeviceFound(data) 120 }) 121 this.deviceManager.on('discoverFail', (data) => { 122 Logger.info(TAG, `discoverFail data=${JSON.stringify(data)}`) 123 }) 124 this.deviceManager.on('serviceDie', () => { 125 Logger.info(TAG, 'serviceDie') 126 }) 127 this.startDeviceDiscovery() 128 } 129 130 onDeviceFound(data) { 131 for (let i = 0;i < this.discoverDevices.length; i++) { 132 if (this.discoverDevices[i].deviceId === data.device.deviceId) { 133 Logger.info(TAG, 'device founded ignored') 134 return 135 } 136 } 137 this.discoverDevices[this.discoverDevices.length] = data.device 138 Logger.info(TAG, `deviceFound self.discoverDevices=${this.discoverDevices}`) 139 this.stateChangeCallback() 140 } 141 142 startDeviceDiscovery() { 143 SUBSCRIBE_ID = Math.floor(65536 * Math.random()) 144 var info = { 145 subscribeId: SUBSCRIBE_ID, 146 mode: 0xAA, 147 medium: 2, 148 freq: 2, 149 isSameAccount: false, 150 isWakeRemote: true, 151 capability: 0 152 } 153 Logger.info(TAG, `startDeviceDiscovery${SUBSCRIBE_ID}`) 154 this.deviceManager.startDeviceDiscovery(info) 155 } 156 157 unregisterDeviceListCallback() { 158 Logger.info(TAG, `stopDeviceDiscovery${SUBSCRIBE_ID}`) 159 this.deviceManager.stopDeviceDiscovery(SUBSCRIBE_ID) 160 this.deviceManager.off('deviceStateChange') 161 this.deviceManager.off('deviceFound') 162 this.deviceManager.off('discoverFail') 163 this.deviceManager.off('serviceDie') 164 this.devices = [] 165 this.discoverDevices = [] 166 } 167 168 authenticateDevice(device, callBack) { 169 Logger.info(TAG, `authenticateDevice ${JSON.stringify(device)}`) 170 for (let i = 0; i < this.discoverDevices.length; i++) { 171 if (this.discoverDevices[i].deviceId !== device.deviceId) { 172 continue 173 } 174 let extraInfo = { 175 'targetPkgName': BUNDLE, 176 'appName': 'Distributed rdb', 177 'appDescription': 'Distributed rdb', 178 'business': '0' 179 } 180 let authParam = { 181 'authType': 1, 182 'appIcon': '', 183 'appThumbnail': '', 184 'extraInfo': extraInfo 185 } 186 this.deviceManager.authenticateDevice(device, authParam, (err, data) => { 187 if (err) { 188 Logger.info(TAG, `authenticateDevice error: ${JSON.stringify(err)}`) 189 } 190 Logger.info(TAG, `authenticateDevice succeed: ${JSON.stringify(data)}`) 191 this.authCallback = callBack 192 }) 193 } 194 } 195} 196 197export default new RemoteDeviceModel()