Reporting Device Data

API Function

This API is used by a directly connected device to report data to the platform or used by a gateway to report data received from an indirectly connected device to the platform.

deviceId, requestId, and serviceId of the API Reporting Device Data are assembled by the SDK as the header of the message, and serviceProperties is assembled as the body of the message. The message is assembled in JSON format.

API Description

1
public static boolean dataReport(int cookie, String requstId, String deviceId, String serviceId, String serviceProperties);

Class

DataTransService

Parameter Description

Parameter

Mandatory or Optional

Type

Description

cookie

Optional

int

The value ranges from 1 to 65535.

requstId

Mandatory

String

Identifier of a request. This parameter is used to match the service command delivered by the platform, which can be obtained from the broadcast described in Receiving a Command.

  • Active data reporting: The value of this parameter is NULL.
  • Command result reporting: The value of this parameter is the request ID of the command which the reported data matches.

deviceId

Mandatory

String

Identifier of a device.

serviceId

Mandatory

String

Identifier of a service.

serviceProperties

Mandatory

String

Service properties.

Return Value

Return Value

Description

true

Success

false

Failure

Return values only show the API call result. For example, the return value true indicates that the API is called successfully but does not indicate that the service data is reported successfully. The service data is reported successfully only after the TOPIC_DATA_REPORT_RSP broadcast is received.

Output

Broadcast Name

Broadcast Parameter

Member

Description

TOPIC_DATA_REPORT_RSP

IotaMessage

(Obtained by using intent.getSerializableExtra(DataTransService.DATATRANS_BROADCAST_IE_IOTAMSG))

DATATRANS_IE_RESULT

Data reporting result.

DATATRANS_IE_COOKIE

The value ranges from 1 to 65535.

Example

Use JSON components to assemble service properties (serviceProperties) based on the profile format.

1
DataTransService.dataReport(1211, NULL, "xxxx_xxxx_xxxx_xxxx", "DoorWindow", "{\"status\":\"OPEN\"}");

The device waits for the data reporting result.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// Register a broadcast receiver to process the device service data reporting result.
BroadcastReceiver mReportDataRsp;
mReportDataRsp = new BroadcastReceiver() {
    @Override 
    public void onReceive(Context context, Intent intent) {
        //Do Something 
        IotaMessage iotaMsg = (IotaMessage)intent.getSerializableExtra(DataTransService. DATATRANS_BROADCAST_IE_IOTAMSG);
        int cookie = iotaMsg.getUint(DataTransService.DATATRANS_IE_COOKIE, 0);
        int ret = iotaMsg.getUint(DataTransService.DATATRANS_IE_RESULT, 0);
        return;
    }
};
mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
IntentFilter filterReportData 
= new IntentFilter(DataTransService.TOPIC_DATA_REPORT_RSP);
mLocalBroadcastManager.registerReceiver(mReportDataRsp, filterReportData);