How to enable/disable the Scan Engine through Android Studio SDK

A common need for the developers that develop apps for Datalogic Mobile Computers is to enable/disable the Scan Engine in order, for example, to avoid accidental readings with the scanner in input field where it is not allowed.

The components responsible for reading barcode are substantially 3: Triggers, Wedges (Keyboard, Intent, Web…) and Listeners.

Trigger: are software or physical buttons to press to actually activate the scan engine. In code they are controlled through the Trigger interface that represents the trigger, see API reference here.

Listeners: they are components that you can declare in an application. These components are waiting to receive the code read by the Scan Engine, see API reference here.

Wedges: it is like an implementation of automatic Listener, outside the application, that forwards the read code for example (in the case of the Keyboard wedge) to the currently focused input field in the application, see API reference here.

The minimum condition in order to activate the scanner is that

  • At least one trigger is enabled (and pressed)
  • At least one listener is active, in listening for new codes.
    If one of these two conditions is not verified, the Scan Engine won’t turn on.

So, according to the application architecture you can use a combination of these to enable/disable the Scan Engine.

Method 1 (register/release all decode listeners):
So, for example, if you disable the Keyboard Wedge and the Listeners declared in your application, the Scan Engine will be disabled despite all the triggers are still Active
Datalogic Android Studio SDK provides configuration.keyboardWedge.enable.set() to enable/disable Keyboard Wedge, see API reference here

    import com.datalogic.decode.configuration.ScannerProperties; 
    ...

    ScannerProperties configuration = ScannerProperties.edit(manager);
    ...

    //disable
    configuration.keyboardWedge.enable.set(false);
    //store them
    int errorCode = configuration.store(manager, true);

    // Check return value.
    if(errorCode != ConfigException.SUCCESS) {
        //do somethingin in case of failure
    }

It provides also methods to remove/add Listeners, see API reference here :

import com.datalogic.decode.BarcodeManager;  
...

BarcodeManager barcode = new BarcodeManager();
...

 //release all listners registered
 barcode.release();

 //register listner
 // barcode.addReadListener(this)

Method 2 (enable/disable all triggers):
Another way to disable the Scan Engine is to disable the Triggers.
Datalogic Android Studio SDK provides the Trigger.setEnabled() method, see API reference here.

    import com.datalogic.device.input.KeyboardManager;
    import com.datalogic.device.input.Trigger;

    ......

    KeyboardManager keyManager = new KeyboardManager();
    for (Trigger trigger : keyManager.getAvailableTriggers()) {
        boolean result =  trigger.setEnabled(false); 

        if(result){
            //operation done
        }else{
            //do something in case of failure
        }
    }

At this link there is the a simple test app .apk that proves it, the app was tested on the Memor10.

Donato Cataldo
L3 - Mobile Products Specialist SW Engineer

3 Likes

Some customers asked how to do the same using Datalogic Xamarin SDK.
Into the Xamarin SDK the name of some classes can vary a little if compared to names used in the Java SDK, depending on the different syntax and naming convention used in the corresponding languages (Java, Kotlin or C#).

The Java SDK’s Trigger class, in Xamarin SDK is mapped by the ITrigger C# interface and the getAvailable Trigger() method, is mapped on the .AvailableTriggers read-only property.

If the purpose is to selectively allow the user to activate the Scan Engine, to enable/disable all the device triggers, this can be a possible implementation:

        //Datalogic Xamarin SDK
        private static void doEnableTriggers(bool enable)
        {
            KeyboardManager keyManager = new KeyboardManager();
            var triggers = keyManager.AvailableTriggers;
            foreach (var trigger in triggers)
            {
                trigger.SetEnabled(enable);
            }
        }

Simone Callegari
Datalogic Mobile - Products Specialist SW Engineer

1 Like

Is it possible to configure the Device, enabling and disabling DataWedge for exemple, using Intents instead of SDK?

1 Like

Hello @Gustavo_Oliveira,

With the device’s built-in functions, unfortunately is not possible to set the Keyboard-Wedge, or other ScanEngine parameters through intents.

That kind of configuration intent is part of an enhancement program included in the upcoming SDK 1.32 and indicatively planned for the end of the year, aimed at extending the com.datalogic.device.configuration namespace, the list of the available PropertyIDs, and the usability of the ACTION_CONFIGURATION_COMMIT intent already available in the platform.

Today, to bridge the gap, you should write a BroadcastReceiver that invokes the corresponding SDK method for you. You can see the official reference documentation here: Broadcasts overview  |  Android Developers

Something like:

public class MyReceiver extends BroadcastReceiver {
   @Override
   public void onReceive(Context context, Intent intent) {
      ScannerProperties configuration = ScannerProperties.edit(manager);
      configuration.keyboardWedge.enable.set( <true|false> );  //<enable/disable>
      int errorCode = configuration.store(manager, true);
      if(errorCode != ConfigException.SUCCESS) 
         //in case of failure do something 
}

Anyhow, waiting for the SDK 1.32, given the relatively common demand, I recommend that you get in touch with the Datalogic Technical Support, who depending on your specific needs can provide a more customized solution. You can issue a request on the company’s tech support portal available here .

Simone Callegari
Datalogic Mobile Products Specialist - L3 SW Engineer