• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2023 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';
17import featureAbility from '@ohos.ability.featureAbility';
18
19var results;
20var bundleName = "com.ohos.fileiotest";
21var abilityName = "com.ohos.fileiotest.ServiceAbility";
22var deviceList;
23
24export default class TestService {
25    constructor() {
26
27    }
28
29    getDeviceList(deviceManager) {
30        deviceList = deviceManager.getTrustedDeviceListSync();
31        console.info("getDeviceList success, deviceList id: " + JSON.stringify(deviceList))
32    }
33
34    toConnectAbility() {
35        console.info("fileioClient:  toConnectAbility")
36        return new Promise(resolve=>{
37            let self = this;
38            deviceManager.createDeviceManager('com.ohos.fileiotest', (error, deviceManager) => {
39                self.getDeviceList(deviceManager);
40                console.info("fileioClient:  got deviceManager: " + deviceManager)
41                let networkId = deviceList[0].networkId
42                console.info("fileioClient: deviceid : " + networkId)
43                console.info("fileioClient: online deviceList id: " + JSON.stringify(deviceList))
44                let want = {
45                    "bundleName": bundleName,
46                    "abilityName": abilityName,
47                    "deviceId": networkId,
48                    "flags": 256
49                }
50                let connect = {
51                    onConnect: function (elementName, remoteProxy) {
52                        console.log('fileioClient: onConnect called, remoteProxy: ' + remoteProxy);
53                        resolve(remoteProxy)
54                    },
55                    onDisconnect: function (elementName) {
56                        console.log("fileioClient: onDisconnect")
57                    },
58                    onFailed: function () {
59                        console.log("fileioClient: onFailed")
60                    }
61                }
62                let connectId = featureAbility.connectAbility(want, connect)
63                console.info("fileioClient: connect ability got id: " + connectId)
64            })
65        })
66
67    }
68}
69