ClaimedBarcodeScanner.DataReceived ClaimedBarcodeScanner.DataReceived ClaimedBarcodeScanner.DataReceived ClaimedBarcodeScanner.DataReceived ClaimedBarcodeScanner.DataReceived Event

Definition

Occurs when the device scans a barcode.

public : event TypedEventHandler<ClaimedBarcodeScanner, BarcodeScannerDataReceivedEventArgs> DataReceived
// Register
event_token DataReceived(TypedEventHandler<ClaimedBarcodeScanner, BarcodeScannerDataReceivedEventArgs> const& handler) const;

// Revoke with event_token
void DataReceived(event_token const& cookie) const;

// Revoke with event_revoker
DataReceived_revoker DataReceived(auto_revoke_t, TypedEventHandler<ClaimedBarcodeScanner, BarcodeScannerDataReceivedEventArgs> const& handler) const;
public event TypedEventHandler<ClaimedBarcodeScanner, BarcodeScannerDataReceivedEventArgs> DataReceived
Public Event DataReceived As TypedEventHandler (Of ClaimedBarcodeScanner, BarcodeScannerDataReceivedEventArgs)
function onDataReceived(eventArgs){/* Your code */}


claimedBarcodeScanner.addEventListener("datareceived", onDataReceived);
claimedBarcodeScanner.removeEventListener("datareceived", onDataReceived);

Examples

The following example shows how to setup the barcode scanner to receive data after a scanning event.

void Scenario1::ScenarioStartScanButton_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
     // Create the barcode scanner. 
    create_task(CreateDefaultScannerObject()).then([this](void)
    {
        if (scanner != nullptr)
        {
            // Claim the scanner for exclusive use by your application.
            create_task(ClaimScanner()).then([this](void)
            {
                if (claimedScanner)
                {
                        
                    // Add a release device requested event handler. If this event is not handled,  
                    // another app can claim the barcode scanner.
                    releaseDeviceRequestedToken = claimedScanner->ReleaseDeviceRequested::add(ref new EventHandler<ClaimedBarcodeScanner^>(this, &Scenario1::OnReleaseDeviceRequested));

  
                    /// Add a data receive event handler.
                    dataReceivedToken =  claimedScanner->DataReceived::add(ref new TypedEventHandler<ClaimedBarcodeScanner^, BarcodeScannerDataReceivedEventArgs^>(this, &Scenario1::OnDataReceived));
                    UpdateOutput("Attached the DataReceived Event handler.");

                    // Set the app to decode the raw data from the barcode scanner 
                    claimedScanner->IsDecodeDataEnabled = true;

                    // Enable the scanner.
                    create_task(EnableScanner()).then([this](void)
                    {
                        UpdateOutput("Ready to Scan.");

                        // Reset the button state
                        ScenarioEndScanButton->IsEnabled = true;
                        ScenarioStartScanButton->IsEnabled = false;
                    });
                }
            });
        }
    });

}
private async void ScenarioStartScanButton_Click(object sender, RoutedEventArgs e)
{
    // Create the barcode scanner. 
    if (await CreateDefaultScannerObject())
    {
        // Claim the scanner for exclusive use by your application.
        if (await ClaimScanner())
        {
            
            // Add a release device requested event handler. If this event is not handled,  
            // another app can claim the barcode scanner.
            claimedScanner.ReleaseDeviceRequested += claimedScanner_ReleaseDeviceRequested;

            // Add a data receive event handler.
            claimedScanner.DataReceived += claimedScanner_DataReceived;
            UpdateOutput("Attached the DataReceived Event handler.");

            // Set the app to decode the raw data from the barcode scanner 
            claimedScanner.IsDecodeDataEnabled = true;

            // Enable the scanner.
            if (await EnableScanner())
            {
                // Reset the button state
                ScenarioEndScanButton.IsEnabled = true;
                ScenarioStartScanButton.IsEnabled = false;

                UpdateOutput("Ready to Scan.");
            }
        } 
    }
    else
    {
        UpdateOutput("No Barcode Scanner found");
    }
}
void Scenario1::OnDataReceived(Windows::Devices::PointOfService::ClaimedBarcodeScanner ^sender, Windows::Devices::PointOfService::BarcodeScannerDataReceivedEventArgs ^args)
{
    // read the data from the buffer and convert to string.
    Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler( 
        [this,args]() 
    {
        DataReader^ scanDataLabelReader = DataReader::FromBuffer(args->Report->ScanDataLabel);
        ScenarioOutputScanDataLabel->Text = scanDataLabelReader->ReadString(args->Report->ScanDataLabel->Length);

        DataReader^  scanDataReader = DataReader::FromBuffer(args->Report->ScanData);
        ScenarioOutputScanData->Text = scanDataReader->ReadString(args->Report->ScanData->Length);

        ScenarioOutputScanDataType->Text = BarcodeSymbologies::GetName(args->Report->ScanDataType);
    }));
}
async void claimedScanner_DataReceived(ClaimedBarcodeScanner sender, BarcodeScannerDataReceivedEventArgs args)
{
    // update the UI with the data received from the scan.
    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
    {
        // read the data from the buffer and convert to string.
        var scanDataLabelReader = DataReader.FromBuffer(args.Report.ScanDataLabel);
        ScenarioOutputScanDataLabel.Text = scanDataLabelReader.ReadString(args.Report.ScanDataLabel.Length);

        var scanDataReader = DataReader.FromBuffer(args.Report.ScanData);
        ScenarioOutputScanData.Text = scanDataReader.ReadString(args.Report.ScanData.Length);

        ScenarioOutputScanDataType.Text = BarcodeSymbologies.GetName(args.Report.ScanDataType);

    }); 
}
//Creates the barcode scanner, claims it for exclusive use, adds a data event listener, 
//and enables it to receive data.
var _scanner = null;
var _claimedScanner = null;

function startReceivingData() {
    Windows.Devices.PointOfService.BarcodeScanner.getDefaultAsync().then(function (scanner) {
        if (scanner !== null) {
            _scanner = scanner;

            scanner.claimScannerAsync().done(function (claimedScanner) {
                if (claimedScanner !== null) {
                    _claimedScanner = claimedScanner;
                    claimedScanner.isDecodeDataEnabled = true;

                    claimedScanner.addEventListener("datareceived", onDataReceived);
                    claimedScanner.enableAsync().done(function () {

                        document.getElementById("btnStartReading").disabled = true;
                        document.getElementById("btnEndReading").disabled = false;
                    }, function error(e) {
                        // "Failed to enable scanner."
                    });

                } else {
                    //"Could not claim the scanner."
                }
            }, function error(e) {
                //"Could not claim the scanner."
            });

        } else {
            //"Barcode scanner not found. Connect a barcode scanner."
        }

    }, function error(e) {
        //"Asynchronous method failed."
    });
}
function onDataReceived(args) {
    var tempScanLabel = Windows.Storage.Streams.DataReader.fromBuffer(args.report.scanDataLabel).readString(args.report.scanDataLabel.length);
    var tempScanData = Windows.Storage.Streams.DataReader.fromBuffer(args.report.scanData).readString(args.report.scanData.length);
    var tempScanType = args.report.scanDataType;
    WinJS.logAppend("Got data",  "status");

    document.getElementById("scanDataId").textContent = tempScanType;
    document.getElementById("scanData").textContent = tempScanData;
    document.getElementById("scanDataLabel").textContent = tempScanLabel;

}

See also