Command Messaging
Command messaging sends application control data over an established TiRTC connection. It is suitable for requests such as device status queries, control commands, and command responses.
The command channel is peer-to-peer. After a connection is established, Android, HarmonyOS, iOS, Flutter, and device-side C connection objects can send or receive commands.
Agree on cmdw
cmdw is the command word agreed by both sides. TiRTC forwards the command word and payload without interpreting business semantics.
The examples below use:
cmdw | Meaning |
|---|---|
0x10000 | Request device status |
0x10002 | Return device status result |
Send a Command
dart
import 'dart:convert';
import 'dart:typed_data';
final payload = Uint8List.fromList(utf8.encode('status?'));
final code = conn.sendCommand(commandId: 0x10000, data: payload);kotlin
val payload = "status?".encodeToByteArray()
val code = conn.sendCommand(command = 0x10000L, data = payload)ts
const payload = new Uint8Array([0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x3f]);
const code = conn.sendCommand({ commandId: 0x10000, data: payload });swift
let payload = Data("status?".utf8)
let code = conn.send(commandId: 0x10000, data: payload)Receive and Reply
dart
import 'dart:convert';
import 'dart:typed_data';
conn.onCommand = (int commandId, Uint8List data) {
if (commandId != 0x10000 || utf8.decode(data) != 'status?') return;
conn.sendCommand(
commandId: 0x10002,
data: Uint8List.fromList(utf8.encode('device-ok')),
);
};ts
function asciiString(data: Uint8Array): string {
let result = '';
for (let index = 0; index < data.byteLength; index += 1) {
result += String.fromCharCode(data[index]);
}
return result;
}
conn.onCommand = (commandId: number, data: Uint8Array): void => {
if (commandId !== 0x10000 || asciiString(data) !== 'status?') {
return;
}
conn.sendCommand({
commandId: 0x10002,
data: new Uint8Array([0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x2d, 0x6f, 0x6b]),
});
};