Saving connection details to use for auto connection

// Expert user has replied.
t tok yanru 3 years ago
5 1 0

Hi,
Im working on the Link OS SDK (previous generation) and can successfully connect via USB > select printer > print demo ZPL using the demo application.
the printer.Address = "/dev/bus/usb/001/002".
the model im using is the ZD420 with USB interface only.
Given the address, how can I create a Connection object using connection builder?
So that i can simply print the data after the first printer selection.

Please register or login to post a reply

1 Replies

S Steven Si

The ConnectionBuilder class does not support USB connection.
It's recommended to use UsbDiscover to find the USB connected printer, rather than using hardcoded USB connection string.

// Request for USB Host Permission
@Override
protected void findUsbPrinter() {
new Thread(new Runnable() {

public void run() {

// Find printer on USB
UsbDiscoveryHandler handler = new UsbDiscoveryHandler();
UsbDiscoverer.findPrinters(getApplicationContext(), handler);

try {
while (!handler.discoveryComplete) {
Thread.sleep(100);
}

if (handler.printers != null && handler.printers.size() > 0) {
discoveredPrinterUsb = handler.printers.get(0);

// Check for permission
if (!usbManager.hasPermission(discoveredPrinterUsb.device)) {
usbManager.requestPermission(discoveredPrinterUsb.device, permissionIntent);
} else {
hasPermissionToCommunicate = true;
}

} else {
// No printer found on USB.
}
} catch (Exception e) {
// Do something
}
}
}).start();
} 

// Handles USB device discovery
class UsbDiscoveryHandler implements DiscoveryHandler {
public List printers;
public boolean discoveryComplete = false;

public UsbDiscoveryHandler() {
printers = new LinkedList();
}

public void foundPrinter(final DiscoveredPrinter printer) {
printers.add((DiscoveredPrinterUsb) printer);
}

public void discoveryFinished() {
discoveryComplete = true;
}

public void discoveryError(String message) {
discoveryComplete = true;
}
}Once the USB connected printer is discovered, then we can get the connection as shown below. You can save discoveredPrinterUsb object for close/open later without discovering again.

Connection conn = discoveredPrinterUsb.getConnection();
 

CONTACT
Can’t find what you’re looking for?