Printing from Android to a Bluetooth Zebra printer without pairing

P Pietro Francesco Maggi 3 years 7 months ago
530 1 0

I'm looking for a way to print from an Android device to a Zebra printer over bluetooth without previously pairing it.
 
The printer does not have an authentication PIN so pairing seems to have issues with the Android interface: https://code.google.com/p/android/issues/detail?id=26041
 
I've done some testing using the createInsecureRfcommSocketToServiceRecord API with mixed results. Using below code I'm able to print on the MZ320 from Android v4.1.2 devices but the same program fails on Android v4.4.3. So far I've tested:
 

MC40 JB v4.1.2 - Works
TC55 JB v4.1.2 - Works
TC70 KK v4.4.3 - Printer lights up blue led signaling a Bluetooth request but no printing
Nexus 7 (2012) KK v4.4.3 - Printer lights up blue led signaling a Bluetooth request but no printing

 
The only difference in the logs running the app is that I get this warning on KK devices:
11-06 12:10:05.824    2136-2178/com.pietromaggi.sample.printing W/BluetoothAdapter﹕ getBluetoothService() called with no BluetoothManagerCallback   11-06 12:10:05.834    2136-2178/com.pietromaggi.sample.printing D/BluetoothSocket﹕ connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[59]}  But I don't think that this is what generate the issue:
core/java/android/bluetooth/BluetoothAdapter.java - platform/frameworks/base - Git at Google
 
Any idea/suggestion?
 
~Pietro
 
public void pairPrinter()  {           final UUID SerialPortServiceClass_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");           final BluetoothAdapter BA = BluetoothAdapter.getDefaultAdapter();           final String PrinterBsid = "00:22:58:0E:E7:87"; // This is My Printer Bluetooth MAC Address                 Thread t = new Thread(new Runnable() {               @Override               public void run() {                   OutputStream sOut;                   BluetoothSocket socket;                   BA.cancelDiscovery();                         BluetoothDevice BD = BA.getRemoteDevice(PrinterBsid);                   try {                       socket = BD.createInsecureRfcommSocketToServiceRecord(SerialPortServiceClass_UUID);                   } catch (IOException e) {                       return;                   }                         if (!socket.isConnected()) {                       try {                           socket.connect();                           sOut = socket.getOutputStream();   //                        sOut.write(("Hello World\n").getBytes());                           String cpclData = "! 0 200 200 210 1\r\n"                                   + "TEXT 4 0 30 40 This is a CPCL test.\r\n"                                   + "FORM\r\n"                                   + "PRINT\r\n";                           sOut.write(cpclData.getBytes());                           sOut.close();                       } catch (IOException e) {                           e.printStackTrace();                       }                   }                         try {                       Thread.sleep(1000);                       socket.close();                       BA.cancelDiscovery();                   } catch (IOException e) {                       e.printStackTrace();                   } catch (InterruptedException e) {                       e.printStackTrace();                   }               }           });                 t.start();       } 

Please Register or Login to post a reply

1 Replies

E Efkan YILMAZ

Hi Pietro,
You have the right idea here.  The issue is the way Android handles pairing with devices using Bluetooth standards 2.0 and below.  The only way to send info to these devices is to enter a pin on the device, or not pair at all and open a straight insecure socket to it.
Your code looks fine, but apparently KK made a small change to the socket creation method. Try this for lines 15-20 of your sample:

BluetoothDevice BD = BA.getRemoteDevice(PrinterBsid);
try {
    socket = BD.createRfcommSocketToServiceRecord();
} catch (Exception e) {Log.e("","Error creating socket");}

try {
    socket.connect();
    Log.e("","Connected");
} catch (IOException e) {
    Log.e("",e.getMessage());
    try {
          Log.e("","trying fallback...");
          socket =(BluetoothSocket) BD.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(BD,1);
    }
    catch (Exceptio e2) {
          Log.e("",e2.getMessage());    }
}
You might still get the error, but you can ignore it and create the socket now.

Hope this works for you.

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