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