Forum Xamarin.Forms

Barcode Scanners in Xamarin Forms

Hello,

I'm working on a Xamarin Forms Application that needs to interact with a ring scanner. I have an MC40 device which has the EMDK for Xamarin Android, but not for Xamarin Forms. I've seen the ZXing.Net.Mobile project, but it doesn't seem like it will work for a ring scanner. Just wondering if anyone else has run into this issue.

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Mc 40 is a??? motorola???

  • MV1430MV1430 USMember

    It is a Motorola

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    You should find something on GitHub, a @PietroMaggi example...

    here

    https://github.com/pfmaggi/Xam_BarcodeIntent/blob/master/ScannerBarcode/MainActivity.cs

    It's Android... you have only to send a MessageCenter to "App" when you have received the Barcode from Emdk

    in MainActivity

            _broadcastReceiver.scanDataReceived += (s, scanData) =>
            {
                editText.Text = scanData;
            };
    

    should become

        var my_application = new App ();
        _broadcastReceiver.scanDataReceived += (s, scanData) =>
        {
            MessagingCenter.Send<App, string> (my_application, "ScanBarcode", scanData);
    
        };
    
  • MV1430MV1430 USMember

    Ok, Thanks. I'll try that.

  • PietroMaggi.7823PietroMaggi.7823 ITUniversity

    Hi Mark,
    To use a ring scanner (is it an RS507?) with a MC40 (needs to be KitKat) you can use Zebra Xamarin EMDK's Barcode API that can help you have a better control of the connection.

    The application Alessandro linked to works using DataWedge Intents and the Profile API (to setup DataWedge). To use that sample with the Ring Scanner you probably need to modify the profile.

    Let me know if you need more information.

    Ciao
    ~Pietro

  • MV1430MV1430 USMember

    Pietro,

    The ring scanner my company is getting is currently in the mail (not sure what kind). The MC40 I have doesn't have KitKat on it either. I won't be using the DataWedge. This is kind of the set up I've got so far. It is a little buggy though:

    `using System;

    using Android.App;
    using Android.Content.PM;
    using Android.OS;
    using Symbol.XamarinEMDK;
    using Symbol.XamarinEMDK.Barcode;
    using System.Collections.Generic;
    using Xamarin.Forms;
    using MobileLogisticsCenter.Main;

    namespace MobileLogisticsCenter.Droid
    {
    [Activity(Label = "MobileLogisticsCenter", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, ScreenOrientation = ScreenOrientation.SensorLandscape)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity, EMDKManager.IEMDKListener
    {
    private EMDKManager emdkManager = null;
    private BarcodeManager barcodeManager = null;
    private Scanner scanner = null;

        public string ScannerStatus { get; set; }
    
        public void OnClosed()
        {
            ScannerStatus = "Status: EMDK Open failed unexpectedly. ";
    
            if (emdkManager != null)
            {
                emdkManager.Release();
                emdkManager = null;
            }
        }
    
        public void OnOpened(EMDKManager emdkManager)
        {
            ScannerStatus = "Status: EMDK Opened successfully ...";
            this.emdkManager = emdkManager;
    
            InitScanner();
        }
    
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
    
            EMDKResults results = EMDKManager.GetEMDKManager(Android.App.Application.Context, this);
    
            if (results.StatusCode != EMDKResults.STATUS_CODE.Success)
            {
                ScannerStatus = "Status: EMDKManager object creation failed ...";
            }
            else
            {
                ScannerStatus = "Status: EMDKManager object creation succeeded ...";
            }
    
            global::Xamarin.Forms.Forms.Init(this, bundle);
            LoadApplication(new MobileLogisticsCenter.Main.App());
        }
    
        protected override void OnResume()
        {
            base.OnResume();
            InitScanner();
        }
    
        protected override void OnPause()
        {
            base.OnPause();
            DeinitScanner();
        }
    
        protected override void OnDestroy()
        {
            base.OnDestroy();
    
            if (emdkManager != null)
            {
                emdkManager.Release();
                emdkManager = null;
            }
        }
    
        public void OnScanEvent(object sender, Scanner.DataEventArgs e)
        {
            ScanDataCollection scanDataCollection = e.P0;
    
            if ((scanDataCollection != null) && (scanDataCollection.Result == ScannerResults.Success))
            {
                IList<ScanDataCollection.ScanData> scanData = scanDataCollection.GetScanData();
    
                foreach (ScanDataCollection.ScanData data in scanData)
                {
                    string dataString = data.Data;
                    var app = new App();
                    MessagingCenter.Send<App, string>(app, "InitialScan", dataString);
                }
            }
        }
    
        public void OnStatusEvent(object sender, Scanner.StatusEventArgs e)
        {
            // EMDK: The status will be returned on multiple cases. Check the state and take the action.
            StatusData.ScannerStates state = e.P0.State;
    
            if (state == StatusData.ScannerStates.Idle)
            {
                ScannerStatus = "Scanner is idle and ready to submit read.";
                try
                {
                    if (scanner.IsEnabled && !scanner.IsReadPending)
                    {
                        scanner.Read();
                    }
                }
                catch (ScannerException e1)
                {
                    ScannerStatus = e1.Message;
                }
            }
            if (state == StatusData.ScannerStates.Waiting)
            {
                ScannerStatus = "Waiting for Trigger Press to scan";
            }
            if (state == StatusData.ScannerStates.Scanning)
            {
                ScannerStatus = "Scanning in progress...";
            }
            if (state == StatusData.ScannerStates.Disabled)
            {
                ScannerStatus = "Scanner disabled";
            }
            if (state == StatusData.ScannerStates.Error)
            {
                ScannerStatus = "Error occurred during scanning";
            }
        }
    
        #region Private Methods
        private void InitScanner()
        {
            if (emdkManager != null)
            {
                if (barcodeManager == null)
                {
                    try
                    {
                        // Get the feature object such as BarcodeManager object for accessing the feature.
                        barcodeManager = (BarcodeManager)emdkManager.GetInstance(EMDKManager.FEATURE_TYPE.Barcode);
    
                        scanner = barcodeManager.GetDevice(BarcodeManager.DeviceIdentifier.Default);
    
                        if (scanner != null)
                        {
                            // Attahch the Data Event handler to get the data callbacks.
                            scanner.Data += OnScanEvent;
    
                            // Attach Scanner Status Event to get the status callbacks.
                            scanner.Status += OnStatusEvent;
    
                            scanner.Enable();
    
                            // EMDK: Configure the scanner settings
                            ScannerConfig config = scanner.GetConfig();
                            config.SkipOnUnsupported = ScannerConfig.SkipOnUnSupported.None;
                            config.ScanParams.DecodeLEDFeedback = false;
                            config.ReaderParams.ReaderSpecific.ImagerSpecific.PickList = ScannerConfig.PickList.Enabled;
                            config.DecoderParams.Code39.Enabled = false;
                            config.DecoderParams.Code128.Enabled = true;
                            scanner.SetConfig(config);
                        }
                        else
                        {
                            // displayStatus("Failed to enable scanner.\n");
                        }
                    }
                    catch (ScannerException e)
                    {
                        // displayStatus("Error: " + e.Message);
                    }
                    catch (Exception ex)
                    {
                        // displayStatus("Error: " + ex.Message);
                    }
                }
            }
        }
    
        private void DeinitScanner()
        {
            if (emdkManager != null)
            {
                if (scanner != null)
                {
                    try
                    {
                        scanner.Data -= OnScanEvent;
                        scanner.Disable();
                    }
                    catch (ScannerException e)
                    {
                        // Log.Debug(this.Class.SimpleName, "Exception:" + e.Result.Description);
                    }
                }
    
                if (barcodeManager != null)
                {
                    emdkManager.Release(EMDKManager.FEATURE_TYPE.Barcode);
                }
                barcodeManager = null;
                scanner = null;
            }
        }
        #endregion
    }
    

    }

    `

    App.cs in Xamarin Forms Project:

    public App() { MessagingCenter.Subscribe<App, string>(this, "InitialScan", (sender, args) => { var obj = new object(); MessagingCenter.Send<object, string>(obj, EventTopic.ScanRead.ToString(), args); }); }

  • MV1430MV1430 USMember

    Do you know if you can update the MC40 to KitKat or do you have to get an updated device? The approach that I'm using above does work, but it is a little buggy. Could this be related to not using KitKat?

  • rouge1nirouge1ni GBMember ✭✭✭

    Did u get much futher with this my company is recomending a TC25 to company and now I dont like the look of their sdk
    Is the above working in production @MV1430

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    @rouge1ni do you have only to scan barcodes? You can use DataWedge with intents

  • rouge1nirouge1ni GBMember ✭✭✭

    @AlessandroCaliaro Yes I just like how xamrian forms is all clued together with events does this work in xamrian forms if so do you have an example thanks its the TC25 i belevie the company is ordering.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    You have to use Android Intent, so you have to do ALL in Android Project, and send "received data" to XF (I use MessagingCenter for this)

    http://techdocs.zebra.com/datawedge/6-6/guide/api/tutorials/

  • rouge1nirouge1ni GBMember ✭✭✭

    @AlessandroCaliaro Yes I just like how xamrian forms is all clued together with events does this work in xamrian forms if so do you have an example thanks its the TC25 i belevie the company is ordering. That is the shame i like how xmairan forms wires events up for me and all

  • rouge1nirouge1ni GBMember ✭✭✭

    Is there any scanners more comptable with xmarian than the zerbra at all ?

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Scanner compatible with Xamarin?

    Zebra offers an EMDK for Xamarin that works fine, that you can also use INTENT (that does not depending from Xamarin…)

    What do you need more?

    You can also use DataWedge in keyboard emulation...

  • rouge1nirouge1ni GBMember ✭✭✭

    @AlessandroCaliaro yes but I had most of my gui already done using xamrian forms now i will have to change it to use plain andriod

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    no, you have only to "receive the barcode" in Android, then send it to Xamarin Forms

Sign In or Register to comment.