Author: Li Gang, Huawei distributed hardware technology expert, sun Binxin, Huawei application framework technology expert
HarmonyOS is a new distributed operating system, which provides developers with distributed technologies such as meta capability framework, event notification and distributed hardware, so as to develop distributed applications for the whole scene. With the continuous growth of HarmonyOS application ecology, developers are unable to use the original distributed technology to realize some innovation scenarios, because it is difficult to develop, which directly affects the development efficiency.
"Innovation promotes development". We need to constantly explore and innovate distributed technology in order to make developers better adapt to the development of application ecology. In this issue, we will show you the efficient development experience brought by technological innovation to developers through two multi terminal collaborative innovation technologies and innovation cases.
1, Technological innovation
Multi terminal collaboration is the most common technical scheme in distributed applications. It realizes complete business functions by running FA/PA on multi terminal devices simultaneously or alternately. Next, we will introduce two technological innovations: multi terminal collaborative cross device communication and external device hardware resource access.
1. Cross device communication
Cross device communication is the basis of multi device interaction. As shown in Figure 1, in the previous implementation of cross device communication, ServiceAbility, as the medium of message transfer, provides the ability to run tasks in the background and unified data access abstraction, so as to realize the interaction with two pageabilities of different devices.
Developers should not only complete the business logic development of business logic layer devices, but also complete the implementation of complex functions in communication layer, including cross device connection management, data forwarding, life cycle binding and perception. The development is time-consuming and laborious.
Figure 1 implementation of serviceability
In order to help developers realize cross device communication more conveniently and efficiently, we have changed the original implementation method and provided the latest startassisability interface to directly pull up the PageAbility of establishing cooperative peer devices.
As shown in Figure 2, by using the startassisability interface, the cross device connection management, data forwarding, life cycle binding and perception of the communication layer are realized by the system. Developers only need to pay attention to the upper layer business logic to realize simple and efficient development.
Figure 2 implementation of startassisability
2. Peripheral hardware access
Peripheral hardware access is the key to multi device cooperation. As shown in Figure 1, in the original external device hardware resource access process, the hardware resources of the two devices are integrated through the distributed device virtualization capability provided by HarmonyOS to form a virtual terminal to realize the access of hardware resources between the two devices. However, with the deepening of the intelligent life of the whole scene, the one-to-one device hardware resource access method can no longer meet the business needs of some scenarios today.
Figure 3 one to one device hardware resource access
In order to break the "one-to-one" barrier, we propose hardware resource pooling technology. As shown in Figure 3, by enhancing the virtualization capability of the original distributed devices, various hardware resources of multiple devices, such as screens, cameras, loudspeakers, keyboards, sensors and memory, are abstracted and transformed into a unified resource pool in the "super terminal", and support the on-demand allocation and reassembly of hardware resources, It really realizes the call of global hardware resources of super terminal.
Figure 4 hardware resource pooling
2, Innovation case
What different development experiences can the above two distributed technological innovations bring to developers? Next, we will answer for you through two cases.
1. Component collaboration cases
As shown in the figure below, mobile phones and tablets use their respective advantages to cooperate to complete different parts of the same magazine design task. How to efficiently realize the interface collaboration between mobile phone and tablet in this distributed scenario?
We use the latest startassisability interface.
- Firstly, we abstract the connection between the two devices as AssistChannel object, and realize connection success / disconnection / failure by listening to the events of the object;
- Then, the cooperative initiator specifies the corresponding Wan and channel startup parameters through the startassisability interface to initiate the connection;
- Finally, the cooperative response end responds to the connection event of the channel. After the connection is successful, it will call back an Agent object, which provides an interface for sending and receiving messages and listening to the life cycle of the opposite end, which is used to receive and send messages and sense the life cycle changes of the opposite end.
The following are the specific implementation steps and sample code.
(1) The cooperative initiator calls the startassisability interface to make a connection request. The example code is as follows:
const channel = new AssistChannel() channel.on('assistConnect', (agent) => { // Callback after successful listening connection }) channel.on('assistDisconnect', (agent) => { // Callback after listening connection is disconnected }) channel.on('assistFail', (agent) => { // Callback after listening connection failure }) // assistWant specifies the name of the Ability to be started and the NetworkId of the device context.startAssistAbility(assistWant, channel)
(2) The collaboration response end responds to the collaboration event of the collaboration initiator channel. The example code is as follows:
const channel = new AssistChannel() channel.on('assistConnect', (agent) => { // Callback after successful listening connection }) channel.on('assistDisconnect', (agent) => { // Callback after listening connection is disconnected }) channel.on('assistFail', (agent) => { // Callback after listening connection failure }) context.setAssistChannel(channel)
(3) Send and receive messages through the Agent interface. Both the cooperative initiator and the cooperative response end can set the message receiver and send messages to the opposite end through this interface. The example code is as follows:
// Register message receiver agent.on('message', (event)=>{ //Process received events }) //Innereevent can be sent to the opposite end agent.postMessage(event);
(4) The peer life cycle is perceived through the Agent interface. The life cycle of the collaboration response end is bound to the collaboration initiator. When the collaboration initiator exits, the collaboration response end will also exit. The example code is as follows:
//Register life cycle monitoring to monitor the changes of the opposite end life cycle agent.on('stateChange', (event) => { if (event === LifecycleEvent.ON_INACTIVE) { // Processing messages } // TODO other lifecycle processing })
2. Multi camera case
As shown in the figure below, the perspective of different stands in the surrounding environment is presented through the multi stand mode. How to realize the function of accessing peripheral Camera hardware in this distributed scenario?
We use hardware resource pooling technology.
- Firstly, we obtain the list of peripheral Camera hardware in the hardware resource pool through the getCapabilities interface, which contains the peripheral Camera ID, which can call the OpenCamera interface to obtain Camera frame information;
- Then, monitor the plug-in events of peripheral hardware through the distributeHardwareMgr interface;
- Finally, the Camera hardware is enabled through the enableCapability interface.
The following are the specific implementation steps and sample code.
(1) When the user clicks the peripheral button, query the available distributed camera information. The example code is as follows:
// Create a disHardwareMgr instance var disHardwareMgr; distributedHardware.createDHManager(app.getInfo().appID, (err, data) => { if (err) { console.info(TAG + "createDHManager err:" + JSON.stringify(err)); return; } disHardwareMgr = data; }); let filter = { deviceId: "", deviceType: CAMERA }; // Query existing available peripheral information let array = disHardwareMgr.getDHCapabilities(filter); console.info("getCapabilities result:" + JSON.stringify(array)); // Monitor the access and removal events of available peripherals in the super terminal disHardwareMgr.registerListener((data) => { console.info("registerHardwareListener on:" + JSON.stringify(data)); this.capEnable = data.cap; });
(2) When the user clicks to connect the device, enable the peripheral Camera hardware. At this time, the picture of peripheral stand Camera appears. The example code is as follows:
// After finding the specified peripheral, enable the hardware driver of the peripheral and add the external device hardware as the local hardware var cameraId; let deviceId = this.capEnable.deviceId; let dhId = this.capEnable.dhId; let useType = 0; disHardwareMgr.enable(deviceId, dhId, useType, (data) => { cameraId = data; }); // Get the distributed camera from the camera service and open the camera preview CameraManager.openCamera(cameraId); CameraManager.startPreview(cameraId);
The above is the whole content of this issue. This article is just the tip of the iceberg of HamronyOS distributed innovation technology. We look forward to the majority of developers actively joining us to witness the infinite possibility of the whole scene intelligent ecology.
Scan code to add Developer Assistant wechat
Get more information about HarmonyOS development resources and developer activities