title: Unable to Open Connection to Zebra Printer
body: <p>I am currently in the process of creating an android application that requires the ability to print tickets/receipts from a mobile printer. The organization I am currently with has decided to purchase android tablets that are currently running 7.0 as well as Zebra Mobile Printers (ZQ510).  We determined that using the BluetoothConnectionInsecure would be the best method of connecting and printing with these printers, since the application will be used on a multitude of tablets each connecting to their own printers.</p>
  
  <p>Although we were able to successfully connect and print on the previous version of the application which was designed to work with android devices running 5.0 and above, it seems that the current version of our application is unable to establish a connection with the printer. </p>
  
  <p>I've looked through stack for a similar error, but was unable to find any. If there is one similar to mine that exists, I would appreciate a link.</p>
  
  <p><a href="https://stackoverflow.com/questions/36262106/zebra-printer-connection-failed-read-failed-socket-might-closed-or-timeout-re">Zebra printer connection failed “read failed, socket might closed or timeout, read ret: -1”</a></p>
  
  <p>Here is the stack trace of the error: 
  <code>
  01-18 14:24:08.332 9048-9165/marist.edu.mets_issue_ticket W/System.err: com.zebra.sdk.comm.ConnectionException: Could not connect to device: read failed, socket might closed or timeout, read ret: -1
  01-18 14:24:08.333 9048-9165/marist.edu.mets_issue_ticket W/System.err:     at com.zebra.sdk.comm.ConnectionA.open(Unknown Source)
  01-18 14:24:08.333 9048-9165/marist.edu.mets_issue_ticket W/System.err:     at com.zebra.sdk.comm.BluetoothConnection.open(Unknown Source)
  01-18 14:24:08.333 9048-9165/marist.edu.mets_issue_ticket W/System.err:     at marist.edu.mets_issue_ticket_helpers.BluetoothUnsecurePrint$1.run(BluetoothUnsecurePrint.java:308)
  01-18 14:24:08.333 9048-9165/marist.edu.mets_issue_ticket W/System.err:     at java.lang.Thread.run(Thread.java:761)
  01-18 14:24:08.333 9048-9165/marist.edu.mets_issue_ticket W/System.err: Caused by: com.zebra.sdk.comm.ConnectionException: read failed, socket might closed or timeout, read ret: -1
  01-18 14:24:08.333 9048-9165/marist.edu.mets_issue_ticket W/System.err:     at com.zebra.sdk.comm.internal.BluetoothInsecureZebraConnectorImpl.tryPublicApiWay(Unknown Source)
  01-18 14:24:08.333 9048-9165/marist.edu.mets_issue_ticket W/System.err:     at com.zebra.sdk.comm.internal.BluetoothInsecureZebraConnectorImpl.open(Unknown Source)
  </code></p>
  
  <p>The method I am currently using to establish connection with the printer is as follows:</p>
  
  <p>Creating an intent to search for discoverable devices, then passing that intent to my Bluetooth class:</p>
  
  <pre><code>Intent discoverableIntent =
                  new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
          discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
  
          BluetoothUnsecurePrint bluetoothUnsecurePrint = new BluetoothUnsecurePrint(resultMap,violationResults,getActivity(), theTicketNumber, discoverableIntent);
          bluetoothUnsecurePrint.print();
  </code></pre>
  
  <p>Gather the list of devices found and grab MAC address of devices already paired to the tablet:</p>
  
  <pre><code>    public void print(){
      //ask user to allow device to be discoverable by paired printer, display dialog box
   discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
      userActivity.startActivity(discoverableIntent);
      bluetoothAdapter = BluetoothAdapter.getDefaultAdapter().getAddress();
  
      BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
      if (mBluetoothAdapter == null) {
          // Device does not support Bluetooth
      }
  
      Set&lt;BluetoothDevice&gt; pairedDevices = mBluetoothAdapter.getBondedDevices();
  
      if (pairedDevices.size() &gt; 0) {
          // There are paired devices. Get the name and address of each paired device.
          for (BluetoothDevice device : pairedDevices) {
              String deviceHardwareAddress = device.getAddress(); // MAC address
              sendZplOverBluetooth(deviceHardwareAddress);
  
              Log.d(TAG,device.getAddress()+ device.getName()+device.getBondState());
  
              Intent ticketFinishedIntent = new Intent(userActivity, OffendingVehicleSearch.class);
              userActivity.startActivity(ticketFinishedIntent);
          }
      }
  }
  </code></pre>
  
  <p>Using the bluetooth mac address to establish a connection to the printer and then print the written data.</p>
  
  <pre><code>private void sendZplOverBluetooth(final String theBtMacAddress) {
  
      new Thread(new Runnable() {
          public void run() {
              try {
                  // Instantiate insecure connection for given Bluetooth&amp;reg; MAC Address.
                  Connection thePrinterConn = new BluetoothConnectionInsecure(theBtMacAddress);
  
                  // Initialize
                  Looper.prepare();
                  Log.d(TAG,thePrinterConn.getSimpleConnectionName());
  
                  // Open the connection - physical connection is established here.
                  thePrinterConn.open();
                  Log.d(TAG, String.valueOf(thePrinterConn.bytesAvailable()));
                  // Send the data to printer as a byte array.
                  thePrinterConn.write(generateZPL().getBytes());
  
                  // Make sure the data got to the printer before closing the connection
                  Thread.sleep(500);
  
                  // Close the insecure connection to release resources.
                  thePrinterConn.close();
  
                  Looper.myLooper().quit();
              } catch (Exception e) {
                  // Handle communications error here.
                  e.printStackTrace();
              }
          }
      }).start();
  }
  </code></pre>
  
  <p>I did come across some other semi-related forum posts concerning possible new permissions needed for the manifest file, however it looks like those permissions do not make a difference in the connection error.</p>
  
  <p>
      </p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.BLUETOOTH" /&gt;
  &lt;uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
  
  &lt;uses-permission android:name="android.permission.CAMERA" /&gt;
  
  &lt;uses-feature android:name="android.hardware.camera2.full" /&gt;
  
  &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  </code></pre>
  
  <p>Please let me know if any other information is needed.</p>