Replies: 3 comments 10 replies
-
Great share! |
Beta Was this translation helpful? Give feedback.
-
hello, im having problem and i dont know how to solve, the error is info [DEBUG-8:18:14 PM.619] I>0>14.208 quickjs- SBEngine current filepath: /app.js im trying to find "setTimeout" but there is no setTimeout in program any idea? |
Beta Was this translation helpful? Give feedback.
-
Great work!! One question, is It possible to uplpad data with app-service every minute, instead button click? Thank you |
Beta Was this translation helpful? Give feedback.
-
Intro
First, let me introduce why we chose this topic.
So, I combined the above two ideas and prepared this workshop. I believe the content will be very rich.
At the end of the beginning, I will introduce today's expected harvest.
Architecture
I redraw the architecture diagram of the Mini Program. Our main purpose of this workshop is to understand this picture.
The complete Mini Program includes Device App, Settings App and Side Service.
Communication Relationship
Let's sort out the communication relationship together. BLE and SettingsStorage are more complex, and we will explain them separately.
BLE
BLE is used for communication between Device App and Side Service. The underlying BLE API is not user-friendly, so we provide the
MessageBuilder
library.The part of
MessageBuilder
can refer to this document, and the todo-list Mini Program example. That's not our focus today.Today, we will introduce ZML. ZML is a more user-friendly Bluetooth communication library. Just last week, ZML supported API_LEVEL 2.0, making it possible to use the ZML communication API on more devices. Now we can use it on GTR4 running Zepp OS v2.
We use
this.request
method to send messages in Device App andonRequest
to receive messages in Side Service.And we use
this.call
method to send messages in Side Service andonCall
to receive messages in Device App.Settings Storage
Communication between Settings App and Side Service relies on Settings Storage.
To facilitate the display of data relationships, two Settings Storage are drawn in the figure, which are actually the same thing.
The API of Settings Storage is very simple, use
setItem
to set data andgetItem
to get the value.So when we use
setItem
to set data in the Settings App, how do Side Service respond to data changes?addListener
API in Side Service to register callback function to listen to data changes in Settings StoragesetItem
to change data in the Settings App and trigger the registered callback functionWhen we use
setItem
to set data in the Side Service, automatically call Settings Appbuild
lifecycle, redraw UI.setItem
to change data in the Side Servicebuild
lifecycle againFull Example: post-health-data
https://github.com/zepp-health/zeppos-samples/tree/main/application/2.0/post-health-data
The logic of the post-health-data Mini Program is very simple. The Device App obtains sleep information and posts data to a Web Server.
The same logic is also implemented in the Settings App.
The server code is in the /Server directory, using Node.js implementation, the database uses MongoDB, a free Altas service.
Although the Mini Program looks very simple, it includes all the communication links mentioned above.
Action 1: Get Sleep Data
In this action, call the Sleep sensor, obtain data, and use the
TEXT
widget for drawing UI.Action 2: Post Data
When we click the post data button, sleep data is sent from the Device App to the Side Service and post to the server.
We use the
this.request
method in the Device App to post messages to the Side Service.Use the
onRequest
method in Side-service to receive information, and then request the web server interface throughFetchAPI
.Action 3: Request Sleep Data From Device App
After clicking the button, the Settings App starts a request to the Device App to obtain sleep data.
How to implement this feature? Settings App cannot communicate directly with Device App. Here we need to use Side Service to connect the two.
First, use
addListener
to listen to Settings Storage in the Side Service, then initiate an action in the Settings App to change the value of Settings Storage, In this way, you can respond to this action in the Side Service.We use
this.call
to communicate with Device App in the callback of the Side ServiceaddListener
function.Action 4: Post Data To Web Service
After clicking the button, the Settings App starts a request to the Side Service to upload sleep data.
The principle is the same as Action 3. The Side Service uses
addListener
to listen to Settings Storage. The value of Settings Storage is modified in the Settings App. To distinguish the behavior, we change the name of the key this time.So far, we have practiced all communication ways. This example is simple enough and I believe it will be understood soon.
Some tips for reporting health data
Beta Was this translation helpful? Give feedback.
All reactions