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 19let logTag = "RpcClient_TestService: "; 20let CODE_INVOKE = 1; 21import ApiMessage from '../common/apiMessage.js'; 22import ApiResult from '../common/apiResult.js'; 23import deviceManager from '@ohos.distributedHardware.deviceManager'; 24import featureAbility from '@ohos.ability.featureAbility'; 25 26let results; 27let isConnected = false; 28let bundleName = "com.ohos.distributekvdisjs"; 29let abilityName = "com.ohos.distributekvdisjs.ServiceAbility"; 30let deviceList; 31 32export default class TestService { 33 callback; 34 35 onCreate() { 36 console.info(logTag + 'AceApplication onCreate'); 37 } 38 39 onDestroy() { 40 console.info(logTag + 'AceApplication onDestroy'); 41 } 42 43 constructor() { 44 45 } 46 47 async invoke(gIRemoteObject,message) { 48 var messageParcel = rpc.MessageParcel.create(); 49 console.log(logTag + "create object successfully."); 50 var messageParcelreply = rpc.MessageParcel.create(); 51 var option = new rpc.MessageOption(); 52 var writeResult = messageParcel.writeSequenceable(message); 53 gIRemoteObject.sendRequest(CODE_INVOKE, messageParcel, messageParcelreply, option).then(function (result) { 54 if (result.errCode === 0) { 55 console.log(logTag + "sendRequest got result"); 56 var ret = new ApiMessage(null, null, null, null, null, null,null); 57 var dataReply = result.reply.readSequenceable(ret); 58 console.log(logTag + "run readSequenceable success, result is" + dataReply); 59 results = JSON.parse(ret._apiResult); 60 console.log(logTag + " read success, results is" + results._result); 61 return results._result; 62 } else { 63 console.log(logTag + "sendRequest failed, errCode: " + errCode); 64 } 65 }).catch(function (e) { 66 console.log(logTag + " sendRequest got exception: " + e.message); 67 }); 68 } 69 70 getDeviceList(deviceManager) { 71 deviceList = deviceManager.getTrustedDeviceListSync(); 72 console.info(logTag + "getDeviceList success, deviceList id: " + JSON.stringify(deviceList)); 73 } 74 75 toConnectAbility() { 76 console.info(logTag + " toConnectAbility"); 77 return new Promise(resolve=>{ 78 let self = this; 79 deviceManager.createDeviceManager('com.ohos.distributekvdisjs', (error, deviceManager) => { 80 self.getDeviceList(deviceManager); 81 console.info(logTag + "got deviceManager: " + deviceManager); 82 let deviceId = deviceList[0].deviceId; 83 console.info(logTag + "deviceid : " + deviceId); 84 console.info(logTag + "online deviceList id: " + JSON.stringify(deviceList)); 85 let want = { 86 "bundleName": bundleName, 87 "abilityName": abilityName, 88 "deviceId": deviceId, 89 "flags": 256 90 } 91 let connect = { 92 onConnect: function (elementName, remoteProxy) { 93 console.log(logTag + 'onConnect called, remoteProxy: ' + remoteProxy); 94 resolve(remoteProxy); 95 }, 96 onDisconnect: function (elementName) { 97 console.log(logTag + "onDisconnect"); 98 }, 99 onFailed: function () { 100 console.log(logTag + "onFailed"); 101 } 102 } 103 let connectId = featureAbility.connectAbility(want, connect); 104 console.info(logTag + "connect ability got id: " + connectId); 105 }) 106 }) 107 } 108 109 toConnectRdbAbility() { 110 console.info(logTag + " toConnectRdbAbility"); 111 return new Promise(resolve=>{ 112 let self = this; 113 deviceManager.createDeviceManager('distributerdbdisjs', (error, deviceManager) => { 114 self.getDeviceList(deviceManager); 115 console.info(logTag + "got deviceManager: " + deviceManager); 116 let deviceId = deviceList[0].deviceId; 117 console.info(logTag + "deviceid : " + deviceId); 118 console.info(logTag + "online deviceList id: " + JSON.stringify(deviceList)); 119 let want = { 120 "bundleName": "com.ohos.distributerdbdisjs", 121 "abilityName": "com.ohos.distributerdbdisjs.ServiceAbility", 122 "deviceId": deviceId, 123 "flags": 256 124 } 125 let connect = { 126 onConnect: function (elementName, remoteProxy) { 127 console.log(logTag + 'onConnect called, remoteProxy: ' + remoteProxy); 128 resolve(remoteProxy); 129 }, 130 onDisconnect: function (elementName) { 131 console.log(logTag + "onDisconnect"); 132 }, 133 onFailed: function () { 134 console.log(logTag + "onFailed"); 135 } 136 } 137 let connectId = featureAbility.connectAbility(want, connect); 138 console.info(logTag + "connect ability got id: " + connectId); 139 }) 140 }) 141 } 142} 143