【轉】Android印表機--沒有裝置驅動sdk,自己實現USB列印功能

來源:互聯網
上載者:User

標籤:引用   return   印表機   javadoc   comm   start   ssi   廠商   value   

原文:http://blog.csdn.net/johnwcheung/article/details/71576833

Android下的裝置調試,如果裝置提供了驅動,按照廠家的驅動調試即可;裝置未提供驅動,只能按照通用的方法進行調試。

對於智能POS、收銀機以及其他列印裝置,如果廠商不提供列印相關sdk,那麼列印功能怎麼實現呢?其實我們可以基於USB通訊機制,自己去實現列印驅動。

整個實現流程如下
  1. 初始化印表機:首先要擷取USB管理器;其次要註冊監聽USB裝置插拔變化和請求許可權的廣播;最後列出所有的USB裝置,並且都請求擷取USB許可權;

  2. 實現這個廣播接收器:當接收到請求許可權的廣播時,擷取USB裝置的引用,android系統會詢問你是否允許裝置訪問,預設為false;當允許了訪問之後,會判斷USB的引用是否為null, 如果不為空白,說明該裝置可以串連,否則提示裝置拒絕訪問;如果接收到已串連的列印裝置移除的廣播,則要關閉本次串連。

  3. 對於獲得許可權可使用的USB,我們將擷取裝置的功能集(UsbInterface)和通訊通道(UsbEndpoint),然後建立host與device的串連用來傳輸資料。

  4. 最後,我們在需要列印的地方調用上面封裝好的列印方法,就可以發送指令來控制已經建立串連的印表機了。

USB印表機實現
/** * USB印表機 * Created by john on 17-5-10. */public class USBPrinter {    private static final String ACTION_USB_PERMISSION = "com.usb.printer.USB_PERMISSION";    private static USBPrinter mInstance;    private Context mContext;    private UsbDevice mUsbDevice;    private PendingIntent mPermissionIntent;    private UsbManager mUsbManager;    private UsbDeviceConnection mUsbDeviceConnection;    private final BroadcastReceiver mUsbDeviceReceiver = new BroadcastReceiver() {        public void onReceive(Context context, Intent intent) {            String action = intent.getAction();            if (ACTION_USB_PERMISSION.equals(action)) {                synchronized (this) {                    UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);                    if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {                        mUsbDevice = usbDevice;                    } else {                        Toast.makeText(context, "Permission denied for device " + usbDevice, Toast.LENGTH_SHORT).show();                    }                }            } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {                if (mUsbDevice != null) {                    Toast.makeText(context, "Device closed", Toast.LENGTH_SHORT).show();                    if (mUsbDeviceConnection != null) {                        mUsbDeviceConnection.close();                    }                }            }        }    };    private USBPrinter() {    }    public static USBPrinter getInstance() {        if (mInstance == null) {            mInstance = new USBPrinter();        }        return mInstance;    }    /**     * 初始化印表機,需要與destroy對應     *     * @param context 上下文     */    public static void initPrinter(Context context) {        getInstance().init(context);    }    /**     * 銷毀印表機持有的對象     */    public static void destroyPrinter() {        getInstance().destroy();    }    private void init(Context context) {        mContext = context;        mUsbManager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);        mPermissionIntent = PendingIntent.getBroadcast(mContext, 0, new Intent(ACTION_USB_PERMISSION), 0);        IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);        filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);        mContext.registerReceiver(mUsbDeviceReceiver, filter);        // 列出所有的USB裝置,並且都請求擷取USB許可權        HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();        for (UsbDevice device : deviceList.values()) {            mUsbManager.requestPermission(device, mPermissionIntent);        }    }    private void destroy() {        mContext.unregisterReceiver(mUsbDeviceReceiver);        if (mUsbDeviceConnection != null) {            mUsbDeviceConnection.close();            mUsbDeviceConnection = null;        }        mContext = null;        mUsbManager = null;    }    /**     * 列印方法     * @param msg     */    public void print(String msg) {        final String printData = msg;        if (mUsbDevice != null) {            UsbInterface usbInterface = mUsbDevice.getInterface(0);            for (int i = 0; i < usbInterface.getEndpointCount(); i++) {                final UsbEndpoint ep = usbInterface.getEndpoint(i);                if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {                    if (ep.getDirection() == UsbConstants.USB_DIR_OUT) {                        mUsbDeviceConnection = mUsbManager.openDevice(mUsbDevice);                        if (mUsbDeviceConnection != null) {                            Toast.makeText(mContext, "Device connected", Toast.LENGTH_SHORT).show();                            mUsbDeviceConnection.claimInterface(usbInterface, true);                            new Thread(new Runnable() {                                @Override                                public void run() {                                    byte[] bytes = printData.getBytes();                                    int b = mUsbDeviceConnection.bulkTransfer(ep, bytes, bytes.length, 100000);                                    Log.i("Return Status", "b-->" + b);                                }                            }).start();                            mUsbDeviceConnection.releaseInterface(usbInterface);                            break;                        }                    }                }            }        } else {            Toast.makeText(mContext, "No available USB print device", Toast.LENGTH_SHORT).show();        }    }}

【轉】Android印表機--沒有裝置驅動sdk,自己實現USB列印功能

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.