基本信息
源码名称:android usb 通讯实例源码下载
源码大小:1.30M
文件格式:.zip
开发语言:Java
更新时间:2014-08-08
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
源码在压缩包中,解压几次就能看到,android usb 通讯例子
源码在压缩包中,解压几次就能看到,android usb 通讯例子
package com.example.usbprinter;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.AssetManager;
import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
private UsbManager mManager;
private UsbDevice mDevice;
private UsbDeviceConnection mDeviceConnection;
private UsbInterface mInterface;
private PrinterDevice mPrinterDevice;
private static String LOG_TAG = "UsbPrinter";
AssetManager mAssetManager = null;
InputStream mInputStream = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//读取资源文件
mAssetManager = getAssets();
try {
mInputStream = mAssetManager.open("HelloWorld.PCL3GUI");
} catch (IOException e) {
e.printStackTrace();
}
mManager = (UsbManager) getSystemService(Context.USB_SERVICE);
for(UsbDevice device : mManager.getDeviceList().values()) {
UsbInterface intf = findPrinterInterface(device);
if (setPrinterInterface(device, intf)) {
break;
}
}
// listen for new devices
IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
registerReceiver(mUsbReceiver, filter);
}
BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
UsbDevice device = (UsbDevice) intent
.getParcelableExtra(UsbManager.EXTRA_DEVICE);
UsbInterface intf = findPrinterInterface(device);
if (intf != null) {
Log.i(LOG_TAG, "Found Printer interface " intf);
setPrinterInterface(device, intf);
}
} else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
UsbDevice device = intent
.getParcelableExtra(UsbManager.EXTRA_DEVICE);
String deviceName = device.getDeviceName();
if (mDevice != null && mDevice.equals(deviceName)) {
Log.i(LOG_TAG, "printer interface removed");
setPrinterInterface(null, null);
}
}
}
};
// searches for an adb interface on the given USB device
static private UsbInterface findPrinterInterface(UsbDevice device) {
Log.d(LOG_TAG, "findPrinterInterface " device);
int count = device.getInterfaceCount();
for (int i = 0; i < count; i ) {
UsbInterface intf = device.getInterface(i);
if (intf.getInterfaceClass() == UsbConstants.USB_CLASS_PRINTER) {
Log.i(LOG_TAG, "Printer");
return intf;
}
}
return null;
}
// Sets the current USB device and interface
private boolean setPrinterInterface(UsbDevice device, UsbInterface intf) {
if (mDeviceConnection != null) {
if (mInterface != null) {
mDeviceConnection.releaseInterface(mInterface);
mInterface = null;
}
mDeviceConnection.close();
mDevice = null;
mDeviceConnection = null;
}
if (device != null && intf != null) {
UsbDeviceConnection connection = mManager.openDevice(device);
if (connection != null) {
Log.i(LOG_TAG, "open succeeded");
if (connection.claimInterface(intf, false)) {
Log.i(LOG_TAG, "claim interface succeeded");
mDevice = device;
mDeviceConnection = connection;
mInterface = intf;
mPrinterDevice = new PrinterDevice(this, mDeviceConnection, intf);
Log.i(LOG_TAG, "call start");
mPrinterDevice.start();
return true;
} else {
Log.i(LOG_TAG, "claim interface failed");
connection.close();
}
} else {
Log.i(LOG_TAG, "open failed");
}
}
if (mDeviceConnection == null && mPrinterDevice != null) {
//mPrinterDevice.stop();
mPrinterDevice = null;
}
return false;
}
}