jpos.JposException: This device cannot be claimed for exclusive access. Android Studio Error

148 views Asked by At

I'm trying to develop an app that let me print tickets with a thermal print.

I'm using a bixolon printer, the SRP-350III Model, but every time I try to connect it, I got the same error, this is the stack trace:

2023-12-13 12:37:42.409  7762-7858  System.err              com.gesplanet.gestion.TPVComercial   W  jpos.JposException: This device cannot be claimed for exclusive access.
2023-12-13 12:37:42.409  7762-7858  System.err              com.gesplanet.gestion.TPVComercial   W      at com.bxl.services.posprinter.POSPrinterBaseService.claim(POSPrinterBaseService.java:158)
2023-12-13 12:37:42.409  7762-7858  System.err              com.gesplanet.gestion.TPVComercial   W      at jpos.BaseJposControl.claim(BaseJposControl.java:284)
2023-12-13 12:37:42.409  7762-7858  System.err              com.gesplanet.gestion.TPVComercial   W      at com.gesplanet.gestion.TPVComercial.Bixolon.BixolonPrinter.printerOpen(BixolonPrinter.java:187)
2023-12-13 12:37:42.409  7762-7858  System.err              com.gesplanet.gestion.TPVComercial   W      at com.gesplanet.gestion.TPVComercial.PrinterConnectActivity

I'm following the example that bixolon includes with the SDK but the same is happening.

Any ideas?

This is the code im using to connect:

public boolean printerOpen(int portType, String logicalName, String address, boolean isAsyncMode) {
        if (setTargetDevice(portType, logicalName, BXLConfigLoader.DEVICE_CATEGORY_POS_PRINTER, address)) {
            int retry = 1;
            if (portType == BXLConfigLoader.DEVICE_BUS_BLUETOOTH_LE) {
                retry = 5;
            }
            //address = "/dev/bus/usb/001/004";
            address = logicalName;

            try {
                for (JposEntry entry: bxlConfigLoader.getEntries()) {
                    bxlConfigLoader.removeEntry(entry.getLogicalName());
                }

                Log.i("BixolonPrinter 163", "Iniciando datos: \n LogicalName: "+logicalName+" \n " +
                        "DeviceCategory: " + BXLConfigLoader.DEVICE_CATEGORY_POS_PRINTER+" \n" +
                        "ProductName: "  + BXLConfigLoader.PRODUCT_NAME_SRP_350III+" \n" +
                        "DeviceBus: "  + portType+" \n" +
                        "Address: " + address);

                bxlConfigLoader.addEntry(logicalName, BXLConfigLoader.DEVICE_CATEGORY_POS_PRINTER, BXLConfigLoader.PRODUCT_NAME_SRP_350III, portType, address);
                bxlConfigLoader.saveFile();
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }

            for (int i = 0; i < retry; i++) {
                try {
                    posPrinter.open(logicalName);
                    posPrinter.claim(5000 * 2);
                    posPrinter.setDeviceEnabled(true);
                    posPrinter.setAsyncMode(isAsyncMode);

                    mPortType = portType;
                    mAddress = address;
                    return true;
                } catch (JposException e) {
                    e.printStackTrace();
                    try {
                        posPrinter.close();
                    } catch (JposException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }
        return false;
    }
2

There are 2 answers

0
davidm On

If you are targeting Android 12 or higher then you should add and request the following permission in your AndroidManifest.xml.

<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />

Alongside with:

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
0
Huber Thomas On

In my case the outcoming app was not asking for granting permissions, so I had to allow them manually:

Android - Settings - Apps - select your app - scroll down to permissions and allow them all

Afterwards everything worked perfectly for me!