• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021 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 deviceManager from '@ohos.distributedHardware.deviceManager';
17
18var SUBSCRIBE_ID = 100;
19
20export default class RemoteDeviceModel {
21    deviceList = [];
22    discoverList = [];
23    callback;
24    authCallback = null;
25    #deviceManager;
26    constructor() {
27    }
28
29    registerDeviceListCallback(callback) {
30        if (typeof (this.#deviceManager) === 'undefined') {
31            console.log('[dmsDemo] deviceManager.createDeviceManager begin');
32            let self = this;
33            deviceManager.createDeviceManager('ohos.samples.etsDemo', (error, value) => {
34                if (error) {
35                    console.error('createDeviceManager failed.');
36                    return;
37                }
38                self.#deviceManager = value;
39                self.registerDeviceListCallback_(callback);
40                console.log('[dmsDemo] createDeviceManager callback returned, error=' + error + ' value=' + value);
41            });
42            console.log('[dmsDemo] deviceManager.createDeviceManager end');
43        } else {
44            this.registerDeviceListCallback_(callback);
45        }
46    }
47
48    registerDeviceListCallback_(callback) {
49        console.info('[dmsDemo] registerDeviceListCallback');
50        this.callback = callback;
51        if (this.#deviceManager == undefined) {
52            console.error('[dmsDemo] deviceManager has not initialized');
53            this.callback();
54            return;
55        }
56
57        console.info('[dmsDemo] getTrustedDeviceListSync begin');
58        var list = this.#deviceManager.getTrustedDeviceListSync();
59        console.info('[dmsDemo] getTrustedDeviceListSync end, deviceList=' + JSON.stringify(list));
60        if (typeof (list) != 'undefined' && typeof (list.length) != 'undefined') {
61            this.deviceList = list;
62        }
63        this.callback();
64        console.info('[dmsDemo] callback finished');
65
66        let self = this;
67        this.#deviceManager.on('deviceStateChange', (data) => {
68            console.info('[dmsDemo] deviceStateChange data=' + JSON.stringify(data));
69            switch (data.action) {
70                case deviceManager.DeviceStateChangeAction.OFFLINE:
71                    if (self.deviceList.length > 0) {
72                        for (var i = 0; i < self.deviceList.length; i++) {
73                            if (self.deviceList[i].deviceId === data.device.deviceId) {
74                                self.deviceList[i] = data.device;
75                                break
76                            }
77                        }
78                    }
79                    console.info('[dmsDemo] change, updated device list=' + JSON.stringify(self.deviceList))
80                    self.callback()
81                    break
82                case deviceManager.DeviceStateChangeAction.READY:
83                    this.discoverList = []
84                    this.deviceList.push(data.device)
85                    this.callback();
86                    let list = this.#deviceManager.getTrustedDeviceListSync()
87                    if (typeof (list) !== 'undefined' && typeof (list.length) !== 'undefined') {
88                        this.deviceList = list
89                    }
90                    this.callback()
91                    break
92                default:
93                    break
94            }
95        })
96        this.#deviceManager.on('deviceFound', (data) => {
97            console.info('[dmsDemo] deviceFound data=' + JSON.stringify(data));
98            console.info('[dmsDemo] deviceFound self.deviceList=' + self.deviceList);
99            console.info('[dmsDemo] deviceFound self.deviceList.length=' + self.deviceList.length);
100            for (var i = 0; i < self.discoverList.length; i++) {
101                if (self.discoverList[i].deviceId === data.device.deviceId) {
102                    console.info('[dmsDemo] device founded, ignored');
103                    return;
104                }
105            }
106            self.discoverList[self.discoverList.length] = data.device;
107            self.callback();
108        });
109        this.#deviceManager.on('discoverFail', (data) => {
110            console.info('[dmsDemo] discoverFail data=' + JSON.stringify(data));
111        });
112        this.#deviceManager.on('serviceDie', () => {
113            console.error('[dmsDemo] serviceDie');
114        });
115
116        SUBSCRIBE_ID = Math.floor(65536 * Math.random());
117        var 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        console.info('[dmsDemo] startDeviceDiscovery ' + SUBSCRIBE_ID);
127        this.#deviceManager.startDeviceDiscovery(info);
128    }
129
130    authDevice(deviceId, callback) {
131        console.info('[dmsDemo] authDevice ' + deviceId);
132        for (var i = 0; i < this.discoverList.length; i++) {
133            if (this.discoverList[i].deviceId === deviceId) {
134                console.info('[dmsDemo] device founded, ignored');
135                let extraInfo = {
136                    "targetPkgName": 'ohos.samples.etsDemo',
137                    "appName": 'DMS',
138                    "appDescription": 'Ability and Service',
139                    "business": '0'
140                };
141                let authParam = {
142                    "authType": 1,
143                    "appIcon": '',
144                    "appThumbnail": '',
145                    "extraInfo": extraInfo
146                };
147                console.info('[dmsDemo] authenticateDevice ' + JSON.stringify(this.discoverList[i]));
148                let self = this;
149                this.#deviceManager.authenticateDevice(this.discoverList[i], authParam, (err, data) => {
150                    if (err) {
151                        console.info('[dmsDemo] authenticateDevice failed, err=' + JSON.stringify(err));
152                        self.authCallback = null;
153                    } else {
154                        console.info('[dmsDemo] authenticateDevice succeed, data=' + JSON.stringify(data));
155                        self.authCallback = callback;
156                    }
157                });
158            }
159        }
160    }
161}