1/* 2 * Copyright (C) 2022 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 deviceManager from '@ohos.distributedHardware.deviceManager'; 18import featureAbility from '@ohos.ability.featureAbility'; 19 20 21var results; 22var bundleName = "com.ohos.rpctest"; 23var abilityName = "com.ohos.rpctest.ServiceAbility"; 24var deviceList; 25 26export default class TestService { 27 constructor() { 28 29 } 30 31 getDeviceList(deviceManager) { 32 deviceList = deviceManager.getTrustedDeviceListSync(); 33 console.info("getDeviceList success, deviceList id: " + JSON.stringify(deviceList)) 34 } 35 36 toConnectAbility() { 37 console.info("RpcClient: toConnectAbility") 38 return new Promise(resolve=>{ 39 let self = this; 40 deviceManager.createDeviceManager('ohos.rpc.test', (error, deviceManager) => { 41 self.getDeviceList(deviceManager); 42 console.info("RpcClient: got deviceManager: " + deviceManager) 43 let networkId = deviceList[0].networkId 44 console.info("RpcClient: deviceid : " + networkId) 45 console.info("RpcClient: online deviceList id: " + JSON.stringify(deviceList)) 46 let want = { 47 "bundleName": bundleName, 48 "abilityName": abilityName, 49 "deviceId": networkId, 50 "flags": 256 51 } 52 let connect = { 53 onConnect: function (elementName, remoteProxy) { 54 console.log('RpcClient: onConnect called, remoteProxy: ' + remoteProxy); 55 resolve(remoteProxy) 56 }, 57 onDisconnect: function (elementName) { 58 console.log("RpcClient: onDisconnect") 59 }, 60 onFailed: function () { 61 console.log("RpcClient: onFailed") 62 } 63 } 64 let connectId = featureAbility.connectAbility(want, connect) 65 console.info("RpcClient: connect ability got id: " + connectId) 66 }) 67 }) 68 69 } 70} 71