一般用到的USB通訊都是USB Host模式。
一、這里首先需要在Manifest 和resource file中添加一些東西。
<manifest ...>
<uses-feature android:name="android.hardware.usb.host" />
<uses-sdk android:minSdkVersion="12" />
...
<application>
<activity ...>
...
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-device vendor-id="1234" product-id="5678" />
</resources>
vendor-id="1234" product-id="5678"這里是需要修改的。這里是USB所對應(yīng)的固定值。
二、使用設(shè)備Working with Devices
具體如何使用設(shè)備。
當(dāng)用戶將USB設(shè)備連接到Android設(shè)備時,Android系統(tǒng)需要申請權(quán)限,然后建立與設(shè)備的通信。
1.Discovering a device
上面已經(jīng)在activity中添加了intent-filter。
現(xiàn)在,首先定義獲取UsbDevice 。
UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
2.Obtaining permission to communicate with a device
請求用戶連接USB設(shè)備的權(quán)限。
在與USB設(shè)備進行通信之前,您的應(yīng)用程序必須獲得用戶的許可。
這里主要是用到了動態(tài)廣播。
先定義一個廣播BroadcastReceiver ,PendingIntent是一個打包Intent的類,用于發(fā)送intent。
要明確獲得許可,首先創(chuàng)建廣播接收器。該接收器偵聽當(dāng)您調(diào)用requestPermission()時獲得廣播的意圖。對requestPermission()的調(diào)用向用戶顯示一個對話框,要求用戶連接設(shè)備。以下示例代碼顯示如何創(chuàng)建廣播接收器:
private static final String ACTION_USB_PERMISSION =
"com.android.example.USB_PERMISSION";
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if(device != null){
//call method to set up device communication
}
}
else {
Log.d(TAG, "permission denied for device " + device);
}
}
}
}
};
要注冊廣播接收器,請在您的活動中的onCreate()方法中添加:
UsbManager mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
private static final String ACTION_USB_PERMISSION =
"com.android.example.USB_PERMISSION";
...
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
要顯示請求用戶連接到設(shè)備的權(quán)限的對話框,請調(diào)用requestPermission()方法:
UsbDevice device;
...
mUsbManager.requestPermission(device, mPermissionIntent);
mPermissionIntent是一個PendingIntent類調(diào)用getBroadcast函數(shù),打包了一個啟動對應(yīng)的動態(tài)BroadCast的intent的一個PendingIntent。
最后通過調(diào)用mUsbManager的requestPermission函數(shù),發(fā)送了BroadCastIntent,由于之前已經(jīng)注冊了動態(tài)廣播,所以,當(dāng)intent發(fā)送來的時候,BroadCastReceiver就會接受intent,然后執(zhí)行BroadCastReceiver中重寫的onReceive里面的內(nèi)容。
通常,onReceive這里面寫的一般是具體的通訊。
3.Communicating with a device
通過在相應(yīng)的接口端點上讀取和寫入數(shù)據(jù)與USB設(shè)備進行通信。
與USB設(shè)備的通信可以是同步或異步的。在任一情況下,您應(yīng)該創(chuàng)建一個新的線程來執(zhí)行所有數(shù)據(jù)傳輸,因此您不會阻止UI線程。要正確設(shè)置與設(shè)備的通信,您需要獲得要進行通信的設(shè)備的相應(yīng)UsbInterface和UsbEndpoint,并使用UsbDeviceConnection在此端點上發(fā)送請求。一般來說,您的代碼應(yīng)該:
private Byte[] bytes;
private static int TIMEOUT = 0;
private boolean forceClaim = true;
...
UsbInterface intf = device.getInterface(0);
UsbEndpoint endpoint = intf.getEndpoint(0);
UsbDeviceConnection connection = mUsbManager.openDevice(device);
connection.claimInterface(intf, forceClaim);
connection.bulkTransfer(endpoint, bytes, bytes.length, TIMEOUT); //do in another thread
參考鏈接:
https://developer.android.google.cn/guide/topics/connectivity/usb/host.html#working-d
http://www.cnblogs.com/sowhat4999/p/4439877.html
<receiver android:name=".StartupReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
public static final String ACTION_SHOW_NOTIFICATION =
"com.bignerdranch.android.photogallery.SHOW_NOTIFICATION";
IntentFilter filter = new IntentFilter(PollService.ACTION_SHOW_NOTIFICATION);
getActivity().registerReceiver(mOnShowNotification, filter);
注意,要傳入一個IntentFilter,必須先以代碼的方式創(chuàng)建它。這里創(chuàng)建的IntentFilter
同以下XML文件定義的filter是一樣的:
<intent-filter>
<action android:name="com.bignerdranch.android.photogallery.SHOW_NOTIFICATION" />
</intent-filter>
registerReceiver就相當(dāng)于在注冊清單中寫入了類似StartupReceiver的receiver。