android打印功能,Android通过OTG线连接打印机实现打印功能
一、創建demo調用PrinterShare服務,雖然能找到相應的類和方法但調用后沒有反應
//打印圖片,進入PrinterShare打印圖片預覽界面
public static void printPicture(File file,Context context,AlertDialog.Builder builder){
Log.e("test", "printPicture");
if (!CheckAPK.appIsInstalled(context)) {
builder.show();
return;
}
Intent intent = new Intent();
ComponentName comp = new ComponentName("com.dynamixsoftware.printershare"
,"com.dynamixsoftware.printershare.ActivityPrintPictures");
intent.setComponent(comp);
intent.setAction("android.intent.action.VIEW");
intent.setType("application/picture");
intent.setData(Uri.fromFile(file));
context.startActivity(intent);
invokeAimMethod(context);
}
//調用目標方法
private static void invokeAimMethod(Context context){
try {
Context otherPackageContext = context.createPackageContext("com.dynamixsoftware.printershare",
Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
if (otherPackageContext != null) {
//載入類
Class> clazz = otherPackageContext.getClassLoader().loadClass("com.dynamixsoftware.printershare.ActivityPrintPictures");
if (clazz != null) {
//創建類的實例
Object obj = clazz.newInstance();
Class[] clazzs = {boolean.class};
Object[] objs = {false};
Thread.sleep(2000);
//調用PrinterShare的打印方法后打印機沒反應
invokeMethod(obj,"print",clazzs,objs);
} else {
Log.e("print","沒有獲取到類");
}
} else {
Log.e("print", "沒有獲取到其它包上下文");
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
Log.e("print", "NameNotFoundException");
} catch (ClassNotFoundException e) {
e.printStackTrace();
Log.e("print", "ClassNotFoundException");
} catch (InstantiationException e) {
e.printStackTrace();
Log.e("print", "InstantiationException");
} catch (IllegalAccessException e) {
e.printStackTrace();
Log.e("print", "IllegalAccessException");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 直接調用對象方法, 而忽略修飾符(private, protected, default)
// @param object : 子類對象
// @param methodName : 父類中的方法名
// @param parameterTypes : 父類中的方法參數類型
// @param parameters : 父類中的方法參數
// @return 父類中方法的執行結果
public static Object invokeMethod(Object object, String methodName,
Class> [] parameterTypes,Object [] parameters) {
//根據 對象、方法名和對應的方法參數 通過反射 調用上面的方法獲取 Method 對象
Method method = getDeclaredMethod(object, methodName, parameterTypes) ;
//抑制Java對方法進行檢查,主要是針對私有方法而言
method.setAccessible(true) ;
try {
if(null != method) {
//調用object 的 method 所代表的方法,其方法的參數是 parameters
return method.invoke(object, parameters);
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
// 循環向上轉型, 獲取對象的 DeclaredMethod
// @param object : 子類對象
// @param methodName : 父類中的方法名
// @param parameterTypes : 父類中的方法參數類型
// @return 父類中的方法對象
public static Method getDeclaredMethod(Object object, String methodName, Class> ... parameterTypes){
Method method;
for(Class> clazz = object.getClass() ; clazz != Object.class ; clazz = clazz.getSuperclass()) {
try {
method = clazz.getDeclaredMethod(methodName, parameterTypes) ;
return method ;
} catch (Exception e) {
//這里甚么都不要做!并且這里的異常必須這樣寫,不能拋出去。
//如果這里的異常打印或者往外拋,則就不會執行clazz = clazz.getSuperclass(),最后就不會進入到父類中了
}
}
return null;
}
二、不使用PrinterShare服務,雖然下面的方法可以和打印機建立連接但是并不能控制打印機,發送的命令打印機沒有反應。是否是需要打印機驅動,還有如何在Android中調用打印機驅動與打印機進行信息交流呢?
獲取手機外接usb設備
USBManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap deviceList = usbManager.getDeviceList();
List list;
Iterator> iterator = deviceList.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry2 = iterator.next();
list.add(entry2.getValue());
}
點擊列表獲取設備的UsbEndpoint
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
UsbEndpoint end_in, end_out;
UsbDevice usbDevice = list.get(position);
UsbInterface usbInterface = usbDevice.getInterface(0);
int j = usbInterface.getEndpointCount();
for (int i = 0; i < j; i++) {
// getEndpoint(index)對于指定的index獲得此接口的一個節點,返回一個UsbEndpoint
UsbEndpoint localUsbEndpoint = usbInterface.getEndpoint(i);
// getDirection返回的是一個整數,128(USB_DIR_IN)代表in,0(USB_DIR_OUT)代表out
if (localUsbEndpoint.getDirection() == 128) {
end_in = localUsbEndpoint;
} else if (localUsbEndpoint.getDirection() == 0) {
end_out = localUsbEndpoint;
}
}
}
});
請求臨時接入權限
private void requestPermission(UsbDevice paramUsbDevice) {
PendingIntent localPendingIntent = PendingIntent.getBroadcast(this,
0, new Intent(ACTION_USB_PERMISSION),PendingIntent.FLAG_UPDATE_CURRENT);
// USB設備請求臨時的接入權限
usbManager.requestPermission(paramUsbDevice, localPendingIntent);
}
建立連接,通過UsbEndpoint發送消息
UsbDeviceConnection connection = usbManager.openDevice(usbDevice);
if (end_out != null && connection != null) {
connection.claimInterface(usbInterface, true);
String cmdClear = "" + (char)29 + (char)86 + (char)66 + (char)0;
try {
output = cmdClear.getBytes("GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// bulkTransfer通過給定的endpoint來進行大量的數據傳輸,傳輸的方向取決于該節點的方向,
// 傳輸成果返回傳輸字節數組的長度,失敗返回負數
int flag = connection.bulkTransfer(end_out, output, output.length, 0);
if (flag < 0) {
Toast.makeText(LocalPrinterActivity.this, "傳輸失敗!",
Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(LocalPrinterActivity.this, "傳輸成功flag="+flag,
Toast.LENGTH_SHORT).show();
}
if (connection != null) {
connection.releaseInterface(usbInterface);
connection.close();
}
}
總結
以上是生活随笔為你收集整理的android打印功能,Android通过OTG线连接打印机实现打印功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android OTG-HID的连接与通
- 下一篇: Android 启用/禁用通过otg连接