TeoVr81
TeoVr81

Reputation: 1009

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.

Upvotes: 1

Views: 1029

Answers (2)

Alessandro Caliaro
Alessandro Caliaro

Reputation: 5768

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.

Upvotes: 0

Dan Siegel
Dan Siegel

Reputation: 5799

For starters stop using Messaging Center... you can and should instead use IEventAggregator. Remember that the Container is a public property of your App so you can actually use it to resolve dependencies like the IEventAggregator from platform code like:

// Remember that you may have an Application class already in scope 
// in the platform and may need to fully qualify the XF Application
var app = (App)Xamarin.Forms.Application.Current;
var eventAggregator = app.Container.Resolve<IEventAggregator>();

To use IEventAggregator you will need to create events that make sense for your application. These can contain a payload or not depending on your need.

// An event with no payload
public class FooEventA : PubSubEvent { }

// An event with a string payload
public class FooEventB : PubSubEvent<string> { }

You can then publish and subscribe as follows:

eventAggregator.GetEvent<FooEventB>().Publish("Some Payload");

eventAggregator.GetEvent<FooEventB>().Subscribe(OnFooEventBPublished);

private void OnFooEventBPublished(string payload)
{
   // Do stuff
}

Upvotes: 0

Related Questions