smpclient port to react-native-ble-managerble-manager
npm install react-native-smpclientinspired by the smpclient python library
``sh`
npm install react-native-smpclient
`typescript
import {
SMPClient,
SMPBLETransport,
isSuccess,
isError,
} from 'react-native-smpclient';
import BleManager from 'react-native-ble-manager';
// Initialize BLE Manager first
BleManager.start({ showAlert: false });
`
`typescript`
const transport = new SMPBLETransport();
const client = new SMPClient(transport, 'My Board', {
timeoutMs: 5000,
debug: true, // Enable debug logging
});
`typescript`
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);
}
`typescript
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();
`
`typescript
// 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}%);Uploaded: ${progress.offset} / ${progress.total} bytes
console.log();
},
});
// 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();
`
#### 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 callbacktestImage(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.
MIT