When is it allowable to call `BarcodeScanner.GetDe

2019-03-01 06:38发布

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
  • The first page .Loaded
  • The first page OnNavigatedTo
  • The first page constructor

This doesn't seem to be an issue with DeviceCapabilities or drivers, as it will work perfectly if I call it from:

  • A button Click event handler
  • The first page's OnGotFocus
  • The first page's constructor if wrapped in:
    this.Dispatcher.RunIdleAsync(e => { var res = await BarcodeScanner.GetDefaultAsync(); Assert(res != null); });
  • Subsequent page's constructors

Which makes me suspect that you must have focus to access the POS devices, and the constructor et al. are being called prior to focus being received.

Question: Is there published guidance as to when you can call GetDefaultAsync()?

1条回答
你好瞎i
2楼-- · 2019-03-01 06:56

Loosely following Microsoft Sample BarcodeScanner I had no difficulty connecting a barcode scanner in OnNavigatedTo though the BarcodeScanner.GetDefaultAsync() call is a bit nested.

I claimed the scanner in the OnNavigatedTo because the point of this particular page is to scan barcodes if the scanner is not found/claimed for some reason I want an error upfront I do not want the page to look and feel functional if it is not and I do not want to force the user to attempt a scan before they find out that the barcodescanner is not working.

I cannot tell you why calling in different locations was not working 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 research for my own windows 8.1 embedded barcode scanner so I wanted to ensure it was not leading anyone else down the wrong path thinking GetDefaultAsync would not work in certain call situations.

查看更多
登录 后发表回答