BarcodeScanner
@MainActor
public protocol BarcodeScanner : AnyObject
Defines the common interface for all barcode scanner implementations within the framework.
This protocol ensures that different types of scanners (e.g., camera-based, Bluetooth hardware)
can be managed and interacted with in a consistent manner by the BarcodeScannerManager
.
All methods are expected to be called or operate on the main actor.
-
The specific type of the scanner (e.g.,
.visionKit
,.proGlove
).Declaration
Swift
@MainActor var type: ScannerType { get }
-
The current operational status of the scanner.
Declaration
Swift
@MainActor var currentStatus: ScannerStatus { get }
-
The delegate object that receives callbacks for status changes and scanned barcodes.
Declaration
Swift
@MainActor var delegate: (any BarcodeScannerDelegate)? { get set }
-
startMonitoring()
AsynchronousPrepares the scanner for operation. For camera scanner, this might involve checking permissions and initializing the camera session. For hardware scanner, this might involve powering on Bluetooth or connecting to a previously paired device. This method should bring the scanner to a
.ready
state if successful.Throws
AScannerError
if monitoring cannot be started (e.g., permissions denied, Bluetooth off).Declaration
Swift
@MainActor func startMonitoring() async throws
-
Stops the scanner’s operation. For camera scanner, this releases the camera. For hardware scanner, this may disconnect the device or put it into a low-power state. The scanner should ideally transition to an
.idle
or appropriate error state.Declaration
Swift
@MainActor func stopMonitoring()
-
triggerScan()
Default implementation, asynchronousInitiates a scan attempt. For camera scanner using VisionKit, this typically means starting the
DataScannerViewController
‘s scan. For hardware scanner that requires a software trigger (if any), this would send the trigger command. For hardware scanner that is trigger-based (e.g., ProGlove button press), this method might do nothing or thrownot_supported
.Throws
AScannerError
if the scan cannot be triggered (e.g., not ready, not supported).Default Implementation
Default implementation for
triggerScan
. Many hardware scanners are self-triggered. Does nothing by default. Can be overridden by scanners that support software triggering.Declaration
Swift
@MainActor func triggerScan() async throws
-
Resets the scanner to its initial or a clean state. This may involve disconnecting from a hardware scanner, clearing any cached data or error states, and preparing it for a completely new session (including pairing, if applicable).
Declaration
Swift
@MainActor func reset()
-
getPairingQRCode()
Default implementationFor scanners requiring pairing (e.g., ProGlove, IPCMobile), this method generates a pairing QR code. The application can then display this
Image
for the user to scan with the hardware scanner.Default Implementation
Default implementation for
getPairingQRCode
. Only relevant for pairable hardware scanners. Returnsnil
by default.Declaration
Swift
@MainActor func getPairingQRCode() -> Image?
Return Value
A SwiftUI
Image
containing the pairing QR code, ornil
if not applicable or if generation fails. Ifnil
is returned due to an error, the scanner should update itscurrentStatus
. -
updateScannerDisplay(data:
Default implementation) Sends data to be displayed on the scanner’s screen, if the hardware supports it (e.g., ProGlove display, IPCMobile display).
Default Implementation
Default implementation for
updateScannerDisplay
. Not all scanners have displays. Does nothing by default.Declaration
Swift
@MainActor func updateScannerDisplay(data: ScannerDisplayData)
Parameters
data
A
ScannerDisplayData
enum case containing the specific data for the active scanner type. -
Indicates whether the scanner type is available and generally usable on the current device. This might check for SDK availability, required hardware (like Bluetooth), or basic permissions if known synchronously. It doesn’t necessarily mean a specific device is connected and ready, but rather that the type of scanner can function.
Declaration
Swift
@MainActor func isAvailable() -> Bool
Return Value
true
if the scanner type is available,false
otherwise. -
getScannerView()
Default implementationFor scanners that provide their own UI (primarily VisionKit’s
DataScannerViewController
), this method returns the view controller. The application can then present this view controller to show the camera feed for scanning.Default Implementation
Default implementation for
getScannerView
. Only VisionKit provides a view controller. Returnsnil
by default.Declaration
Swift
@MainActor func getScannerView() -> UIViewController?
Return Value
A
UIViewController
instance for the scanner’s UI, ornil
if the scanner does not provide one.