Search code examples
c#xamarin.formsprismbarcode-scanner

Suspend barcode scanner in Zebra TC51


I'm developing an application with Xamarin Forms and Prism. I'm using Zebra SDK to use barcode scanner hardware. I intercept the barcode scanning event with the MessagingCenter, I have the Send declaration in the MainActivity.cs:

Xamarin.Forms.MessagingCenter.Send<App, string>(_my_application, "Barcode", data.Data);

and I receive the scanned data in ViewModels with the subscription:

Xamarin.Forms.MessagingCenter.Subscribe<App, string>(this, "Barcode", (sender, arg) =>
                {
...

}

Now I need to stop the barcode scanner in case of functional errors. I usually show a pop-up message:

_pageDialogService.DisplayAlertAsync("Barcode Scanner", "My message", "OK");

and I want to enable again the scanner after that the user touch the "ok" button. (I want that this is a blocking message) How I can do that? The problem is that my barcode scanner code is in the MainActivity and I don't know how I can stop and resume the barcode scanner from the ViewModel code. The pop-up message is not enough because the scanner is triggered by the hardware button.


Solution

  • I think a simple solution is to use a boolean variable "isScannerWorking".

    In your Subscribe check isScannerWorking value

    Xamarin.Forms.MessagingCenter.Subscribe<App, string>(this, "Barcode", (sender, arg) =>
                    {
        if(isScannerWorking){
    
             // do something
        }
    
    }
    

    Set "isScannerWorking" to false before your _pageDialogService.DisplayAlertAsync("Barcode Scanner", "My message", "OK");and set to true after the DisplayAlertAsync. In this way, your scanner continue to read barcode but it does not use it.

    Otherwise I think you have to use DependencyService to call some SDK function to disable the scanner.