Android连接蓝牙打印机实现PDF文档的打印

目前网上教程与Demo介绍的都是蓝牙连接热敏打印机(pos机大小的打印机),如果想通过蓝牙连接日常所见到的打印机,进行打印,这些教程或Demo是做不到的。

目前Android的蓝牙并不支持BPP(Basic Printing Profile),所以在Android实现蓝牙打印,通过正常的手段是实现不了的。网上能够搜索的那些教程或demo我都试过了,Google Play上与打印相关的app,也都安装使用过,目前只有PrinterShare可以实现Word、PDF的打印。接下来的的内容就与这个软件有关。

由于Android本身并没有提供相关API,打印机厂商也没有提供Android的驱动,如果自己从头开始开发相关功能,会是一项非常浩大的工程。在经过一段时间的折腾与领导的不停催促后,我们决定使用PrinterShare来实现蓝牙打印功能,使用过支付宝的应该都知道,它会帮助我们安装一个快捷支付的APP,我采用的是相同的方法。我们的应用在使用打印功能时,首先判断PrinterShare是否安装,如果没有安装,就先安装该软件,如果已经安装,就调用PrinterShare的打印Activity,并且把文档的路径传递过去。
1.判断apk是否安装

1
2
3
4
5
6
7
8
public static boolean appIsInstalled(Context context, String pageName) {
  try {
    context.getPackageManager().getPackageInfo(pageName, 0);
    return true;
  } catch (NameNotFoundException e) {
    return false;
  }
}

2.安装apk

1
2
3
4
Intent intent = new Intent(Intent.ACTION_VIEW);
File file = FileUtils.getAssetFileToCacheDir(activity,"xxx.apk");
intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
activity.startActivity(intent);

3.把Asset下的apk拷贝到sdcard下 /Android/data/你的包名/cache 目录下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
publicstatic File getAssetFileToCacheDir(Context context, String fileName) {
  try {
    File cacheDir = FileUtils.getCacheDir(context);
    final String cachePath = cacheDir.getAbsolutePath()+ File.separator + fileName;
    InputStream is = context.getAssets().open(fileName);
    File file = new File(cachePath);
    file.createNewFile();
    FileOutputStream fos = new FileOutputStream(file);
    byte[] temp = newbyte[1024];

    int i = 0;
    while ((i = is.read(temp)) > 0) {
      fos.write(temp, 0, i);
    }
    fos.close();
    is.close();
    return file;
    } catch (IOException e) {
      e.printStackTrace();
    }
    return null; 
}

4.获取sdcard中的缓存目录

1
2
3
4
5
6
7
8
public static File getCacheDir(Context context) {
  String APP_DIR_NAME = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/";
  File dir = new File(APP_DIR_NAME + context.getPackageName() + "/cache/");
  if (!dir.exists()) {
    dir.mkdirs();
  }
  return dir; 
}

5.调用printershare打印pdf

1
2
3
4
5
6
7
8
Intent intent = new Intent();
ComponentName comp = new ComponentName("com.dynamixsoftware.printershare","com.dynamixsoftware.printershare.ActivityPrintPDF");
intent = new Intent();
intent.setComponent(comp);
intent.setAction("android.intent.action.VIEW");
intent.setType("application/pdf");
intent.setData(Uri.fromFile(pdf));
startActivity(intent);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值