Search code examples
c#win-universal-appbarcode-scanner

ClaimedBarcodeScanner.DataReceived() event as keyboard input for WebView control (UWP)


I have successfully written an app for Honeywell Dolphin 75e device with both embedded and external ring scanner running Windows 10 Mobile Enterprise.

There are plenty of resources on how to deal with barcode scanners in UWP on the Internet. However, all off them are scanning into some text based user controls like so:

private async void ScenarioStartScanButton_Click(object sender, RoutedEventArgs e)
{
    // Add a data receive event handler.
    claimedScanner.DataReceived += claimedScanner_DataReceived;
}

async void claimedScanner_DataReceived(ClaimedBarcodeScanner sender, BarcodeScannerDataReceivedEventArgs args)
{
    // Update the UI with the data received from the scan.
    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
    {
        // Read the data from the buffer and convert to string.
        var scanDataLabelReader = DataReader.FromBuffer(args.Report.ScanDataLabel);
        ScenarioOutputScanDataLabel.Text = scanDataLabelReader.ReadString(args.Report.ScanDataLabel.Length);

        var scanDataReader = DataReader.FromBuffer(args.Report.ScanData);
        ScenarioOutputScanData.Text = scanDataReader.ReadString(args.Report.ScanData.Length);

        ScenarioOutputScanDataType.Text = BarcodeSymbologies.GetName(args.Report.ScanDataType);

    }); 
}

But what I need is the scanner to act like a keyboard on my WebView control:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <WebView Source="http://google.co.uk"/>
</Grid>

When I don't use (don't claim) scanner explicitly in my app and leave the default working like it does globally for any app, the scanner does what I need in my WebView. But since I have to assign/claim different scanners within my app, I have to make it act like a keyboard, meaning, that when any user input field inside the WebView gets focus, I just scan input into it.

Here I found a similar question, although it was easy to solve, since only the parameter in URL (search term) had to be different for each scan.

UPDATE: I also thought about a workaround, so my app would claim the scanner and retain it even when I leave the app. In that case I could open a browser, navigate to my web app and use desired scanner. Unfortunately, all but embedded scanners seem to get disposed after I leave the app. Even though I don't explicitly dispose them.

None of Honeywell provided test apps seem to retain the claim either.


Solution

  • Ok, looks like I now understand how it all works. For what I need, I don't need to create any app that would claim and enable specific scanner, as it would put the scanner from default Wedge Mode to POS Mode.

    The difference between those two modes is described in Dolphin 75e user guide.

    Dolphin 75e models running Windows 10 IoT Mobile Enterprise has two scan modes, Wedge mode and POS mode. In Wedge mode bar code data is inserted into the keyboard interface, as if the bar code data was entered using the keyboard. POS mode implements Microsoft Point of Service interface. In POS mode, barcode data is sent to an application via the Microsoft defined APIs.

    Scan wedge mode is enabled by default. The 75e remains in wedge mode until POS application starts and claims the scanner. The terminal only switches back to wedge mode when the POS application releases its claim on scanner.

    So, all I need is to place scanner config file into /Documents/Profile folder, specifying which scanner I want to use in the Wedge mode. Unfortunately, my USB HID ring scanner cannot be put into the wedge mode...

    Note that wedge mode is not supported for USB HID scanner in v66.4.0.638, or v66.4.0.569.

    So after all this struggle I found out that if one wants to use 75e scanner for custom web app, or scan to, say, Excel spreadsheet - his only option is to use embedded scanner, which operates in Wedge mode by default.

    And if you want to use USB HID scanner, then you can only use it for specifically developed app that implements Microsoft defined APIs. So basically, you will only be able to scan to specific user controls - text blocks, input fields - as shown in my first post.

    UPDATE: Since version 66.4.0.718 USB HID scanner can also be used in wedge mode!