• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2024 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
19
20import ApiMessage from '../common/apiMessage.ets';
21import ApiResult from '../common/apiResult.ets';
22import deviceManager from '@ohos.distributedDeviceManager';
23import common from '@ohos.app.ability.common';
24import { BusinessError } from '@ohos.base';
25import promptAction from '@ohos.promptAction';
26
27let logTag = "RpcClient_TestService:  ";
28let CODE_INVOKE = 1;
29
30let results;
31let isConnected = false;
32let bundleNameKv = "com.acts.distributekvdisets";
33let abilityNameKv = "com.acts.distributekvdisets.ServiceAbility";
34let bundleNameObj = "com.acts.distributedobjectdisets";
35let abilityNameObj = "com.acts.distributedobjectdisets.ServiceAbility";
36
37let deviceList: Array<deviceManager.DeviceBasicInfo>;
38let dmInstance : deviceManager.DeviceManager;
39let deviceId : string;
40let tempData = undefined;
41
42export default class TestService {
43    callback;
44
45    onCreate() {
46        console.info(logTag + 'AceApplication onCreate');
47    }
48
49    onDestroy() {
50        console.info(logTag + 'AceApplication onDestroy');
51    }
52
53    constructor() {
54
55    }
56
57    getDeviceList(deviceManager) {
58        deviceList = deviceManager.getAvailableDeviceListSync();
59        console.info(logTag + "getDeviceList success, deviceList id: " + JSON.stringify(deviceList));
60    }
61
62    toConnectAbility() {
63        console.info(logTag + " -----toConnectAbility-----");
64        return new Promise(resolve=>{
65            let self = this;
66            console.info(logTag + '-----getDeviceId is begin-----')
67            try {
68                dmInstance = deviceManager.createDeviceManager(bundleNameKv);
69                console.info(logTag + 'get deviceManager is success')
70            } catch (error) {
71                console.info(logTag + 'get deviceManager is failed' + JSON.stringify(error))
72            }
73            deviceList = dmInstance.getAvailableDeviceListSync();
74            deviceId = deviceList[0].networkId;
75            console.info(logTag + "deviceid : " + deviceId);
76
77            console.info(logTag + '-----connectServiceExtensionAbility is begin-----');
78            let context:common.UIAbilityContext | undefined = AppStorage.get('context') as common.UIAbilityContext
79
80            let want = {
81                deviceId: deviceId,
82                bundleName: bundleNameKv,
83                abilityName: abilityNameKv
84            };
85            let connect = {
86                onConnect: function (elementName, remoteProxy) {
87                    console.log(logTag + ' ***** onConnect called, remoteProxy: ' + remoteProxy);
88                    resolve(remoteProxy);
89                },
90                onDisconnect: function (elementName) {
91                    console.log(logTag + " ***** onDisconnect...");
92                },
93                onFailed: function () {
94                    console.log(logTag + " ***** onFailed...");
95                }
96            }
97            let connection: number;
98            try {
99              connection = context.connectServiceExtensionAbility(want, connect);
100              console.info(logTag + " ***** connectServiceExtensionAbility success. got id: " + connection);
101            } catch (err) {
102              console.info(logTag + "  ***** connectServiceExtensionAbility failed. got id: " + connection);
103              let code = (err as BusinessError).code;
104              let message = (err as BusinessError).message;
105              console.error(logTag + ` ***** connectServiceExtensionAbility failed. err code is ${code}, message is ${message}`);
106            }
107        })
108    }
109
110    toConnectObjAbility() {
111        console.info(logTag + " -----toConnectAbility-----");
112        return new Promise(resolve=>{
113            let self = this;
114            console.info(logTag + '-----getDeviceId is begin-----')
115            try {
116                dmInstance = deviceManager.createDeviceManager(bundleNameObj);
117                console.info(logTag + 'get deviceManager is success')
118            } catch (error) {
119                console.info(logTag + 'get deviceManager is failed' + JSON.stringify(error))
120            }
121            deviceList = dmInstance.getAvailableDeviceListSync();
122            deviceId = deviceList[0].networkId;
123            console.info(logTag + "deviceid : " + deviceId);
124
125            console.info(logTag + '-----connectServiceExtensionAbility is begin-----');
126            let context:common.UIAbilityContext | undefined = AppStorage.get('context') as common.UIAbilityContext
127
128            let want = {
129                deviceId: deviceId,
130                bundleName: bundleNameObj,
131                abilityName: abilityNameObj
132            };
133            let connect = {
134                onConnect: function (elementName, remoteProxy) {
135                    console.log(logTag + ' ***** onConnect called, remoteProxy: ' + remoteProxy);
136                    resolve(remoteProxy);
137                },
138                onDisconnect: function (elementName) {
139                    console.log(logTag + " ***** onDisconnect...");
140                },
141                onFailed: function () {
142                    console.log(logTag + " ***** onFailed...");
143                }
144            }
145            let connection: number;
146            try {
147              connection = context.connectServiceExtensionAbility(want, connect);
148              console.info(logTag + " ***** connectServiceExtensionAbility success. got id: " + connection);
149            } catch (err) {
150              console.info(logTag + "  ***** connectServiceExtensionAbility failed. got id: " + connection);
151              let code = (err as BusinessError).code;
152              let message = (err as BusinessError).message;
153              console.error(logTag + ` ***** connectServiceExtensionAbility failed. err code is ${code}, message is ${message}`);
154            }
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: deviceManager.DeviceBasicInfo[] = 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
242}
243