Zebra ZD610 printer using Android to print


Audience

Hi,
I'm developing an Android app to print on a zebra linkOS 6.7 label printer device. Here is the code i'm using--

new Thread(new Runnable() {
public void run() {
Connection thePrinterConn = null;
try {
thePrinterConn = new BluetoothLeConnection(theBtMacAddress, context);
thePrinterConn.open();
thePrinterConn.write(zplData.getBytes());
Thread.sleep(1000);
} catch (final Exception e) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(context, "Error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});

} finally {
if (null != thePrinterConn) {
try {
thePrinterConn.close();
} catch (final Exception e) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d("TAG", e.getMessage());
Toast.makeText(context, "Error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});

}
} else {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(context, "No printer connection", Toast.LENGTH_SHORT).show();
}
});
}
}
}
}).start();

My problem is, it prints for a few times, and then gives a connection error and doesn't print.
Please let me know if there's a solution. Thanks in advance.

Submitted by James Swinton … on June 29, 2022 Permalink

Can you try adding this code to your activity and see if it produces the same result?

 

private void sendZplOverBluetoothLe(final String theBtMacAddress, final Context context) {
         new Thread(new Runnable() {
             public void run() {
                 Connection thePrinterConn = null;
                 try {
                     // Instantiate connection for given Bluetooth® MAC Address.
                     thePrinterConn = new BluetoothLeConnection(theBtMacAddress, context);
 
                     // Open the connection - physical connection is established here.
                     thePrinterConn.open();
 
                     // This example prints "This is a ZPL test." near the top of the label.
                     String zplData = "^XA^FO20,20^A0N,25,25^FDThis is a ZPL test.^FS^XZ";
 
                     // Send the data to printer as a byte array.
                     thePrinterConn.write(zplData.getBytes());
 
                     // Make sure the data got to the printer before closing the connection
                     Thread.sleep(500);
                 } catch (Exception e) {
                     // Handle communications error here.
                     e.printStackTrace();
                 } finally {
                     // Close the connection to release resources.
                     if (null != thePrinterConn) {
                         try {
                             thePrinterConn.close();
                         } catch (ConnectionException e) {
                             e.printStackTrace();
                         }
                     }
                 }
             }
         }).start();
     }