Speed up SparkScan integration with Agent Skills
Install the Scandit plugin and use the /sparkscan-kmp skill so that your AI coding agent can integrate, debug, and customize SparkScan on Kotlin Multiplatform following Scandit's recommended patterns. More info →
Run the following command in your project directory. It detects the supported coding agents you have installed and adds the Scandit plugin to each. Re-run it to update.
npx plugins add scandit/skillsPrefer to set it up yourself? Manual installation steps for each agent →
Configuring SparkScan
SparkScan works without any configuration. This page shows the code for the changes you can make to it. For what each option means for your users, see How SparkScan works.
Most options are on SparkScanViewSettings. Pass your settings as viewSettings to the Compose SparkScanView, or to the imperative SparkScanView constructor. The Compose SparkScanView reads viewSettings only when it creates the view. To apply other settings, pass a new SparkScanViewSettings object and create the view again, for example by wrapping the composable in key(viewSettings).
Using a hardware button to scan
Let users start scanning with the hardware button instead of the trigger button on the screen. On iOS, the hardware button is the volume button.
Before you start:
- On Android, make sure the device runs Android API level 28 or higher. To check at runtime whether a device supports hardware buttons, read
SparkScanView.hardwareTriggerSupported.
Set
SparkScanViewSettings.hardwareTriggerEnabledtotruebefore you create theSparkScanView. It's off by default. On Android, changes after that don't apply to the view.import com.kmp.datacapture.barcode.spark.SparkScanViewSettingsval viewSettings = SparkScanViewSettings.sparkScanViewSettings().apply {hardwareTriggerEnabled = true}Optional: to choose the button on Android, set its key code in
SparkScanViewSettings.hardwareTriggerKeyCode. By default, the button is the dedicated hardware button on XCover devices and the volume-down button on other devices.
To check that it works: press the hardware button. The scanner starts, as it does when users tap the trigger button.
Showing an error for a scanned barcode
Show an error when your app doesn't accept a barcode, for example one that's already on a list or that comes from the wrong lot. Users see your message, and scanning pauses until the timeout ends or they tap the trigger button. For how this works and how to choose the timeout, see Error state.
-
Implement
SparkScanFeedbackDelegate. IngetFeedbackForBarcode(), return aSparkScanBarcodeSuccessFeedbackor aSparkScanBarcodeErrorFeedbackwith a message and a timeout. This method runs on a background thread, so update the UI on the main thread.import com.kmp.datacapture.barcode.data.Barcodeimport com.kmp.datacapture.barcode.spark.SparkScanBarcodeErrorFeedbackimport com.kmp.datacapture.barcode.spark.SparkScanBarcodeFeedbackimport com.kmp.datacapture.barcode.spark.SparkScanBarcodeSuccessFeedbackimport com.kmp.datacapture.barcode.spark.SparkScanFeedbackDelegateclass MyFeedbackDelegate : SparkScanFeedbackDelegate {override fun getFeedbackForBarcode(barcode: Barcode): SparkScanBarcodeFeedback? =if (isValidBarcode(barcode)) {SparkScanBarcodeSuccessFeedback()} else {SparkScanBarcodeErrorFeedback(errorMessage = "This code should not have been scanned",resumeCapturingDelay = 60_000L, // in milliseconds)}private fun isValidBarcode(barcode: Barcode): Boolean {// Your validation logic, for example a lookup in your item listreturn true}} -
Set the delegate on
SparkScanView.feedbackDelegate.Use the imperative
SparkScanViewfor this: the ComposeSparkScanViewhas no parameter for a feedback delegate.sparkScanView.feedbackDelegate = MyFeedbackDelegate()
You can return different errors for different conditions, for example one for a wrong barcode and another for a duplicate.
Each error has its own message and timeout, and can also set the color of the screen flash (visualFeedbackColor), the brush that highlights the barcode (brush), and the sound and vibration (feedback). Set them with the constructor, where every parameter after the timeout is optional.
To turn off the screen flash for every scan, set SparkScanViewSettings.visualFeedbackEnabled to false.
To check that it works: scan a barcode that your app rejects. The error message appears, and the scanner pauses for the timeout or until you tap the trigger button.
Rejecting barcodes
Reject a barcode without pausing the scanner when a wrong barcode shouldn't stop users. They get the error feedback and can keep scanning right away.
-
In the feedback delegate from Showing an error for a scanned barcode, return a
SparkScanBarcodeErrorFeedbackwhoseresumeCapturingDelayis0L.SparkScanBarcodeErrorFeedback(errorMessage = "This code is already on the list",resumeCapturingDelay = 0L,)
To check that it works: scan a barcode that your app rejects. You get the error feedback, and you can scan the next barcode right away.
Customizing the UI
Change which buttons and controls the SparkScan view shows, and control scanning from your own UI. The full list of SparkScanView properties is in the SparkScanView API reference.
With the Compose SparkScanView, choose which controls are visible with its parameters, such as torchControlVisible and zoomSwitchControlVisible. For example, to hide the torch and zoom controls:
SparkScanView(
settings = settings,
torchControlVisible = false,
zoomSwitchControlVisible = false,
)
The Compose SparkScanView has no startScanning(), pauseScanning() or uiListener. To use your own trigger button or to handle the buttons for other scanning modes, as the following sections describe, use the imperative SparkScanView and set its properties.
You can change most of the SparkScan UI to fit your app:
- The colors of icons, buttons and the toolbar
- The trigger button and its icon
- The text of every toast message
- The size of the mini preview
- Which icons are visible
Using your own trigger button
The default trigger button fits most apps. When yours needs a different control, for example a button that's already part of your UI, hide the default trigger button with sparkScanView.triggerButtonVisible and control scanning yourself:
- To start scanning, call
sparkScanView.startScanning()from your own button. To update your button when the scanner's state changes, use the SparkScan view UI listener. - To stop scanning, show the mini preview's close button with
sparkScanView.previewCloseControlVisible, or callsparkScanView.pauseScanning()from your own button.
You can also rely only on a hardware button, if you turned that on with sparkScanViewSettings.hardwareTriggerEnabled.
Two examples of a customized SparkScan UI:


Showing the settings toolbar
The settings toolbar is hidden by default. When you show one of its buttons, such as the selection mode or scanning behavior button, the toolbar appears at the bottom of the mini preview.
In the Compose SparkScanView, the settings toolbar shows by default, because some of its buttons are visible by default.
Showing buttons for other scanning modes
Some workflows need another scanning mode next to SparkScan, such as MatrixScan Find, MatrixScan Count or Smart Label Capture. SparkScan has prebuilt toolbar buttons that switch to those modes from the SparkScan UI.
First, show the buttons:
// Show the Smart Label Capture, MatrixScan Count and MatrixScan Find buttons
sparkScanView.labelCaptureButtonVisible = true
sparkScanView.barcodeCountButtonVisible = true
sparkScanView.barcodeFindButtonVisible = true


Then set your UI listener on the SparkScanView with sparkScanView.uiListener. It's called when users tap the MatrixScan Find, MatrixScan Count or Smart Label Capture button in the toolbar.
Changing the scanning workflow
Change the workflow when the default one doesn't fit how your users scan. Three settings set the scanner's initial state, and you can combine them.
-
Set the selection mode with
SparkScanSettings.selectionMode. -
Set the scanning behavior with
SparkScanViewSettings.scanningBehaviorand the preview behavior withSparkScanViewSettings.previewBehavior. -
Optional: to stop users from scanning continuously by touching & holding the trigger button, set
SparkScanViewSettings.holdToScanEnabledtofalse.
To check that it works: tap the trigger button. The scanner starts in the selection mode, scanning behavior, and preview behavior you set.
For the values and defaults, see the SparkScan settings reference. For what each mode does, see How SparkScan works.