How fabi019/hid-barcode-scanner works
1. This is a single-module Android app (app) written in Kotlin, with the architecture centered around three runtime concerns: camera-based scanning, Bluetooth output, and user-configurable scan processing.
2. The entry points are MainActivity and SettingsActivity. The app uses a Compose-style UI layer under ui/, with reusable UI components, theme files, and screen-specific state holders in ui/model/ (CameraViewModel, DevicesViewModel, HistoryViewModel).
3. The main user flow starts on the device-selection / connection path and then moves into the scanner. Devices.kt, Scanner.kt, DevicesViewModel, and the Bluetooth controller classes coordinate device discovery, connection choice, and handoff into scanning once a target device is ready.
4. The scanning pipeline is camera-driven: CameraPreview feeds frames into ZXingAnalyzer, which produces BarcodeResult data. There is also an OCR-related model under org/totschnig/ocr/Text.kt, suggesting the scanner can work with more than just barcode symbology in some paths, but the primary barcode decoding engine is ZXing C++.
5. Scan results are not sent directly. They pass through a processing layer in utils/ and ui/ that can apply regex filtering, template expansion, custom JavaScript logic (JsEngineService, JsEditor, TemplateProcessor), and app-specific options such as extra keys or volume-key behavior. This processing layer is the main “business logic” boundary between raw scan output and transport output.
6. Bluetooth output is abstracted in bt/. BluetoothController and BluetoothService coordinate the connection lifecycle, while KeyboardSender, KeyTranslator, and Descriptor turn processed text into low-level HID keyboard reports. The app’s core architectural distinction is that it can emit either HID keyboard events or RFCOMM/SPP serial data.
7. The HID path is the default and is designed to behave like a physical keyboard, using the Android Bluetooth HID capability. The RFCOMM path (RfcommController) is an alternate transport for serial-port-style delivery over Bluetooth, useful when keyboard emulation is undesirable or unsupported. The README makes clear that both modes share the same scanned-data source but diverge at the transport boundary.
8. User preferences and feature toggles are centralized in Settings.kt and utils/PreferenceStore.kt. These settings drive key architectural decisions such as keyboard layout selection, auto-connect behavior, auto-send, filtering, connection mode, and other scanner/transport options. ui/Preference.kt and related components render this configuration layer.
9. History is a separate subsystem rather than a side effect of scanning. History.kt, HistoryViewModel, and ui/HistoryFilter.kt manage stored scan sessions and export them through utils/Serializer into text, JSON, or CSV. This makes history an independent data domain that can be consumed without the live scanner or Bluetooth connection.
10. Keyboard layout mapping is data-driven. Layout definitions live in app/src/main/assets/keymaps/*.layout, and KeyTranslator uses them to convert characters into HID codes appropriate for the selected host keyboard layout. This is an important boundary because the app sends scan content as key events, not as literal characters.
11. The app also includes device and system integration helpers in utils/: Permission handling, SystemBroadcastReceiver, lifecycle helpers, image utilities, device metadata, and latency tracing. These classes support the main flows but are not separate business subsystems; they bridge Android platform concerns into the scanner/Bluetooth architecture.
12. The repository is covered by both unit and instrumented tests. Existing tests focus on the most architecture-sensitive logic: key translation, device info, serialization, and template processing. That lines up with the repo’s main risk areas: correct character-to-HID mapping, stable export formats, and deterministic scan transformation.
13. Operationally, the app is fully offline and self-contained. It ships localized resources, locale-specific string sets, XML shortcuts, backup/data-extraction configs, and app metadata for distribution channels, but it does not depend on external backend services at runtime.
Main entry points: Scanner.kt Scanner · BluetoothController.kt BluetoothController · Navigation.kt Navigation · PreferenceStore.kt PreferenceStore
Components 30
App shell
Android entry points and screen routing
- MainActivity · App entry and primary navigation host
app/src/main/java/dev/fabik/bluetoothhid/MainActivity.kt - SettingsActivity · Settings entry screen
app/src/main/java/dev/fabik/bluetoothhid/SettingsActivity.kt - Navigation · Compose screen routing between app flows
app/src/main/java/dev/fabik/bluetoothhid/ui/Navigation.kt - Devices · Device selection and connection handoff
app/src/main/java/dev/fabik/bluetoothhid/Devices.kt - DevicesViewModel · State and coordination for device flow
app/src/main/java/dev/fabik/bluetoothhid/ui/model/DevicesViewModel.kt
Scan flow
Camera capture and result generation
- Scanner · Live scanning screen and scan actions
app/src/main/java/dev/fabik/bluetoothhid/Scanner.kt - CameraPreview · Camera feed presentation
app/src/main/java/dev/fabik/bluetoothhid/ui/CameraPreview.kt - CameraOverlay · Overlay and framing controls for scanning
app/src/main/java/dev/fabik/bluetoothhid/ui/CameraOverlay.kt - ZXingAnalyzer · Frame analyzer producing barcode results
app/src/main/java/dev/fabik/bluetoothhid/utils/ZXingAnalyzer.kt - BarcodeResult · Decoded scan payload model
app/src/main/java/dev/fabik/bluetoothhid/ui/model/BarcodeResult.kt - OCR Text · Text model for OCR-oriented paths
app/src/main/java/org/totschnig/ocr/Text.kt - CameraViewModel · Scanner camera state holder
app/src/main/java/dev/fabik/bluetoothhid/ui/model/CameraViewModel.kt
Bluetooth transport
HID and RFCOMM output paths
- BluetoothController · Bluetooth connection lifecycle and mode selection
app/src/main/java/dev/fabik/bluetoothhid/bt/BluetoothController.kt - BluetoothService · Service backing Bluetooth operations
app/src/main/java/dev/fabik/bluetoothhid/bt/BluetoothService.kt - KeyboardSender · Builds and sends HID keyboard reports
app/src/main/java/dev/fabik/bluetoothhid/bt/KeyboardSender.kt - KeyTranslator · Maps characters to HID codes by keyboard layout
app/src/main/java/dev/fabik/bluetoothhid/bt/KeyTranslator.kt - Descriptor · HID report descriptor definition
app/src/main/java/dev/fabik/bluetoothhid/bt/Descriptor.kt - RfcommController · RFCOMM/SPP alternate Bluetooth transport
app/src/main/java/dev/fabik/bluetoothhid/bt/RfcommController.kt
Config and logic
Preferences and scan processing
- PreferenceStore · Persistent configuration and feature flags
app/src/main/java/dev/fabik/bluetoothhid/utils/PreferenceStore.kt - Settings · Central option definitions driving app behavior
app/src/main/java/dev/fabik/bluetoothhid/Settings.kt - TemplateProcessor · Template expansion and scan shaping
app/src/main/java/dev/fabik/bluetoothhid/utils/TemplateProcessor.kt - JsEngineService · Custom JavaScript scan processing
app/src/main/java/dev/fabik/bluetoothhid/utils/JsEngineService.kt - JsEditor · UI for editing custom JavaScript logic
app/src/main/java/dev/fabik/bluetoothhid/ui/JsEditor.kt
History
Stored scans and export
- HistoryViewModel · History screen state holder
app/src/main/java/dev/fabik/bluetoothhid/ui/model/HistoryViewModel.kt - History · Stored scan sessions and records
app/src/main/java/dev/fabik/bluetoothhid/History.kt - Serializer · History export to text, JSON, and CSV
app/src/main/java/dev/fabik/bluetoothhid/utils/Serializer.kt - HistoryFilter · History filtering and browsing controls
app/src/main/java/dev/fabik/bluetoothhid/ui/HistoryFilter.kt
Platform support
Android bridge helpers and system integration
- DeviceInfo · Device metadata and identification helpers
app/src/main/java/dev/fabik/bluetoothhid/utils/DeviceInfo.kt - Permissions · Permission flows and gates
app/src/main/java/dev/fabik/bluetoothhid/ui/Permissions.kt - SystemBroadcastReceiver · Receives system broadcasts for app integration
app/src/main/java/dev/fabik/bluetoothhid/utils/SystemBroadcastReceiver.kt
Connections 32
- MainActivity → Navigation: hosts · Main entry hosts navigation
- SettingsActivity → Settings: edits · Settings entry exposes app options
- Navigation → Devices: routes to · Initial flow starts with device selection
- Navigation → Scanner: routes to · Handoff into scanning after connection
- Navigation → HistoryFilter: routes to · History browsing path
- Devices → DevicesViewModel: uses · Device UI driven by view model state
- DevicesViewModel → BluetoothController: chooses · Coordinates discovery and connection choice
- BluetoothController → BluetoothService: starts · Connection lifecycle backed by service
- BluetoothController → RfcommController: switches · Selects alternate RFCOMM transport
- Scanner → CameraViewModel: uses · Scanner UI driven by camera state
- Scanner → CameraPreview: renders · Live camera feed
- Scanner → CameraOverlay: renders · Scan framing and controls
- CameraPreview → ZXingAnalyzer: feeds · Camera frames enter decoder
- ZXingAnalyzer → BarcodeResult: produces · Decoded barcode output
- ZXingAnalyzer → OCR Text: may produce · OCR-oriented text path
- BarcodeResult → TemplateProcessor: passes through · Raw result enters processing layer
- TemplateProcessor → JsEngineService: can invoke · Custom JavaScript transforms scan text
- JsEngineService → JsEditor: configured by · UI for editing scripts
- Settings → PreferenceStore: persisted in · Central options stored as preferences
- PreferenceStore → TemplateProcessor: drives · Filtering and template behavior
- PreferenceStore → BluetoothController: drives · Connection and auto-connect behavior
- PreferenceStore → KeyTranslator: drives · Keyboard layout selection
- KeyTranslator → Descriptor: uses · Produces HID-compatible reports
- BluetoothService → KeyboardSender: sends via · Keyboard-emulation output path
- KeyboardSender → KeyTranslator: maps through · Characters converted to keycodes
- RfcommController → TemplateProcessor: receives · Shares processed scan payloads
- History → Serializer: exports via · History exported in multiple formats
- HistoryViewModel → History: reads · History state backed by stored sessions
- HistoryFilter → HistoryViewModel: filters · UI narrows stored scans
- Permissions → Scanner: gates · Camera and device permissions for scanning
- SystemBroadcastReceiver → BluetoothController: observes · System integration for Bluetooth lifecycle
- DeviceInfo → Serializer: annotates · Metadata used in exports and diagnostics