10704ebd2Sopenharmony_ci/* 20704ebd2Sopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd. 30704ebd2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 40704ebd2Sopenharmony_ci * you may not use this file except in compliance with the License. 50704ebd2Sopenharmony_ci * You may obtain a copy of the License at 60704ebd2Sopenharmony_ci * 70704ebd2Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 80704ebd2Sopenharmony_ci * 90704ebd2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 100704ebd2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 110704ebd2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 120704ebd2Sopenharmony_ci * See the License for the specific language governing permissions and 130704ebd2Sopenharmony_ci * limitations under the License. 140704ebd2Sopenharmony_ci */ 150704ebd2Sopenharmony_ci 160704ebd2Sopenharmony_ciimport rpc from '@ohos.rpc'; 170704ebd2Sopenharmony_ciimport process from '@ohos.process'; 180704ebd2Sopenharmony_ci 190704ebd2Sopenharmony_cilet logTag = "RpcClient_TestService: "; 200704ebd2Sopenharmony_cilet CODE_INVOKE = 1; 210704ebd2Sopenharmony_ciimport ApiMessage from '../common/apiMessage.ets'; 220704ebd2Sopenharmony_ciimport ApiResult from '../common/apiResult.ets'; 230704ebd2Sopenharmony_ciimport deviceManager from '@ohos.distributedDeviceManager'; 240704ebd2Sopenharmony_ciimport common from '@ohos.app.ability.common'; 250704ebd2Sopenharmony_ciimport { BusinessError } from '@ohos.base'; 260704ebd2Sopenharmony_ci 270704ebd2Sopenharmony_cilet results; 280704ebd2Sopenharmony_cilet isConnected = false; 290704ebd2Sopenharmony_cilet bundleNameKv = "com.acts.distributekvdisets"; 300704ebd2Sopenharmony_cilet abilityNameKv = "com.acts.distributekvdisets.ServiceAbility"; 310704ebd2Sopenharmony_cilet bundleNameObj = "com.acts.distributedobjectdisets"; 320704ebd2Sopenharmony_cilet abilityNameObj = "com.acts.distributedobjectdisets.ServiceAbility"; 330704ebd2Sopenharmony_ci 340704ebd2Sopenharmony_cilet deviceList: Array<deviceManager.DeviceBasicInfo>; 350704ebd2Sopenharmony_cilet dmInstance : deviceManager.DeviceManager; 360704ebd2Sopenharmony_cilet deviceId : string; 370704ebd2Sopenharmony_ci 380704ebd2Sopenharmony_ciexport default class TestService { 390704ebd2Sopenharmony_ci callback; 400704ebd2Sopenharmony_ci 410704ebd2Sopenharmony_ci onCreate() { 420704ebd2Sopenharmony_ci console.info(logTag + 'AceApplication onCreate'); 430704ebd2Sopenharmony_ci } 440704ebd2Sopenharmony_ci 450704ebd2Sopenharmony_ci onDestroy() { 460704ebd2Sopenharmony_ci console.info(logTag + 'AceApplication onDestroy'); 470704ebd2Sopenharmony_ci } 480704ebd2Sopenharmony_ci 490704ebd2Sopenharmony_ci constructor() { 500704ebd2Sopenharmony_ci 510704ebd2Sopenharmony_ci } 520704ebd2Sopenharmony_ci 530704ebd2Sopenharmony_ci getDeviceList(deviceManager) { 540704ebd2Sopenharmony_ci deviceList = deviceManager.getAvailableDeviceListSync(); 550704ebd2Sopenharmony_ci console.info(logTag + "getDeviceList success, deviceList id: " + JSON.stringify(deviceList)); 560704ebd2Sopenharmony_ci } 570704ebd2Sopenharmony_ci 580704ebd2Sopenharmony_ci toConnectAbility() { 590704ebd2Sopenharmony_ci console.info(logTag + " -----toConnectAbility-----"); 600704ebd2Sopenharmony_ci return new Promise(resolve=>{ 610704ebd2Sopenharmony_ci let self = this; 620704ebd2Sopenharmony_ci console.info(logTag + '-----getDeviceId is begin-----') 630704ebd2Sopenharmony_ci try { 640704ebd2Sopenharmony_ci dmInstance = deviceManager.createDeviceManager(bundleNameKv); 650704ebd2Sopenharmony_ci console.info(logTag + 'get deviceManager is success') 660704ebd2Sopenharmony_ci } catch (error) { 670704ebd2Sopenharmony_ci console.info(logTag + 'get deviceManager is failed' + JSON.stringify(error)) 680704ebd2Sopenharmony_ci } 690704ebd2Sopenharmony_ci deviceList = dmInstance.getAvailableDeviceListSync(); 700704ebd2Sopenharmony_ci deviceId = deviceList[0].networkId; 710704ebd2Sopenharmony_ci console.info(logTag + "deviceid : " + deviceId); 720704ebd2Sopenharmony_ci 730704ebd2Sopenharmony_ci console.info(logTag + '-----connectServiceExtensionAbility is begin-----'); 740704ebd2Sopenharmony_ci let context:common.UIAbilityContext | undefined = AppStorage.get('context') as common.UIAbilityContext 750704ebd2Sopenharmony_ci 760704ebd2Sopenharmony_ci let want = { 770704ebd2Sopenharmony_ci deviceId: deviceId, 780704ebd2Sopenharmony_ci bundleName: bundleNameKv, 790704ebd2Sopenharmony_ci abilityName: abilityNameKv 800704ebd2Sopenharmony_ci }; 810704ebd2Sopenharmony_ci let connect = { 820704ebd2Sopenharmony_ci onConnect: function (elementName, remoteProxy) { 830704ebd2Sopenharmony_ci console.log(logTag + ' ***** onConnect called, remoteProxy: ' + remoteProxy); 840704ebd2Sopenharmony_ci resolve(remoteProxy); 850704ebd2Sopenharmony_ci }, 860704ebd2Sopenharmony_ci onDisconnect: function (elementName) { 870704ebd2Sopenharmony_ci console.log(logTag + " ***** onDisconnect..."); 880704ebd2Sopenharmony_ci }, 890704ebd2Sopenharmony_ci onFailed: function () { 900704ebd2Sopenharmony_ci console.log(logTag + " ***** onFailed..."); 910704ebd2Sopenharmony_ci } 920704ebd2Sopenharmony_ci } 930704ebd2Sopenharmony_ci let connection: number; 940704ebd2Sopenharmony_ci try { 950704ebd2Sopenharmony_ci connection = context.connectServiceExtensionAbility(want, connect); 960704ebd2Sopenharmony_ci console.info(logTag + " ***** connectServiceExtensionAbility success. got id: " + connection); 970704ebd2Sopenharmony_ci } catch (err) { 980704ebd2Sopenharmony_ci console.info(logTag + " ***** connectServiceExtensionAbility failed. got id: " + connection); 990704ebd2Sopenharmony_ci let code = (err as BusinessError).code; 1000704ebd2Sopenharmony_ci let message = (err as BusinessError).message; 1010704ebd2Sopenharmony_ci console.error(logTag + ` ***** connectServiceExtensionAbility failed. err code is ${code}, message is ${message}`); 1020704ebd2Sopenharmony_ci } 1030704ebd2Sopenharmony_ci }) 1040704ebd2Sopenharmony_ci } 1050704ebd2Sopenharmony_ci 1060704ebd2Sopenharmony_ci toConnectObjAbility() { 1070704ebd2Sopenharmony_ci console.info(logTag + " -----toConnectAbility-----"); 1080704ebd2Sopenharmony_ci return new Promise(resolve=>{ 1090704ebd2Sopenharmony_ci let self = this; 1100704ebd2Sopenharmony_ci console.info(logTag + '-----getDeviceId is begin-----') 1110704ebd2Sopenharmony_ci try { 1120704ebd2Sopenharmony_ci dmInstance = deviceManager.createDeviceManager(bundleNameObj); 1130704ebd2Sopenharmony_ci console.info(logTag + 'get deviceManager is success') 1140704ebd2Sopenharmony_ci } catch (error) { 1150704ebd2Sopenharmony_ci console.info(logTag + 'get deviceManager is failed' + JSON.stringify(error)) 1160704ebd2Sopenharmony_ci } 1170704ebd2Sopenharmony_ci deviceList = dmInstance.getAvailableDeviceListSync(); 1180704ebd2Sopenharmony_ci deviceId = deviceList[0].networkId; 1190704ebd2Sopenharmony_ci console.info(logTag + "deviceid : " + deviceId); 1200704ebd2Sopenharmony_ci 1210704ebd2Sopenharmony_ci console.info(logTag + '-----connectServiceExtensionAbility is begin-----'); 1220704ebd2Sopenharmony_ci let context:common.UIAbilityContext | undefined = AppStorage.get('context') as common.UIAbilityContext 1230704ebd2Sopenharmony_ci 1240704ebd2Sopenharmony_ci let want = { 1250704ebd2Sopenharmony_ci deviceId: deviceId, 1260704ebd2Sopenharmony_ci bundleName: bundleNameObj, 1270704ebd2Sopenharmony_ci abilityName: abilityNameObj 1280704ebd2Sopenharmony_ci }; 1290704ebd2Sopenharmony_ci let connect = { 1300704ebd2Sopenharmony_ci onConnect: function (elementName, remoteProxy) { 1310704ebd2Sopenharmony_ci console.log(logTag + ' ***** onConnect called, remoteProxy: ' + remoteProxy); 1320704ebd2Sopenharmony_ci resolve(remoteProxy); 1330704ebd2Sopenharmony_ci }, 1340704ebd2Sopenharmony_ci onDisconnect: function (elementName) { 1350704ebd2Sopenharmony_ci console.log(logTag + " ***** onDisconnect..."); 1360704ebd2Sopenharmony_ci }, 1370704ebd2Sopenharmony_ci onFailed: function () { 1380704ebd2Sopenharmony_ci console.log(logTag + " ***** onFailed..."); 1390704ebd2Sopenharmony_ci } 1400704ebd2Sopenharmony_ci } 1410704ebd2Sopenharmony_ci let connection: number; 1420704ebd2Sopenharmony_ci try { 1430704ebd2Sopenharmony_ci connection = context.connectServiceExtensionAbility(want, connect); 1440704ebd2Sopenharmony_ci console.info(logTag + " ***** connectServiceExtensionAbility success. got id: " + connection); 1450704ebd2Sopenharmony_ci } catch (err) { 1460704ebd2Sopenharmony_ci console.info(logTag + " ***** connectServiceExtensionAbility failed. got id: " + connection); 1470704ebd2Sopenharmony_ci let code = (err as BusinessError).code; 1480704ebd2Sopenharmony_ci let message = (err as BusinessError).message; 1490704ebd2Sopenharmony_ci console.error(logTag + ` ***** connectServiceExtensionAbility failed. err code is ${code}, message is ${message}`); 1500704ebd2Sopenharmony_ci } 1510704ebd2Sopenharmony_ci }) 1520704ebd2Sopenharmony_ci } 1530704ebd2Sopenharmony_ci 1540704ebd2Sopenharmony_ci} 155