• 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
27    constructor() {
28    }
29
30    registerDeviceListCallback(callback) {
31        if (typeof (this.#deviceManager) === 'undefined') {
32            console.log('[dmsDemo] deviceManager.createDeviceManager begin');
33            let self = this;
34            deviceManager.createDeviceManager('com.ohos.distributedmusicplayer', (error, value) => {
35                if (error) {
36                    console.error('createDeviceManager failed.');
37                    return;
38                }
39                self.#deviceManager = value;
40                self.registerDeviceListCallback_(callback);
41                console.log('[dmsDemo] createDeviceManager callback returned, error=' + error + ' value=' + value);
42            });
43            console.log('[dmsDemo] deviceManager.createDeviceManager end');
44        } else {
45            this.registerDeviceListCallback_(callback);
46        }
47    }
48
49    registerDeviceListCallback_(callback) {
50        console.info('[dmsDemo] registerDeviceListCallback');
51        this.callback = callback;
52        if (this.#deviceManager == undefined) {
53            console.error('[dmsDemo] deviceManager has not initialized');
54            this.callback();
55            return;
56        }
57
58        console.info('[dmsDemo] getTrustedDeviceListSync begin');
59        var list = this.#deviceManager.getTrustedDeviceListSync();
60        console.info('[dmsDemo] getTrustedDeviceListSync end, deviceList=' + JSON.stringify(list));
61        if (typeof (list) != 'undefined' && typeof (list.length) != 'undefined') {
62            this.deviceList = list;
63        }
64        this.callback();
65        console.info('[dmsDemo] callback finished');
66
67        registerDeviceStateChangeCallback();
68        registerDeviceFoundCallback();
69        registerDiscoverFailCallback();
70        registerServiceDieCallback();
71
72        SUBSCRIBE_ID = Math.floor(65536 * Math.random());
73        var info = {
74            subscribeId: SUBSCRIBE_ID,
75            mode: 0xAA,
76            medium: 2,
77            freq: 2,
78            isSameAccount: false,
79            isWakeRemote: true,
80            capability: 0
81        };
82        console.info('[dmsDemo] startDeviceDiscovery ' + SUBSCRIBE_ID);
83        this.#deviceManager.startDeviceDiscovery(info);
84    }
85
86    registerDeviceStateChangeCallback() {
87        let self = this;
88        this.#deviceManager.on('deviceStateChange', (data) => {
89            console.info('[dmsDemo] deviceStateChange data=' + JSON.stringify(data));
90            switch (data.action) {
91                case 0:
92                    self.deviceList[self.deviceList.length] = data.device;
93                    console.info('[dmsDemo] online, updated device list=' + JSON.stringify(self.deviceList));
94                    self.callback();
95                    if (self.authCallback != null) {
96                        self.authCallback();
97                        self.authCallback = null;
98                    }
99                    break;
100                case 2:
101                    if (self.deviceList.length > 0) {
102                        for (var i = 0; i < self.deviceList.length; i++) {
103                            if (self.deviceList[i].deviceId === data.device.deviceId) {
104                                self.deviceList[i] = data.device;
105                                break;
106                            }
107                        }
108                    }
109                    console.info('[dmsDemo] change, updated device list=' + JSON.stringify(self.deviceList));
110                    self.callback();
111                    break;
112                case 1:
113                    if (self.deviceList.length > 0) {
114                        var list = [];
115                        for (var i = 0; i < self.deviceList.length; i++) {
116                            if (self.deviceList[i].deviceId != data.device.deviceId) {
117                                list[i] = data.device;
118                            }
119                        }
120                        self.deviceList = list;
121                    }
122                    console.info('[dmsDemo] offline, updated device list=' + JSON.stringify(data.device));
123                    self.callback();
124                    break;
125                default:
126                    break;
127            }
128        });
129    }
130
131    registerDeviceFoundCallback() {
132        this.#deviceManager.on('deviceFound', (data) => {
133            console.info('[dmsDemo] deviceFound data=' + JSON.stringify(data));
134            console.info('[dmsDemo] deviceFound self.deviceList=' + self.deviceList);
135            console.info('[dmsDemo] deviceFound self.deviceList.length=' + self.deviceList.length);
136            for (var i = 0; i < self.discoverList.length; i++) {
137                if (self.discoverList[i].deviceId === data.device.deviceId) {
138                    console.info('[dmsDemo] device founded, ignored');
139                    return;
140                }
141            }
142            self.discoverList[self.discoverList.length] = data.device;
143            self.callback();
144        });
145    }
146
147    registerDiscoverFailCallback() {
148        this.#deviceManager.on('discoverFail', (data) => {
149            console.info('[dmsDemo] discoverFail data=' + JSON.stringify(data));
150        });
151    }
152
153    registerServiceDieCallback() {
154        this.#deviceManager.on('serviceDie', () => {
155            console.error('[dmsDemo] serviceDie');
156        });
157    }
158
159    authDevice(deviceId, callback) {
160        console.info('[dmsDemo] authDevice ' + deviceId);
161        for (var i = 0; i < this.discoverList.length; i++) {
162            if (this.discoverList[i].deviceId === deviceId) {
163                console.info('[dmsDemo] device founded, ignored');
164                let extraInfo = {
165                    "targetPkgName": 'com.ohos.distributedmusicplayer',
166                    "appName": 'Music',
167                    "appDescription": 'Music player application',
168                    "business": '0'
169                };
170                let authParam = {
171                    "authType": 1,
172                    "appIcon": '',
173                    "appThumbnail": '',
174                    "extraInfo": extraInfo
175                };
176                console.info('[dmsDemo] authenticateDevice ' + JSON.stringify(this.discoverList[i]));
177                let self = this;
178                this.#deviceManager.authenticateDevice(this.discoverList[i], authParam, (err, data) => {
179                    if (err) {
180                        console.info('[dmsDemo] authenticateDevice failed, err=' + JSON.stringify(err));
181                        self.authCallback = null;
182                    } else {
183                        console.info('[dmsDemo] authenticateDevice succeed, data=' + JSON.stringify(data));
184                        self.authCallback = callback;
185                    }
186                });
187            }
188        }
189    }
190
191    unregisterDeviceListCallback() {
192        console.info('[dmsDemo] stopDeviceDiscovery ' + SUBSCRIBE_ID);
193        this.#deviceManager.stopDeviceDiscovery(SUBSCRIBE_ID);
194        this.#deviceManager.off('deviceStateChange');
195        this.#deviceManager.off('deviceFound');
196        this.#deviceManager.off('discoverFail');
197        this.#deviceManager.off('serviceDie');
198        this.deviceList = [];
199    }
200}