DEV Community

HarmonyOS
HarmonyOS

Posted on

Sports Watch btproxy Capability Development Guide

Read the original article:Sports Watch btproxy Capability Development Guide

Question

How can developers use the btproxy fetch() capability on Sports Watch devices to obtain data over the network?

Short Answer

The fetch(Object) method allows applications to request and obtain data over the network using either the GET or POST method. It supports different data formats (string or Object), handles headers, and provides callback functions (success, fail, complete) for request lifecycle management.

@system.fetch (data request)

The first batch of APIs of this module is supported since API version 10.

Module Import

fetch(Object): void

Obtaining data over the network

parameter

parameter name type mandatory explain
url string YES Resource address
data string | Object NO Request parameter. The value can be a character string or a JSON object. For details, see the relationship between data and Content-Type.
header Object NO Set the request header.
method string NO The default request method is GET, and the options are POST.
responseType string NO By default, the return type is determined based on Content-Type in the header returned by the server. The text and JSON formats are supported. For details, see the return value of success.
success Function NO Callback function that is successfully invoked.
fail Function NO Callback function that fails to be invoked.
complete Function NO Callback function that ends interface invoking.

Table 1 Relationship between data and Content-Type

data Content-Type explain
string Not set The default value of Content-Type is text/plain, and the value of data is used as the request body.
data Content-Type explain
string Any type The data value is used as the request body.
Object Not set The default value of Content-Type is application/x-www-form-urlencoded. The data is encoded based on the resource address rule and combined as the request body.
Object application/x-www-form-urlencoded Data is encoded based on the resource address rule and combined as the request body.

Return value of success:

parameter name type explain
code number Indicates the status code of the server.
data string | Object The returned data type is determined by responseType. For details, see the data relationship between responseType and success.
headers Object Indicates all headers of the server response.

Table 2 Relationship between responseType and data in success

responseType data explain
string If the value of type in the header returned by the server is text/* or application/json, the value is text content.
text string Returns the text content.
json Object Returns an object in JSON format.

Examples:

Error code:

Error code Meaning Recommended Actions
0 connection timeout Re-initiate the request after 1 to 3 seconds
-1 WebClient common error Check whether the request URL is correct.
-2 receive timeout Re-initiate the request after 1 to 3 seconds
-3 out of memory Contact the watch system developer to check.
-4 Failed to create the socket. Contact the watch system developer to check.
-5 WebClient No HeaderBuf Check whether the requested data is too large.
-6 connect timeout Re-initiate the request after 1 to 3 seconds
-7 Disconnected Check the Bluetooth connection status. If the Bluetooth connection is disconnected, reconnect the Bluetooth connection.
-9 Network request in progress Re-initiate the request after 1 to 3 seconds

Note: The size of a single request cannot exceed 6 KB.

Packing and putting on shelves:

If the application that is not compatible with GT3 uses the btproxy, you need to use API10 to package and release the IDE. The method of obtaining the IDE is as follows. The third party needs to sign a related agreement to obtain the IDE that contains API10.

Applicable Scenarios

  • When an application on a Sports Watch needs to retrieve data from a remote server via Bluetooth proxy (btproxy).
  • For sending network requests (GET/POST) to APIs while ensuring compatibility with API version 10 or higher.
  • Suitable for lightweight network communications where request payloads are below 6 KB.
  • Ideal for Bluetooth-connected environments where the watch depends on the phone’s network connection for data transfer.

Written by Sinan Yilmaz

Top comments (0)