When is it allowed to call `BarcodeScanner.GetDefaultAsync ()`?

I am trying to use the new Windows 8.1 Point of Service API for barcode scanners and if I call GetDefaultAsync()

from any of the following locations it returns null

.

  • App.OnLaunched

  • First page .Loaded

  • First page OnNavigatedTo

  • First page builder

It doesn't seem like a problem with DeviceCapabilities

or drivers, as it will work fine if I call it:

  • Click

    Event handler button
  • First page OnGotFocus

  • First page constructor, if wrapped:
    this.Dispatcher.RunIdleAsync(e => { var res = await BarcodeScanner.GetDefaultAsync(); Assert(res != null); });

  • Subsequent page builders

Because of this, I suspect you should be focusing on accessing POS devices and the constructor et al are called before focus is obtained.

Q: Are there published directions on when you can call GetDefaultAsync()

?

+1


source to share


1 answer


Directly after Microsoft Sample BarcodeScanner I was unable to connect the barcode scanner in OnNavigatedTo

, although the call is a BarcodeScanner.GetDefaultAsync()

bit nested.

I said scanner in OnNavigatedTo

because the point of this particular page is to scan barcodes, if the scanner is not found or claimed for any reason, I want the error to be in advance. I don’t want the page to look and feel functional if it’s not, and I don’t want to force the user to try to scan before they know barcodescanner is not working.

I can't tell you why calling in different places didn't work in your particular case without seeing more of your code, but I suggest trying the following.



protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        EnableScanner();
    }

private async void EnableScanner()
    {
        if (await CreateDefaultScannerObject())
        {
            // after successful creation, claim the scanner for exclusive use and enable it so that data reveived events are received.
            if (await ClaimScanner())
            {
                Task<bool> AsyncSuccess = EnableClaimedScanner();
                bool x = await AsyncSuccess;
                if (x)
                {
                    HookUpEventsClaimedScanner();
                }
            }
        }
    }
        private async Task<bool> CreateDefaultScannerObject()
    {
        if (scanner == null)
        {
            UpdateOutput("Creating Barcode Scanner object.");
            scanner = await BarcodeScanner.GetDefaultAsync();

            if (scanner != null)
            {
                UpdateOutput("Default Barcode Scanner created.");
                UpdateOutput("Device Id is:" + scanner.DeviceId);
            }
            else
            {
                UpdateOutput("Barcode Scanner not found. Please connect a Barcode Scanner.");
                return false;
            }
        }
        return true;
    }

    private async Task<bool> EnableClaimedScanner()
    {
        bool result = false;
        try
        {
            await claimedScanner.EnableAsync();
            if (claimedScanner.IsEnabled)
            {
                claimedScanner.IsDecodeDataEnabled = true;
                UpdateOutput("ClaimedScanner is now Enabled.");
                result = true;
            }
            else
                UpdateOutput("ClaimedScanner wasn't Enabled.");
        }
        catch (Exception ex)
        {
            UpdateOutput( ex.Message);
        }
        return result;
    }

    public void HookUpEventsClaimedScanner()
    {
        claimedScanner.DataReceived += ScannerDataReceived;
        claimedScanner.ReleaseDeviceRequested += ScannerReleaseRequest;
    }

      

EDIT: I realize this question was over a year old, but I found it in researching for my own built-in Windows 8.1 barcode scanner, so I wanted to make sure it wasn't leading anyone down the wrong path, thinking it GetDefaultAsync

wouldn't work in certain call situations.

0


source







All Articles