react-native-smpclient
v0.1.1
Published
smpclient port to react-native-ble-managerble-manager
Maintainers
Readme
react-native-smpclient
inspired by the smpclient python library
Installation
npm install react-native-smpclientUsage
Basic Setup
import {
SMPClient,
SMPBLETransport,
isSuccess,
isError,
} from 'react-native-smpclient';
import BleManager from 'react-native-ble-manager';
// Initialize BLE Manager first
BleManager.start({ showAlert: false });Creating a Client
const transport = new SMPBLETransport();
const client = new SMPClient(transport, 'My Board', {
timeoutMs: 5000,
debug: true, // Enable debug logging
});Echo Test
try {
await client.connect();
const response = await client.echo('Hello from React Native!');
if (isSuccess(response)) {
console.log('Device echoed:', response.data.r);
} else if (isError(response)) {
console.error('Echo failed:', response);
}
await client.disconnect();
} catch (error) {
console.error('Error:', error);
}Getting Device Information
await client.connect();
// Get MCU parameters
const params = await client.getMCUMgrParameters();
if (isSuccess(params)) {
console.log('Buffer size:', params.data.buf_size);
console.log('Buffer count:', params.data.buf_count);
}
// Get image states
const imageStates = await client.getImageStates();
if (isSuccess(imageStates)) {
imageStates.data.images.forEach((img, idx) => {
console.log(`Image ${idx}: Slot ${img.slot}, Active: ${img.active}`);
});
}
await client.disconnect();Uploading Firmware
// Read firmware file (example using react-native-fs)
const fileData = new Uint8Array(/* your firmware data */);
await client.connect();
// Upload with progress tracking
await client.uploadImage(fileData, {
imageNum: 1, // Target slot
onProgress: (progress) => {
console.log(`Upload progress: ${progress.percentage}%`);
console.log(`Uploaded: ${progress.offset} / ${progress.total} bytes`);
},
});
// Verify and activate the uploaded image
const imageStates = await client.getImageStates();
if (isSuccess(imageStates) && imageStates.data.images[1]?.hash) {
// Set image as active
await client.testImage(imageStates.data.images[1].hash);
// Reset device to apply update
await client.reset();
}
await client.disconnect();API Reference
SMPClient Methods
connect()- Connect to the devicedisconnect()- Disconnect from the deviceecho(message: string)- Send an echo commandgetMCUMgrParameters()- Get MCU manager parametersgetImageStates()- Get current firmware image statesuploadImage(data: Uint8Array, options)- Upload firmware imageoptions.imageNum- Target image slot (default: 0)options.onProgress- Progress callback
testImage(hash: Uint8Array)- Mark an image for testing on next bootconfirmImage(hash?: Uint8Array)- Confirm current image as permanentreset()- Reset the device
See the example app for a complete implementation.
License
MIT
