I am trying to implement a barcode scanner feature in my uwp app, but the barcode scanner device is not being detected in my C# UWP application. Please help.

Jay 140 Reputation points
2024-08-08T12:36:30.2766667+00:00

This is sample code

_scanner = await BarcodeScanner.GetDefaultAsync(); this function always returns null

using Windows.Devices.PointOfService; 


private BarcodeScanner _scanner;
private ClaimedBarcodeScanner _claimedScanner;


private async void Button_Click(object sender, RoutedEventArgs e)
{
    // Find the first available barcode scanner
    _scanner = await BarcodeScanner.GetDefaultAsync();
    if (_scanner != null)
    {
        _claimedScanner = await _scanner.ClaimScannerAsync();
        if (_claimedScanner != null)
        {
            // Enable the scanner
            await _claimedScanner.EnableAsync();
            // Register for the DataReceived event to handle scanned barcodes
            _claimedScanner.DataReceived += ClaimedScanner_DataReceived;
        }
    }
    else
    {
        BarcodeDataTextBlock.Text = "No barcode scanner found.";
    }
}

 private async void ClaimedScanner_DataReceived(ClaimedBarcodeScanner sender, BarcodeScannerDataReceivedEventArgs args)
 {
     // Update UI with the scanned barcode data
     await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
     {
         BarcodeDataTextBlock.Text = $"Scanned Barcode: {args.Report.ScanDataLabel}";
     });
 }
Universal Windows Platform (UWP)
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,010 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.