Android USB打印

  1. 获取权限
    <uses-permission android:name="android.permission.USB_PERMISSION" />
    <uses-feature android:name="android.hardware.usb.host" />
    <uses-feature
        android:name="android.hardware.usb.accessory"
        android:required="false" />
  1. 注册广播
    private BroadcastReceiver initUsbReceiver() {
        return new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (ACTION_USB_PERMISSION2.equals(action)) {
                    synchronized (this) {
                        UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                        if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                            if (device != null) {
                                // 权限已授予
                            }
                        }
                    }
                }
            }
        };
    }
  1. 通用方法
    private static UsbInterface usbInterface = null;
    private Activity mContext;
    private static UsbDeviceConnection connection;

    /**
     * 获取usbDevice列表
     *
     * @return
     */
    public List<UsbDevice> getUsbDeviceList() {
        if (usbManager == null) {
            intManage();
        }
        HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
        if (deviceList.size() == 0) {
            CustomToast.showCustomToast(mContext, "暂无可连接USB设备");
            return null;
        }
        List<UsbDevice> usbDeviceList = new ArrayList<>();
        for (UsbDevice device : deviceList.values()) {
            if (device != null) {
                usbDeviceList.add(device);
            }
        }
        return usbDeviceList;
    }

	**
     * 连接usbDevice
     *
     * @param usbDevice
     */
    public void connectDevice(UsbDevice usbDevice, CompleteLisenter lisenter) {
        if (connection != null) {
            close(false);
        }
        if (usbDevice == null) {
            Log.i(TAG, "connectDevice:该USB设备不可用 ");
            CustomToast.showCustomToast(mContext, "该USB设备不可用");
            return;
        }
        showProgress("正在连接..."); // 展示progress
        connection = usbManager.openDevice(usbDevice);
        if (connection == null) {
            lisenter.onError("连接失败");
            return;
        }
        UsbPrintFactory.setCurUsbDevice(usbDevice); // 设置全局变量
        connection = usbManager.openDevice(usbDevice);
        usbInterface = usbDevice.getInterface(0);
        UsbEndpoint endpoint = usbInterface.getEndpoint(0);
        Log.i(TAG, "connectDevice: 已连接USB设备,设备vid: " + usbDevice.getVendorId() + ", pid: " + usbDevice.getProductId());
        dismissProgress(); // 关闭progress
        CustomToast.showCustomToast(mContext, "连接成功");
        lisenter.onComplete("连接成功"); // 成功回调
    }
  1. 打印
	/**
     * 打印
     * @param infos
     */
    public void printData(List<String> infos, CompleteLisenter lisenter) {
        UsbDevice usbDevice = UsbPrintFactory.getCurUsbDevice();
        List<UsbDevice> list = getUsbDeviceList();
        if (usbDevice == null) {
            CustomToast.showCustomToast(mContext, "还未连接设备,请先连接设备");
            return;
        }
        if (!list.contains(usbDevice)) {
            CustomToast.showCustomToast(mContext, "还未连接设备,请先连接设备");
            UsbPrintFactory.setCurUsbDevice(null);
            return;
        }
        if(connection == null){
            connection = usbManager.openDevice(usbDevice);
            usbInterface = usbDevice.getInterface(0);
            UsbEndpoint endpoint = usbInterface.getEndpoint(0);
        }
        // 连接设备
        connection.claimInterface(usbInterface, true);
        for (int i = 0; i < infos.size(); i++) {
            String str = "^XA\r\n" +
                    "^PW1000\r\n" +
                    "^LL800\r\n" +
                    "^LH0,0\r\n" +
                    "^MD10\r\n" +
                    "^FO50,50^GB700,200,9^FS\r\n" +
                    "^XZ";
            byte[] data = str.getBytes();
            connection.bulkTransfer(endpoint, data, data.length, 60000);
        }
        dismissProgress();
        lisenter.onComplete(""); // 成功回调
        CustomToast.showCustomToast(mContext, "打印成功");
    }

  1. 关闭连接
    public void close(Boolean showResult) {
        if (connection != null) {
            connection.releaseInterface(usbInterface);
            connection.close();
            connection = null;
            UsbPrintFactory.setCurUsbDevice(null); // 设置全局变量
            if (showResult) {
                CustomToast.showCustomToast(mContext, "已断开连接");
            }
        }
    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值