项目地址:点击打开链接
基本思路:
1.创建usbManager;
2.获取到所有设备 选择出满足的设备;
3.查找设备接口;
4.获取设备endpoint;
5.打开conn连接通道;
6.发送打印信息。
注意点:
1.USB权限,在AndroidManifest.xml中添加:
<uses-feature android:name="android.hardware.usb.host" >
</uses-feature>
<uses-permission android:name="android.permission.HARDWARE_TEST" >
</uses-permission>
如果要想监听USB连接动作,在相应的activity加上如下
(qwm_usb_xml为要监听的设备信息,例如:<usb-device product-id="30507" vendor-id="2965" />):
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<!-- usb设备的动作 -->
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- android设备的信息过滤 -->
<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/qwm_usb_xml" >
</meta-data>
</activity>
2.必须USB设备先连接(此处为USB打印机),
usbManager.getDeviceList();
才能获得连接设备的信息。具体参数说明;
vendor-id (VID):(此处为USB打印机)产商id
product-id (PID):(此处为USB打印机)产品id
3.打印需要用的两个类,分别为:UsbDeviceConnection和UsbEndpoint;
贴上代码:
package com.tastykhana.usbprinter;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notificati