Androidデバイス接続プリンタ
3970 ワード
Androidデバイスはプリンタに接続できる.
また、システムバージョンが高いほど、APIが完備する.
私の設備は4です.xの平板設備
印刷デバイスのコードを検出します.
..........
また、システムバージョンが高いほど、APIが完備する.
私の設備は4です.xの平板設備
印刷デバイスのコードを検出します.
// UsbManager
UsbDevice usbDevice = null;
UsbManager usbManager = (UsbManager) Application.getInstance().getSystemService(Service.USB_SERVICE);
HashMap deviceMap = usbManager.getDeviceList();
if (deviceMap.isEmpty()) {
if (BuildConfig.DEBUG) return;
return;
}
Iterator deviceIterator = deviceMap.keySet().iterator();
while (deviceIterator.hasNext()) {
/**
*device.getInterface(0).getInterfaceClass() == 7
*/
UsbDevice device = deviceMap.get(deviceIterator.next());
if (device.getDeviceClass() == 0 && device.getInterface(0).getInterfaceClass() == 7) {
// vid pid
usbDevice = device;
// usb Vid Pid id id
// vid,pid
if (1155 == device.getVendorId() && 208 == getProductId()) { // vid,pid
// usbDevice
} else if (2655 == device.getVendorId() && 1122 == getProductId()) {// vid,pid
// usbDevice
// , . , ,
// , SP , Sp , vid,pid
// , ,
}
}
// context UsbManager
UsbManager usbManager = (UsbManager) Application.getInstance().getSystemService(Service.USB_SERVICE);
if (usbManager.hasPermission(usbDevice)) {
//
openDevice();
} else {
PendingIntent pendingIntent = PendingIntent.getBroadcast(Application.getInstance(), 0, new Intent(
" .USB"), 0);
Application.getInstance().registerReceiver(permissionReceiver,
new IntentFilter(" .USB"));
usbManager.requestPermission(usbDevice, pendingIntent);
}
}
//
public void openDevice() {
UsbManager usbManager = (UsbManager) Application.getInstance().getSystemService(Service.USB_SERVICE);
usbDeviceConnection = usbManager.openDevice(usbDevice);
if (usbDeviceConnection == null) {
ToastUtil.showShort(" ");
return;
}
if (usbDeviceConnection.claimInterface(usbDevice.getInterface(0), true)) {
ToastUtil.showShort(" ");
} else {
ToastUtil.showShort(" ");
usbDeviceConnection.close();
usbDeviceConnection = null;
}
}
// UsbDeviceConnection bulkTransfer
usbDeviceConnection.bulkTransfer();
//
public void onDestroy() {
if (usbDeviceConnection != null) {
usbDeviceConnection.close();
usbDeviceConnection = null;
}
if (usbDevice != null) {
usbDevice = null;
}
if (usbEndOut != null) {
usbEndOut = null;
}
}
package android.hardware.usb;
public class UsbDeviceConnection {
.........
/**
* Performs a bulk transaction on the given endpoint.
* The direction of the transfer is determined by the direction of the endpoint.
*
* This method transfers data starting from index 0 in the buffer.
* To specify a different offset, use
* {@link #bulkTransfer(UsbEndpoint, byte[], int, int, int)}.
*
*
* @param endpoint the endpoint for this transaction
* @param buffer buffer for data to send or receive
* @param length the length of the data to send or receive
* @param timeout in milliseconds
* @return length of data transferred (or zero) for success,
* or negative value for failure
*/
public int bulkTransfer(UsbEndpoint endpoint,
byte[] buffer, int length, int timeout) {
return bulkTransfer(endpoint, buffer, 0, length, timeout);
}
..........
}