• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022-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 rpc from '@ohos.rpc';
17import process from '@ohos.process';
18
19import ApiMessage from '../common/apiMessage.js';
20import ApiResult from '../common/apiResult.js';
21import deviceManager from '@ohos.distributedDeviceManager';
22import featureAbility from '@ohos.ability.featureAbility';
23import promptAction from '@ohos.promptAction';
24
25let logTag = "RpcClient_TestService:  ";
26let CODE_INVOKE = 1;
27
28let results;
29let isConnected = false;
30let bundleName = "com.acts.distributekvdisjs";
31let abilityName = "com.acts.distributekvdisjs.ServiceAbility";
32let bundleNameObject = "com.acts.distributeobjectdisjs";
33let abilityNameObject = "com.acts.distributeobjectdisjs.ServiceAbility";
34
35let deviceList;
36let tempData = undefined;
37
38export default class TestService {
39    callback;
40
41    onCreate() {
42        console.info(logTag + 'AceApplication onCreate');
43    }
44
45    onDestroy() {
46        console.info(logTag + 'AceApplication onDestroy');
47    }
48
49    constructor() {
50
51    }
52
53    getDeviceList(deviceManager) {
54        deviceList = deviceManager.getAvailableDeviceListSync();
55        console.info(logTag + "getDeviceList success, deviceList id: " + JSON.stringify(deviceList));
56    }
57
58    toConnectAbility() {
59        console.info(logTag + " toConnectAbility");
60        return new Promise(resolve=>{
61            let self = this;
62            let dmInstance = deviceManager.createDeviceManager('com.acts.distributekvdisjs');
63            self.getDeviceList(dmInstance);
64            console.info("got deviceManager: " + dmInstance)
65            let deviceId = deviceList[0].networkId;
66            console.info(logTag + "deviceid : " + deviceId);
67            console.info(logTag + "online deviceList id: " + JSON.stringify(deviceList));
68            let want = {
69                "bundleName": bundleName,
70                "abilityName": abilityName,
71                "deviceId": deviceId,
72                "flags": 256
73            }
74            let connect = {
75                onConnect: function (elementName, remoteProxy) {
76                    console.log(logTag + 'onConnect called, remoteProxy: ' + remoteProxy);
77                    resolve(remoteProxy);
78                },
79                onDisconnect: function (elementName) {
80                    console.log(logTag + "onDisconnect");
81                },
82                onFailed: function () {
83                    console.log(logTag + "onFailed");
84                }
85            }
86            let connectId = featureAbility.connectAbility(want, connect);
87            console.info(logTag + "connect ability got id: " + connectId);
88
89        })
90    }
91
92    toConnectRdbAbility() {
93        console.info(logTag + " toConnectRdbAbility");
94        return new Promise(resolve=>{
95            let self = this;
96            let dmInstance = deviceManager.createDeviceManager('com.acts.distributerdbdisjs');
97            self.getDeviceList(dmInstance);
98            console.info(logTag + "got deviceManager: " + dmInstance);
99            let deviceId = deviceList[0].networkId;
100            console.info(logTag + "deviceid : " + deviceId);
101            console.info(logTag + "online deviceList id: " + JSON.stringify(deviceList));
102            let want = {
103                "bundleName": "com.acts.distributerdbdisjs",
104                "abilityName": "com.acts.distributerdbdisjs.ServiceAbility",
105                "deviceId": deviceId,
106                "flags": 256
107            }
108            let connect = {
109                onConnect: function (elementName, remoteProxy) {
110                    console.log(logTag + 'onConnect called, remoteProxy: ' + remoteProxy);
111                    resolve(remoteProxy);
112                },
113                onDisconnect: function (elementName) {
114                    console.log(logTag + "onDisconnect");
115                },
116                onFailed: function () {
117                    console.log(logTag + "onFailed");
118                }
119            }
120            let connectId = featureAbility.connectAbility(want, connect);
121            console.info(logTag + "connect ability got id: " + connectId);
122        })
123    }
124
125    toConnectObjectAbility() {
126        console.info(logTag + " toConnectObjectAbility");
127        return new Promise(resolve=>{
128            let self = this;
129            let dmInstance = deviceManager.createDeviceManager('com.acts.distributeobjectdisjs');
130            self.getDeviceList(dmInstance);
131            console.info(logTag + "got deviceManager: " + dmInstance);
132            let deviceId = deviceList[0].networkId;
133            console.info(logTag + "deviceid : " + deviceId);
134            console.info(logTag + "online deviceList id: " + JSON.stringify(deviceList));
135            let want = {
136                "bundleName": bundleNameObject,
137                "abilityName": abilityNameObject,
138                "deviceId": deviceId,
139                "flags": 256
140            }
141            let connect = {
142                onConnect: function (elementName, remoteProxy) {
143                    console.log(logTag + 'onConnect called, remoteProxy: ' + remoteProxy);
144                    resolve(remoteProxy);
145                },
146                onDisconnect: function (elementName) {
147                    console.log(logTag + "onDisconnect");
148                },
149                onFailed: function () {
150                    console.log(logTag + "onFailed");
151                }
152            }
153            let connectId = featureAbility.connectAbility(want, connect);
154            console.info(logTag + "connect ability got id: " + connectId);
155        })
156    }
157
158    startDiscovering(bundleName) {
159        let discoverParam = {
160           'discoverTargetType': 1
161        };
162        let filterOptions = {
163            'availableStatus': 0
164        };
165
166        try {
167           let dmInstance = deviceManager.createDeviceManager(bundleName);
168           console.info(logTag + 'startDiscovering  get deviceManager is success');
169           dmInstance.on('discoverSuccess', (data) => {
170             console.info(logTag + 'startDiscovering success: ' + JSON.stringify(data));
171             promptAction.showToast({
172                message: `discoverSuccess:  ${JSON.stringify(data.device.deviceName)}`,
173                duration: 1000
174             });
175            if (tempData === undefined) {
176                tempData = data;
177                console.info(logTag + 'tempData is: ' + JSON.stringify(tempData));
178            }
179           });
180           dmInstance.on('discoverFailure', (data) => {
181            console.info(logTag + 'startDiscovering failed into discoverFailure: ' + JSON.stringify(data));
182           });
183           //设备发现时 进入discoverSuccess回调
184           dmInstance.startDiscovering(discoverParam, filterOptions);
185        } catch (error) {
186            console.error(logTag + 'startDiscovering error errCode: ' + error.code + 'errMessage: ' + error.message);
187        }
188    }
189
190    stopDiscovering(bundleName) {
191        try {
192            let dmInstance = deviceManager.createDeviceManager(bundleName);
193            console.info(logTag + 'stopDiscovering  get deviceManager is success');
194            dmInstance.stopDiscovering();
195        } catch (error) {
196            console.error(logTag + 'stopDiscovering error errCode: ' + error.code + 'errMessage: ' + error.message);
197        }
198    }
199
200    //PIN码bind
201    bindStub(bundleName) {
202        let deviceId = undefined;
203        try {
204            let dmInstance = deviceManager.createDeviceManager(bundleName);
205            console.info(logTag + 'bindStub  get deviceManager is success');
206            console.info(logTag + 'tempData is: ' + JSON.stringify(tempData));
207            deviceId = tempData.device.deviceId;
208            console.info(logTag + 'bindStub  get deviceId is: ' + deviceId);
209            let bindParam = {
210                'bindType': 1, //无账号PIN码bind
211                'targetPkgName': bundleName, //远端应用包名
212                'appName': bundleName,
213            };
214            dmInstance.bindTarget(deviceId, bindParam, (err, data) => {
215                if (err) {
216                   console.error(logTag + 'bindTarget error errCode: ' + error.code + 'errMessage: ' + error.message);
217                   return;
218                }
219                console.info(logTag + 'bindTarget  result is: ' + JSON.stringify(tempData));
220            });
221        } catch (error) {
222            console.error(logTag + 'bindStub error errCode: ' + error.code + 'errMessage: ' + error.message);
223        }
224    }
225
226    unbindStub(bundleName) {
227        try {
228            let dmInstance = deviceManager.createDeviceManager(bundleName);
229            console.info(logTag + 'unbindStub  get deviceManager is success');
230            let deviceInfoList = dmInstance.getAvailableDeviceListSync();
231            console.info(logTag + 'unbindStub  deviceInfoList.length: ' + deviceInfoList.length);
232            for (let i = 0; i < deviceInfoList.length; i++) {
233                dmInstance.unbindTarget(deviceInfoList[i].deviceId);
234            }
235        } catch (error) {
236            console.error(logTag + 'unbindStub error errCode: ' + error.code + 'errMessage: ' + error.message);
237        }
238    }
239
240}
241