23. Reusable Space Special Upgrade Process

This function is an upgrade process designed for small capacity solutions.

23.1. Compressed Resource Package Method

23.1.1. Flow Chart

Watch Reusable Space Special Upgrade Flow Chart

23.1.2. Sample Code

public class ReuseSpaceOTADemo {

    private WatchManager watchManager;
    private OTAManager otaManager;

    private boolean isOTAInitOk;
    private OnWatchCallback onWatchCallback;


    void init(@NonNull Context context) {
        //WatchManager is a subclass of WatchOpImpl, must configure sdk in 1.3
        watchManager = WatchManager.getInstance();
        //Initialize OTA manager
        otaManager = new OTAManager(context);
        otaManager.addOnRcspCallback(new OnRcspCallback() {
            @Override
            public void onRcspInit(BluetoothDevice bluetoothDevice, boolean b) {
                isOTAInitOk = b;
            }

            @Override
            public void onMandatoryUpgrade(BluetoothDevice bluetoothDevice) {
                isOTAInitOk = true;
            }
        });
    }

    void destroy() {
        isOTAInitOk = false;
        releaseCallback();
        if (null != otaManager) {
            otaManager.release();
            otaManager = null;
        }
    }

    void releaseCallback() {
        if (null != onWatchCallback) {
            watchManager.unregisterOnWatchCallback(onWatchCallback);
            onWatchCallback = null;
        }
    }

    void tryToUpdateResource() {
        if (null == watchManager || null == otaManager || !isOTAInitOk) return;
        //resourcePath: Resource path (zip compressed package)
        final String resourcePath = "Required updated resource compressed package path";
        onWatchCallback = new OnWatchCallback() {
            @Override
            public void onRcspInit(BluetoothDevice device, boolean isInit) {
                //Callback RCSP protocol layer initialization result
                if (!isInit) { //Initialization failed, disconnect device connection
                    releaseCallback();
                    watchManager.getBluetoothHelper().disconnectDevice(device);
                }
            }

            @Override
            public void onWatchSystemInit(int code) {
                final BluetoothDevice device = watchManager.getConnectedDevice();
                //Callback watch system initialization result
                if (code == WatchError.ERR_NONE) {
                    final DeviceInfo deviceInfo = watchManager.getDeviceInfo(device);
                    if (null != deviceInfo && deviceInfo.getExpandMode() != WatchConstant.EXPAND_MODE_NONE) {
                        onResourceUpdateUnfinished(device);
                        return;
                    }
                    return;
                }
                onRcspInit(device, false);
            }

            @Override
            public void onResourceUpdateUnfinished(BluetoothDevice device) {
                //Callback resource not updated
                updateResource(resourcePath);
            }
        };
        watchManager.registerOnWatchCallback(onWatchCallback);
        //Normal mode
        updateResource(resourcePath);
    }

    void updateResource(String resourcePath) {
        //OnUpdateResourceCallback: Update resource progress listener
        watchManager.updateWatchResource(resourcePath, new OnUpdateResourceCallback() {
            @Override
            public void onStart(String filePath, int total) {
                //Callback start
                //filePath -- Resource file path
                //total -- Total number of files to update
            }

            @Override
            public void onProgress(int index, String filePath, float progress) {
                //Callback progress
                //index -- Index (starting from 0)
                //filePath -- Resource file path
                //progress -- Update progress
            }

            @Override
            public void onStop(String otaFilePath) {
                //Callback update end
                //otaFilePath: Firmware upgrade file path
                //If firmware upgrade file is not null, it means OTA upgrade is required
                if (!TextUtils.isEmpty(otaFilePath)) {
                    updateFirmware(otaFilePath);
                    return;
                }
                //Resource update completed
                onError(WatchError.ERR_NONE, "Update Resource Successfully");
            }

            @Override
            public void onError(int code, String message) {
                //Callback error event
                //code -- Error code (refer to WatchError)
                //message -- Error message
                releaseCallback();
            }
        });
    }

    void updateFirmware(String otaFilePath) {
        if (null == otaManager || !isOTAInitOk || TextUtils.isEmpty(otaFilePath)) return;
        final BluetoothOTAConfigure configure = otaManager.getBluetoothOption();
        configure.setFirmwareFilePath(otaFilePath);
        otaManager.configure(configure);
        otaManager.startOTA(new IUpgradeCallback() {
            @Override
            public void onStartOTA() {

            }

            @Override
            public void onNeedReconnect(String s, boolean b) {

            }

            @Override
            public void onProgress(int i, float v) {

            }

            @Override
            public void onStopOTA() {
                //Upgrade completed
                onError(OTAError.buildError(ErrorCode.ERR_NONE));
            }

            @Override
            public void onCancelOTA() {

            }

            @Override
            public void onError(BaseError baseError) {
                if (null == baseError) return;
                //Firmware program update completed, need to reconnect device to force update resources
                if (baseError.getSubCode() == ErrorCode.SUB_ERR_NEED_UPDATE_RESOURCE) {
                    String reconnectDeviceMac = baseError.getMessage();
                    //Reconnect device
                    reconnectDevice(reconnectDeviceMac);
                    return;
                }
                releaseCallback();
            }
        });
    }

    void reconnectDevice(String address) {
        if (null == watchManager || !CHexConver.checkBluetoothAddress(address)) return;
        final BluetoothHelper btHelper = watchManager.getBluetoothHelper();
        btHelper.addBluetoothEventListener(new BluetoothEventListener() {
            @Override
            public void onBtDiscoveryStatus(boolean bBle, boolean bStart) {
                if (!bStart) {
                    btHelper.removeBluetoothEventListener(this);
                }
            }

            @Override
            public void onBtDiscovery(BluetoothDevice device, BleScanMessage bleScanMessage) {
                if (null == device) return;
                if (TextUtils.equals(device.getAddress(), address)) {
                    //Stop Bluetooth search
                    btHelper.getBluetoothOp().stopBLEScan();
                    btHelper.addBluetoothEventListener(new BluetoothEventListener() {
                        @Override
                        public void onConnection(BluetoothDevice device, int status) {
                            if (null == device) return;
                            if (status != BluetoothConstant.CONNECT_STATE_CONNECTING && TextUtils.equals(device.getAddress(), address)) {
                                btHelper.removeBluetoothEventListener(this);
                                if (status == BluetoothConstant.CONNECT_STATE_CONNECTED) {
                                    //Reconnection successful
                                    return;
                                }
                                //Reconnection failed
                                releaseCallback();
                            }
                        }
                    });
                    if (!btHelper.connectDevice(device, bleScanMessage)) {
                        //Reconnection device failed
                        releaseCallback();
                    }
                }
            }
        });
        //Perform device search operation
        btHelper.getBluetoothOp().startBLEScan(60 * 1000);
    }
}

23.2. With Resource Upgrade File Method

Refer to Reusable Space Special Upgrade Process