0

am trying to connect to a Zebra Printer, model ZDesigner GK420t, from an Android Application via USB using the new UsbConnection(), where the parameters assigned to USBConnection() constructor are as follows:

            UsbManager manager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);
        HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
        String DeviceName = "";
        for (String s : deviceList.keySet()) {
            DeviceName = s;
            break;
        }
        UsbDevice device = deviceList.get(DeviceName);
        connection = new UsbConnection(manager, device);
        connection.open();

However, the connection.Open() is causing the following Exception:

Exception Message: Could not connect to device: NullPointerException
Exception Class: com.zebra.sdk.comm.ConnectionException
Cause: java.lang.NullPointerException

What I tried so far to make sure that the assigned parameters as UsbManager and USB Device are not being handled to the UsbConnection() as Nulls is appending Toasts before the connection.Open() as follows, where the Toasts printed respectively "manager", "device":

Toast.makeText(mContext, manager == null ? "null manager" : "manager", Toast.LENGTH_LONG).show();
        Toast.makeText(mContext, device == null ? "null device" : "device", Toast.LENGTH_LONG).show();

Also made sure that the connection instantiated is not null by appending the Toast before connection.Open(), as follows, where the Toast printed connection:

Toast.makeText(mContext, connection == null ? "null connection== " : "connection", Toast.LENGTH_LONG).show();

Important Note: The property connectionAttributes of the "connection" object is being retrieved as null, one time the connectionAttribute property was assigned value of "Map" with length of 7, and the printer printed the buffer, trying to collect data about what was different during this connection.open() call when the connectionAttributes value was not retrieved as null.

Can't figure out what is causing the NullPointerException

Please help!

Yazan
  • 179
  • 2
  • 4
  • 10
  • Where in your code do you look for the GK420t printer? – Delphi Coder Feb 04 '20 at 09:29
  • Dear @Delphi Coder, haven't added a functionality to look for "GK420t" printer, the deviceList retrieves a HashMap entry with a keySet of "/dev/bus/usb/001/002", in which only retrieved when the Zebra Printer is plugged, then in my code assign the returned device from deviceList with keySet as "/dev/bus/usb/001/002" to the device variable in which in turn assigned to the UsbConnection() constructor corresponding parameter: "UsbDevice" – Yazan Feb 04 '20 at 09:35
  • OK, I was just wondering... But if more then one USB device is plugged in, your code could fail. – Delphi Coder Feb 04 '20 at 10:22
  • @Delphi Coder, correct, but only one usb device is plugged in, besides have added functionality to validate the looped deviceList item to have a setKey = "/dev/bus/usb/001/002" in which if validates for true only the looped HashMap device will be assigned to the UsbDevice "device" Variable – Yazan Feb 04 '20 at 10:51
  • Hi. Have you found some info about printing over USB? – Alessandro Caliaro Jun 22 '20 at 13:49

0 Answers0