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
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 AsyncSuccess = EnableClaimedScanner();
bool x = await AsyncSuccess;
if (x)
{
HookUpEventsClaimedScanner();
}
}
}
}
private async Task 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 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.