我正在一个Android项目上打印WINTEC Anypos 100上的订单,收据等。 本机具有内置USB打印机。 我本来是打印英文文本的苹果,但是当我尝试打印照片或其他语言文本时,它会打印垃圾。 我想知道是否有什么可以帮助您的?
遵循我的代码:
打印活动
public class PrinterActivity extends Activity
{
public final static String ACTION_USB_PERMISSION =
"com.example.usbprint.activity.USB_PERMISSION";
private UsbManager mUsbManager;
private UsbDevice mDevice;
private PendingIntent mPermissionIntent;
private UsbDeviceConnection connection;
private final USBReceiver mUsbReceiver = new USBReceiver(mUsbManager, mDevice, connection);
HashMap<String, UsbDevice> mDeviceList;
Iterator<UsbDevice> mDeviceIterator;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.print_activity);
Button print = (Button)findViewById(R.id.button);
TextView textView = (TextView) findViewById(R.id.textView);
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
mDeviceList = mUsbManager.getDeviceList();
mDeviceIterator = mDeviceList.values().iterator();
String usbDevice = "";
int i =0;
while (mDeviceIterator.hasNext())
{
UsbDevice usbDevice1 = mDeviceIterator.next();
usbDevice += "\n" +
"DeviceID: " + usbDevice1.getDeviceId() + "\n" +
"DeviceName: " + usbDevice1.getProductName() + "\n" +
"DeviceSubClass: " + usbDevice1.getDeviceSubclass() + "\n" +
"VendorID: " + usbDevice1.getVendorId() + "\n" +
"ProductID: " + usbDevice1.getProductId() + "\n";
int interfaceCount = usbDevice1.getInterfaceCount();
if(i==1)
{
mDevice = usbDevice1;
}
textView.setText(usbDevice);
i++;
}
createConn();
print.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
String msg = "Hello, \n" +
"This is a test message and its working!!!!! \n " +
"Following will be the information! \n " +
"Sincerely, \n " +
"Sondos Salama \n ";
String msg_ar = "اختبار عربي";
msg = msg + msg_ar;
printMessage(PrinterActivity.this, msg);
}
});
}
public void createConn()
{
mUsbManager = (UsbManager) this.getApplicationContext().getSystemService(Context.USB_SERVICE);
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
mUsbReceiver.setmUsbManager(mUsbManager);
mUsbReceiver.setmDevice(mDevice);
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
mUsbManager.requestPermission(mDevice, mPermissionIntent);
}
@SuppressLint("NewApi")
public void printMessage(Context context, String msg)
{
final String printdata = msg;
final UsbEndpoint mEndpointBulkOut;
mUsbManager = mUsbReceiver.getmUsbManager();
connection = mUsbReceiver.getConnection();
if(mDevice==null)
{
Log.e("ERROR:"," mDevice null ");
return;
}
if(mUsbManager==null)
{
Log.e("ERROR:"," mUsbManager null ");
return;
}
if(connection==null)
{
Log.e("ERROR:"," connection null ");
return;
}
if (mUsbManager.hasPermission(mDevice))
{
UsbInterface intf = mDevice.getInterface(0);
for (int i = 0; i < intf.getEndpointCount(); i++)
{
UsbEndpoint ep = intf.getEndpoint(i);
if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)
{
if (ep.getDirection() == UsbConstants.USB_DIR_OUT)
{
mEndpointBulkOut = ep;
connection = mUsbManager.openDevice(mDevice);
if(connection!=null)
{
Toast.makeText(context, "Device connected", Toast.LENGTH_SHORT).show();
}
boolean forceClaim = true;
connection.claimInterface(intf, forceClaim );
Integer res = null;
try
{
byte[] bytes = printdata.getBytes();
res = connection.bulkTransfer(mEndpointBulkOut, bytes, bytes.length, 100);
}
catch (Exception e)
{
e.printStackTrace();
}
break;
}
}
}
}
else
{
mUsbManager.requestPermission(mDevice, mPermissionIntent);
Toast.makeText(context, "Device have no permission", Toast.LENGTH_SHORT).show();
}
}
@SuppressLint("NewApi")
public void closeConnection(Context context)
{
BroadcastReceiver mUsbReceiver = new BroadcastReceiver()
{
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action))
{
UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (device != null)
{
Toast.makeText(context, "Device closed", Toast.LENGTH_SHORT).show();
connection.close();
}
}
}
};
}
@Override
public void onResume()
{
super.onResume();
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
}
@Override
public void onPause()
{
super.onPause();
unregisterReceiver(mUsbReceiver);
}
@Override
public void finish()
{
super.finish();
}
public void sendBroadcast()
{
Intent broadcast = new Intent();
broadcast.setAction(ACTION_USB_PERMISSION);
sendBroadcast(broadcast);
}
}
USB接收器
public class USBReceiver extends BroadcastReceiver
{
private UsbManager mUsbManager;
private UsbDevice mDevice;
private UsbDeviceConnection connection;
public UsbDeviceConnection getConnection() {
return connection;
}
public void setConnection(UsbDeviceConnection connection) {
this.connection = connection;
}
public USBReceiver()
{
}
public UsbManager getmUsbManager() {
return mUsbManager;
}
public void setmUsbManager(UsbManager mUsbManager) {
this.mUsbManager = mUsbManager;
}
public UsbDevice getmDevice() {
return mDevice;
}
public void setmDevice(UsbDevice mDevice) {
this.mDevice = mDevice;
}
public USBReceiver(UsbManager mUsbManager, UsbDevice mDevice, UsbDeviceConnection connection)
{
this.connection = connection;
this.mUsbManager = mUsbManager;
this.mDevice = mDevice;
}
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if (PrinterActivity.ACTION_USB_PERMISSION.equals(action))
{
synchronized (this)
{
mDevice = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false))
{
if(mDevice != null)
{
if(mUsbManager!=null)
{
connection = mUsbManager.openDevice(mDevice);
}
}
else
{
Log.e("Error:"," null Device");
}
}
else
{
Log.d("Error", "permission denied for device " + mDevice);
}
}
}
}
}
在代码中阿拉伯文字未正确打印。当我尝试发送图像的字节时,它打印了一些奇怪的符号和字符。