14. 自定义命令

方便客户拓展功能

Important

  1. 自定义命令的长度限制
    public void checkRCSPProtocolMTU(BluetoothDevice device) {
        //最大发送MTU
        int protocolMtu = DeviceStatusManager.getInstance().getMaxCommunicationMtu(device);
        //自定义命令的大小 = 最大发送MTU - 协议包长度
        int customDataLimit = protocolMtu - 23;
        //建议与固件协商好,不建议发最大数据
    }
    
  2. 建议与固件协商好最大发送数值, 不要超过协议MTU

14.1. 发送自定义命令

public void sendCustomCommand(byte[] data) {
    //WatchManager是WatchOpImpl的子类,须在1.3配置好sdk
    WatchManager manager = WatchManager.getInstance();
    //build the custom command
    CommandBase customCmd = CommandBuilder.buildCustomCmd(data); //carries custom data
    final boolean isNoResponse = false; //设置自定义命令是否不需要回复, 不建议使用命令不回复
    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) { //有回复
                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. 接收自定义命令

//WatchManager是WatchOpImpl的子类,须在1.3配置好sdk
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) { //需要回复
                    ////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) { //需要回复
                //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);
            }
    }
});