目录

通信体验优化功能开发

5G Modem Kit提供通信体验优化功能,以下分别给出上下行速率上报及弱网信息预测上报能力使用方法典型示例。

使用上下行速率上报能力示例如下。

  1. 获取5G Modem Kit与Modem交互实例对象。

    private IInteractionQueryModem mQueryModem = InteractionQueryModemController.getInstance();

  2. 使用实例对象,注册5G Modem Kit的SDK回调接口。

    // 注册5G Modem Kit回调函数,其中IInteractionResProcess为交互信息回调接口,IInteractionConnectProcess为服务连接信息回调接口, appType 为SDK中提供的应用类型枚举定义
    public boolean registerCallback(Context context, int appType) {
        return mQueryModem.registerCallback(context, mResProcess, mConnectProcess, appType);
    }
    
    // IInteractionResProcess为交互信息回调接口
    private IInteractionResProcess mResProcess = response -> {
        if (response != null && response.getResultCode() == 0) {
            // sn为请求编号
            int sn = response.getSn();
            // responseType为本次回复的信息类型
            int responseType = response.getResponseType();
            // dataType为本次回复的数据业务类型, 当responseType=1时,本值有意义
            int dataType = response.getDataType();
            // data为该请求响应的值
            ArrayList<Byte> data = response.getData();
            String content = "";
            if (data != null && data.size() > 0) {
                int length = data.size();
                byte[] dataArray = new byte[length];
                for (int i = 0; i < length; i++) {
                    dataArray[i] = data.get(i);
                }
                content = new String(dataArray);
            } else {
                content = "request result is null";
            }
            Log.i(TAG, content);
        } else {
            // 当响应为空或错误码不为0时,表示当前请求异常
            if (response != null) {
                ArrayList<Byte> data = response.getData();
                String content = "";
                if (data != null && data.size() > 0) {
                    int length = data.size();
                    byte[] dataArray = new byte[length];
                    for (int i = 0; i < length; i++) {
                        dataArray[i] = data.get(i);
                    }
                    content = new String(dataArray);
                } else {
                    content = "request result is null";
                }
                Log.e(TAG, content);
            } else {
                Log.e(TAG, "response is null");
            }
        }
    };
    
    // IInteractionConnectProcess为服务连接信息回调接口
    private IInteractionConnectProcess mConnectProcess = response -> {
        if (response == null) {
            Log.e(TAG, "ConnectProcess callback response data is null");
            return;
        }
        String content;
        // 当错误码不为0时,表示存在连接异常
        if (response.getCode() != 0) {
            content = "connect error code: " + response.getResultCode();
            Log.e(TAG, content);
        } else {
            content = "connect code: " + response.getCode();
            Log.i(TAG, content);
        }
    };

  3. 注册成功之后,利用实例对象与Modem进行交互,使用服务。具体交互参数的枚举定义请参见Constant-values。

    // 查看注册状态
    // 如果没有注册成功,或您的App和底层服务连接断开,查询连接状态返回false,否则返回true
    public boolean getConnectStatus() {
        return mQueryModem.getAidlConnectStatus();
    }
    
    // 通知Modem启动通信体验优化服务
    public boolean startService() {
        int sn = 100;
        InteractionArg arg = new InteractionArg(NotifyType.START_SERVICE, sn);
        return mQueryModem.notify(arg);
    }
    // 设置Modem上报高低门限值
    public boolean setReportThreshold(int minThres, int maxThres) {
        int sn = 101;
        InteractionArg arg = new InteractionArg(StateType.SET_MODEM_CACHE_THRESHOLD, sn);
        SetRptThresholdInfo data = new SetRptThresholdInfo();
        data.setRptMaxThres(maxThres);
        data.setRptMinThres(minThres);
        arg.setArgs(data);
        return mQueryModem.setState(arg);
    }
    // 设置数据包优先发送的过滤规则
    public boolean setPriorityFilterRule(SetPriFilterRulesInfo setPriFilterRulesInfo) {
        int sn = 102;
        InteractionArg arg = new InteractionArg(StateType.SET_HIGH_PRIORITY_FILTER_RULE, sn);
        arg.setArgs(setPriFilterRulesInfo);
        return mQueryModem.setState(arg);
    }
    // 通知Modem丢弃无效、失效的缓存数据
    public boolean startService() {
        int sn = 103;
        InteractionArg arg = new InteractionArg(NotifyType.EXPIRED_DATA_DROP, sn);
        return mQueryModem.notify(arg);
    }
    // 通知Modem停止通信体验优化服务
    public boolean stopService() {
        int sn = 104;
        InteractionArg arg = new InteractionArg(NotifyType.STOP_SERVICE, sn);
        return mQueryModem.notify(arg);
    }

  4. (可选)不需要使用5G Modem Kit的SDK提供的通信体验优化能力时,使用实例对象注销回调。

    // 注销回调,断开和底层服务的连接
    public void unRegisterCallback() {
        mQueryModem.unRegisterCallback();
    }

