14. Custom Commands
Facilitates customer feature extensions
Important
- Length limit for custom commands
public void checkRCSPProtocolMTU(BluetoothDevice device) { //Maximum sending MTU int protocolMtu = DeviceStatusManager.getInstance().getMaxCommunicationMtu(device); //Size of custom command = Maximum sending MTU - Protocol packet length int customDataLimit = protocolMtu - 23; //Suggest negotiating well with firmware, not recommended to send maximum data }
Suggest negotiating maximum sending value with firmware, do not exceed protocol MTU
14.1. Sending Custom Commands
public void sendCustomCommand(byte[] data) {
//WatchManager is a subclass of WatchOpImpl, must configure sdk in 1.3
WatchManager manager = WatchManager.getInstance();
//build the custom command
CommandBase customCmd = CommandBuilder.buildCustomCmd(data); //carries custom data
final boolean isNoResponse = false; //Set whether custom command does not need reply, not recommended to use command without reply
if (isNoResponse) {
customCmd = CommandBuilder.buildCustomCmdWithoutResponse(data);//Setting does not require reply
}
//Send custom command and waiting for the result callback
manager.sendRcspCommand(manager.getTargetDevice(), customCmd, new RcspCommandCallback<CustomCmd>() {
@Override
public void onCommandResponse(BluetoothDevice device, CustomCmd cmd) {
if (cmd.getStatus() != StateCode.STATUS_SUCCESS) {
onErrCode(device, RcspErrorCode.buildJsonError(cmd.getId(), RcspErrorCode.ERR_RESPONSE_BAD_STATUS, cmd.getStatus(), null));
return;
}
boolean hasResponse = cmd.getType() == CommandBase.FLAG_HAVE_PARAMETER_AND_RESPONSE
|| cmd.getType() == CommandBase.FLAG_NO_PARAMETER_HAVE_RESPONSE;
if (hasResponse) { //has reply
CustomResponse response = cmd.getResponse();
if (null == response) {
onErrCode(device, new BaseError(RcspErrorCode.ERR_PARSE_DATA, RcspErrorCode.getErrorDesc(RcspErrorCode.ERR_PARSE_DATA)));
return;
}
byte[] data = response.getData();
//parse data
}
}
@Override
public void onErrCode(BluetoothDevice device, BaseError error) {
//callback error event
}
});
}
14.2. Receiving Custom Commands
//WatchManager is a subclass of WatchOpImpl, must configure sdk in 1.3
WatchManager manager = WatchManager.getInstance();
//add Rcsp event callback
manager.registerOnRcspCallback(new OnRcspCallback() {
@Override
public void onRcspCommand(BluetoothDevice device, CommandBase command) {
//receive rcsp command
if (command.getId() != Command.CMD_EXTRA_CUSTOM) return; //filter other command
CustomCmd customCmd = (CustomCmd) command;
//Determine whether to reply the command
boolean hasResponse = customCmd.getType() == CommandBase.FLAG_HAVE_PARAMETER_AND_RESPONSE
|| customCmd.getType() == CommandBase.FLAG_NO_PARAMETER_HAVE_RESPONSE;
CustomParam param = customCmd.getParam();
if (param == null) {
if (hasResponse) { //need reply
////you can set up the custom response data if desired
//byte[] responseData = new byte[0]; //custom response data
//param.setData(responseData);
//customCmd.setParam(param);
customCmd.setParam(null);
customCmd.setStatus(StateCode.STATUS_SUCCESS);
manager.sendCommandResponse(device, customCmd, null);
}
return;
}
byte[] data = param.getData(); //the custom data from device
//parse data
if (hasResponse) { //need reply
//doing some thing and reply a success result.
customCmd.setStatus(StateCode.STATUS_SUCCESS);
////you can set up the custom response data if desired
//byte[] responseData = new byte[0]; //custom response data
//param.setData(responseData);
//customCmd.setParam(param);
customCmd.setParam(null);
manager.sendCommandResponse(device, customCmd, null);
}
}
});