Hi,
I'm trying to print to a ZD420 label printer using Bluetooth from an Android app I am developing. I've been using the sample apps to guide me, but I can't get them to connect to the printer.
The following code throws a ConnectionException:
public class ZebraActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_zebra); String theBtMacAddress = "7C:EC:79:33:E3:89"; Context context = getApplicationContext(); sendZplOverBluetoothLe(theBtMacAddress, context); } 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 BluetoothConnectionInsecure(theBtMacAddress); // 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(); } }Results in Stacktrace:
08-08 12:45:40.879 24427-24445/xxxW/System.err: com.zebra.sdk.comm.ConnectionException: Could not connect to device: read failed, socket might closed or timeout, read ret: -1
08-08 12:45:40.879 24427-24445/xxx W/System.err: at com.zebra.sdk.comm.ConnectionA.open(Unknown Source)
08-08 12:45:40.879 24427-24445/xxx W/System.err: at com.zebra.sdk.comm.BluetoothConnection.open(Unknown Source)
08-08 12:45:40.879 24427-24445/xxxW/System.err: at com.hermesinnovationlab.hermesreturns.zebra.ZebraActivity$1.run(ZebraActivity.java:41)
08-08 12:45:40.879 24427-24445/xxx W/System.err: at java.lang.Thread.run(Thread.java:818)
08-08 12:45:40.879 24427-24445/xxx W/System.err: Caused by: com.zebra.sdk.comm.ConnectionException: read failed, socket might closed or timeout, read ret: -1
08-08 12:45:40.879 24427-24445/xxx W/System.err: at com.zebra.sdk.comm.internal.BluetoothInsecureZebraConnectorImpl.tryPublicApiWay(Unknown Source)
08-08 12:45:40.879 24427-24445/xxx W/System.err: at com.zebra.sdk.comm.internal.BluetoothInsecureZebraConnectorImpl.open(Unknown Source)
08-08 12:45:40.879 24427-24445/xxx W/System.err: ... 4 more
I'm running the app on a Samsung Galaxy A6 with Android 5.1.1.
The manifest has the following permissions set:
Can anyone offer me any advice as to how I can get this working? Event the Zebra sample app doesn't connect to the printer.
Cheers,Colin.
5 Replies
In my app I include both ZSDK_ANDROID_API.JAR and ZSDK_ANDROID_BTLE.JAR. Then, when pairing I detect whether the printer is Classic BT or BTLE and store that in a variable:
public void findMacAddr(String nfcData) {
int index = 0;
// The Bluetooth MAC Address from a Bluetooth Low Energy (BLE) printer will be identified by "&mBL".
// A Classic Bluetooth address will be identified by "&mB".
index = nfcData.indexOf("&mBL="); // See if this is a BLE connection.
if (index == -1)
{
printerType = "Bluetooth";
index = nfcData.indexOf("&mB="); // Not BLE, so it must be Classic Bluetooth
index += 4;
}
else
{
printerType = "BluetoothLE";
index += 5;
}
nfcMacAddress = nfcData.substring(index, index + 12); // Creates a substring containing only the MAC Address information
index = nfcData.indexOf("&s"); // Index is set to the Serial Number identifier
nfcSerialName = nfcData.substring(index + 3, index + 17); // Creates a substring containg only the Serial Number
index = nfcData.indexOf("&mW=");
nfcWifiAddress = nfcData.substring(index + 4, index + 16); // Creates a substring containing only the WIFI MAC Address information
index = nfcData.indexOf("&c=");
printerModel = nfcData.substring(index + 3, index + 7);
}
And then, when I go to print, I select the proper method to call:
public boolean connectAndPrint(String data, Context context) {
boolean result = false;
Connection conn = null;
if (printerType.equals("Bluetooth"))
{
conn = new BluetoothConnection(nfcMacAddress);
}
else if (printerType.equals("BluetoothLE"))
{
conn = new BluetoothLeConnection(nfcMacAddress, context);
}
if (conn == null) return false;
try {
conn.open();
conn.write(data.getBytes());
// Make sure the data got to the printer before closing the connection
Thread.sleep(500);
result = true;
} catch (Exception e) {
// Handle communications error here.
e.printStackTrace();
result = false;
} finally {
// Close the connection to release resources.
if (null != conn) {
try {
conn.close();
} catch (ConnectionException e) {
e.printStackTrace();
}
}
}
return result;
}
HOWEVER.... I find that I sometimes (and have not spent the time to investigate why) need to perform these lines BEFORE calling connectAndPrint():
BluetoothConnection conn = new BluetoothConnection(printer.getAddress());
printer.connectDevice(conn);
…..
printer.connectAndPrint(labelData);
I hope this helps.
Hi dear i m same devices and i got it same problem you how to solved if you solved that is problem if possible share it with me your solutions
thank you best redards
salih hanifeoglu
Is the ZSDK_ANDROID_BTLE_API.jar (library) still available? I cannot find it anywhere in the Zebra downloads. Also, ZSDK_ANDROID_API.jar (library) only has class files for Bluetooth and NOT Bluetooth low energy like the library I am attempting to locate.
Thank you
Hi Colin,
Your code looks fine. I'm guessing you either have Bluetooth disabled on the printer, or the printer is BTLE only. If you want me to check, reply with the full part number printed on the underside of the printer. You can enable Bluetooth using the Printer Setup Utilities | Zebra app on PC or Android. If the printer is BTLE, there is a separate Android BTLE library in the latest Link-OS SDK.
Hi Colin,
By the Zebra sample app, do you mean the one packaged with the Link-OS SDK? The Zebra demo app is throwing this error as well? When you are trying to use the demo, is your device already paired to the printer?