使用弱网预测上报能力示例如下。

  1. 获取5G Modem Kit与Modem交互实例对象。

    private ICoordinationModem mCoordination = CoordinationModemController.getInstance();

  2. 使用实例对象,注册5G Modem Kit的弱网预测服务及对应的回调接口。

    // 注册弱网预测服务及回调函数,其中ICoordResProcess为弱网服务相关消息上报的回调接口,ICoordConnProcess为服务连接信息回调接口, serviceType 为SDK中提供的弱网预测服务的枚举定义值
    public boolean registerCallback(Context context) {
        return mCoordination.registerCallback(context, serviceType, mResProcess, mConnectProcess);
    }
    
    // ICoordResProcess为modem服务相关消息的回调接口
    private ICoordResProcess mResProcess = new ICoordResProcess() {
        @Override
        public void onExceptionReport(ExpResponse response){
            if (response == null) {
                return;
            }     
            Log.e(TAG, "receive modem exception msg:" + response);
        }
        @Override
        public void onDataReport(DataResponse response){
            if (response == null) {
                return;
            }
            int serviceType = response.getServiceType();  // 上报的本消息所属的服务
            ArrayList<Byte> data = response.getData();  // 上报的消息的具体内容
            if (data == null || data.isEmpty()) {
                return;
            }   
            int length = data.size();
            byte[] dataArray = new byte[length];
            for (int i = 0; i < length; i++) {
                dataArray[i] = data.get(i);
            }
            String content = new String(dataArray);  // 转换成字符串
            Log.i(TAG, "receive modem data msg:" + content);
        }
        @Override
        public void onCnfReport(CnfResponse response){
            if (response == null) {
                return;
            }
            Log.e(TAG, "receive modem cnf msg:" + response);
        }
        @Override
        public void onStartServiceReport(ModemServiceState response){
            if (response == null) {
                return;
            }
            Log.e(TAG, "receive modem service state msg:" + response);
        }
    };
    
    // ICoordConnProcess为服务连接信息回调接口
    private ICoordConnProcess mConnectProcess = response -> {
        if (response == 0) {
            Log.i(TAG, "connect with coordination success");
        } else {
            Log.e(TAG, "connect with coordination error:" + response);
        }
    };

  3. 注册成功之后,利用实例对象与Modem进行交互,使用服务。

    // 如果没有注册成功,或您的App和底层服务连接断开,查询连接状态返回false,否则返回true
    public boolean getConnectStatus() {
        return mCoordination.getAidlConnectStatus();
    }
    
    // 查询弱网预测结果,可查询,同时Modem如自主预测到后,会主动上报
    public boolean queryWeakSignal() {
        int sn = 100;  // 本次请求编号
        return mCoordination.queryWeakSignal(sn);  // 下发成功后,Modem收到该请求消息后,先回调mResProcess的onCnfReport接口,接着回调onDataReport接口上报真正的弱网预测内容
    }

  4. (可选)不需要使用弱网预测能力时,使用实例对象注销服务及回调。

    // 注销回调,断开和底层服务的连接
    public void unRegisterCallback(int serviceType) {
        mCoordination.unRegisterCallback(serviceType);
    }