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
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}";
});
}
Sign in to answer