我目前正在创建一个Android应用程序,需要能够从移动打印机打印票据/收据。我目前所在的组织决定购买目前运行7.0的安卓平板电脑以及Zebra移动打印机(ZQ510)。我们确定使用BluetoothConnectionInsecure是连接和打印这些打印机的最佳方法,因为该应用程序将用于连接到自己的打印机的多个平板电脑上。
虽然我们能够成功连接并打印以前版本的应用程序,该应用程序旨在与运行5.0及更高版本的Android设备一起使用,但似乎我们的应用程序的当前版本无法与打印机建立连接。
我已经通过堆栈查找了类似的错误,但无法找到任何错误。如果有一个类似于我的,我会很感激链接。
Zebra printer connection failed “read failed, socket might closed or timeout, read ret: -1”
以下是错误的堆栈跟踪:
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)
我目前用于与打印机建立连接的方法如下:
创建搜索可发现设备的意图,然后将该意图传递给我的蓝牙类:
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();
收集找到的设备列表,并获取已与平板电脑配对的设备的MAC地址:
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<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 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);
}
}
}
使用蓝牙mac地址建立与打印机的连接,然后打印已写入的数据。
private void sendZplOverBluetooth(final String theBtMacAddress) {
new Thread(new Runnable() {
public void run() {
try {
// Instantiate insecure connection for given Bluetooth® 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();
}
我确实遇到过一些其他与半相关的论坛帖子,这些帖子涉及清单文件所需的可能新权限,但看起来这些权限不会对连接错误产生影响。
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera2.full" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
如果需要任何其他信息,请与我们联系。