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