### Install the package Source: https://github.com/playerdata/react-native-mcu-manager/blob/main/react-native-mcu-manager/README.md Add the library to your project dependencies using npm. ```bash npm install @playerdata/react-native-mcu-manager ``` -------------------------------- ### Install npm dependencies Source: https://github.com/playerdata/react-native-mcu-manager/blob/main/README.md Install project dependencies using npm. ```bash npm install ``` -------------------------------- ### Install React Native MCU Manager Source: https://context7.com/playerdata/react-native-mcu-manager/llms.txt Commands to install the package and configure native dependencies. ```bash # Using pnpm pnpm install @playerdata/react-native-mcu-manager # Using npm npm install @playerdata/react-native-mcu-manager # For iOS, install pods npx pod-install ``` -------------------------------- ### Upgrade Class Usage Source: https://context7.com/playerdata/react-native-mcu-manager/llms.txt Demonstrates how to use the `Upgrade` class to manage firmware update operations, including progress and state callbacks, starting, canceling, and destroying the upgrade process. ```APIDOC ## Upgrade Class Usage ### Description This section details the usage of the `Upgrade` class for managing firmware update operations. It includes initialization with necessary options, handling progress and state changes, initiating the upgrade, canceling an ongoing process, and proper cleanup. ### Class `Upgrade` ### Constructor Parameters - **bleId** (string) - Required - The Bluetooth Low Energy device identifier (MAC address on Android, UUID on iOS). - **updateFileUri** (string) - Required - The URI to the firmware update file. - **options** (UpgradeOptions) - Required - Configuration options for the upgrade process. - **estimatedSwapTime** (number) - Required - Estimated time in seconds for the device to swap to the updated image. - **upgradeFileType** (UpgradeFileType) - Required - The type of the firmware file (e.g., `UpgradeFileType.BIN`, `UpgradeFileType.ZIP`). - **upgradeMode** (UpgradeMode) - Optional - The firmware upgrade strategy (defaults to `UpgradeMode.TEST_AND_CONFIRM`). - **eraseAppSettings** (boolean) - Optional - Whether to erase application settings during the upgrade. - **mcubootBufferCount** (number) - Optional - The number of concurrent packets for faster uploads. - **onProgress** (function) - Optional - A callback function that receives the upload progress percentage (0-100). - **onStateChange** (function) - Optional - A callback function that receives the current firmware upgrade state. ### Methods - **runUpgrade()**: Initiates the firmware upgrade process. Returns a Promise that resolves on success or rejects on failure. - **cancel()**: Cancels an in-progress firmware upgrade. - **destroy()**: Releases native resources associated with the upgrade instance. This should always be called when the instance is no longer needed. ### Request Example ```typescript import { Upgrade, UpgradeMode, UpgradeFileType, type FirmwareUpgradeState, type UpgradeOptions, } from '@playerdata/react-native-mcu-manager'; const options: UpgradeOptions = { estimatedSwapTime: 60, upgradeFileType: UpgradeFileType.BIN, upgradeMode: UpgradeMode.TEST_AND_CONFIRM, eraseAppSettings: false, mcubootBufferCount: 3, }; const onProgress = (progress: number) => { console.log(`Upload progress: ${progress}%`); }; const onStateChange = (state: FirmwareUpgradeState) => { console.log(`Upgrade state: ${state}`); }; const upgrade = new Upgrade( 'AA:BB:CC:DD:EE:FF', // bleId 'file:///path/to/firmware.bin', // updateFileUri options, onProgress, onStateChange ); try { await upgrade.runUpgrade(); console.log('Upgrade completed successfully'); } catch (error) { console.error('Upgrade failed:', error.message); } // To cancel: // upgrade.cancel(); // IMPORTANT: Always destroy to release native resources // upgrade.destroy(); ``` ### Response Example (No direct response body for `runUpgrade`, success is indicated by promise resolution and completion logs, failure by promise rejection and error logs.) ``` -------------------------------- ### Install react-native-mcu-manager with pnpm Source: https://github.com/playerdata/react-native-mcu-manager/blob/main/README.md Add the package to your project dependencies using pnpm. ```bash pnpm install @playerdata/react-native-mcu-manager ``` -------------------------------- ### Get Bootloader Information Source: https://context7.com/playerdata/react-native-mcu-manager/llms.txt Retrieves bootloader configuration and capabilities for a connected device. Requires the device's BLE ID. Logs details like bootloader name, buffer count and size, downgrade protection status, and operational mode. ```typescript import { bootloaderInfo, type BootloaderInfo, MCUBootMode, } from '@playerdata/react-native-mcu-manager'; try { // bleId: MAC address on Android, UUID on iOS const info: BootloaderInfo = await bootloaderInfo('AA:BB:CC:DD:EE:FF'); console.log('Bootloader:', info.bootloader); // e.g., "MCUboot" or null console.log('Buffer Count:', info.bufferCount); // Number of buffers available console.log('Buffer Size:', info.bufferSize); // Size of each buffer in bytes console.log('No Downgrade:', info.noDowngrade); // true if downgrades are blocked console.log('Mode:', info.mode); // MCUBootMode value // Check MCUboot mode if (info.mode === MCUBootMode.MCUBOOT_MODE_SWAP_USING_SCRATCH) { console.log('Device uses swap with scratch partition'); } } catch (error) { console.error('Failed to get bootloader info:', error.message); } ``` -------------------------------- ### React Hook for Firmware Updates Source: https://context7.com/playerdata/react-native-mcu-manager/llms.txt A custom React hook to manage firmware updates, including state tracking for progress and status, and functions to start or cancel the update process. It handles initialization, cleanup, and error reporting. ```typescript import { Upgrade, UpgradeMode, UpgradeFileType, type FirmwareUpgradeState, } from '@playerdata/react-native-mcu-manager'; import { useState, useEffect, useRef } from 'react'; const useFirmwareUpdate = ( bleId: string | null, updateFileUri: string | null, upgradeFileType: UpgradeFileType, upgradeMode?: UpgradeMode ) => { const [progress, setProgress] = useState(0); const [state, setState] = useState(''); const upgradeRef = useRef(null); useEffect(() => { if (!bleId || !updateFileUri) { return () => null; } const upgrade = new Upgrade( bleId, updateFileUri, { estimatedSwapTime: 60, upgradeMode, upgradeFileType, }, setProgress, setState ); upgradeRef.current = upgrade; // Cleanup on unmount or dependency change return function cleanup() { upgrade.cancel(); upgrade.destroy(); }; }, [bleId, upgradeFileType, updateFileUri, upgradeMode]); const runUpdate = async (): Promise => { try { if (!upgradeRef.current) { throw new Error('No upgrade instance - missing bleId or updateFileUri'); } await upgradeRef.current.runUpgrade(); } catch (ex: unknown) { const errorMessage = ex instanceof Error ? ex.message : 'Unknown error'; setState(errorMessage); } }; const cancelUpdate = (): void => { upgradeRef.current?.cancel(); }; return { progress, runUpdate, state, cancelUpdate }; }; // Usage in a component const FirmwareUpdateScreen = () => { const { progress, runUpdate, state, cancelUpdate } = useFirmwareUpdate( 'AA:BB:CC:DD:EE:FF', 'file:///path/to/firmware.bin', UpgradeFileType.BIN, UpgradeMode.TEST_AND_CONFIRM ); return ( State: {state} Progress: {progress}%