同时为API 2.3提供啦一个USB通讯吧,这样也让2.3有啦USB通讯功能 不过只支持USBAccessory模式
下面为USB的相关代码 与 流程 :
<activity ...> ... <intent-filter> <actionandroid:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" /> </intent-filter> <meta-dataandroid:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"android:resource="@xml/accessory_filter" /> </activity>
2 . 获取一个usb管理者:
3. 获取 USBAccessory 当我们的设备连接到到另外的一个设备时 系统会给我们发送一个系统广播
4. 在这个广播中我们获取他的action如果action与UsbManager.EXTRA_ACCESSORY 匹配
5.当我们按下确定后, 我们会在次接到系统广播 ,来打开通信通道
以下为具体代码:
package hk.com.harbourlight.compones;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import com.android.future.usb.UsbAccessory;
import com.android.future.usb.UsbManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.library.Utility;
import android.os.ParcelFileDescriptor;
public class USBController {
// --------------------------------------------------
// ----- enums -----
// --------------------------------------------------
public enum ConnectionState {
Connecting, Connected, Disconnected
}
// --------------------------------------------------
// ----- constants -----
// --------------------------------------------------
public static final String ACTION_CONNECTIONSTATE_CHANGED = "BluetoothControllerConne ctionStateChangedAction";
private static final int TIMEOUT_DISCOVERY = 30000;
public final static String ACTION_USB_PERMISSION = "hk.com.harbourlight.USB_PERMISSION";
public final static String ACTION_OPENACCESSORY_FAIL="USBControllerOpenAccesso ryFail";
public final static String ACTION_SHOWTOAST="USBControllerShowToast";
// --------------------------------------------------
// ----- properties -----
// --------------------------------------------------
private final Context mContext;
private HarbourLightController mHarbourLightController;
private HarbourLightUpgradeContr oller mHarbourLightUpgradeCont roller;
private ConnectionState mConnectionState;
private Boolean mIsStopped;
private PendingIntent mPendingIntent;
private UsbManager mUsbManager;
private ParcelFileDescriptor mFileDescriptor;
private FileInputStream mInputStream;
private FileOutputStream mOutputStream;
private boolean mIsRequestingPermission;
//---------------------------------------------------
//----- extends --------
//---------------------------------------------------
public USBController(final Context context,final UsbManager usbManager){
this.mContext=context;
this.mUsbManager=usbManager;
mHarbourLightController = new HarbourLightController(mContext, null, null);
mConnectionState = ConnectionState.Disconnected;
mIsStopped = false;
mPendingIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0);
mHarbourLightUpgradeCont roller = new HarbourLightUpgradeContr oller(mContext, null, null);
}
public void start(){
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_USB_PERMISSION);
filter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
mContext.registerReceiver(mBroadcastReceiver, filter);
mIsStopped=false;
mIsRequestingPermission=false;
}
public void stop(){
mIsRequestingPermission=false;
mIsStopped=true;
mContext.unregisterReceiver(mBroadcastReceiver);
}
public void disConnect(){
mIsStopped=true;
mHarbourLightController.closeSession();
mConnectionState = ConnectionState.Disconnected;
mContext.sendBroadcast(new Intent(ACTION_CONNECTIONSTATE_CHANGED));
}
public HarbourLightController getHarbourLightControlle r(){
if(mHarbourLightController==null){
mHarbourLightController= new HarbourLightController(mContext, null, null);
}
return mHarbourLightController;
}
public HarbourLightUpgradeContr oller getHarbourLightUpgradeCo ntroller(){
if(mHarbourLightUpgradeCont roller==null){
mHarbourLightUpgradeCont roller = new HarbourLightUpgradeContr oller(mContext, null, null);
}
return mHarbourLightUpgradeCont roller;
}
public ConnectionState getConnectionState(){
return mConnectionState;
}
public boolean getIsRequrstingPermissio n(){
return mIsRequestingPermission;
}
public void setIsRequrstingPermissio n(final boolean isRequrstingPermission){
this.mIsRequestingPermission=isRequrstingPermission;
}
public boolean hasPermission(final UsbAccessory accessory){
return mUsbManager.hasPermission(accessory);
}
public void requestPermission(final UsbAccessory accessory){
synchronized (mBroadcastReceiver) {
mUsbManager.requestPermission(accessory, mPendingIntent);
}
}
public UsbAccessory[] getUsbAccessories(){
return mUsbManager.getAccessoryList();
}
//---------------------------------------------------
//----processes------
//---------------------------------------------------
public void openAccessory(final UsbAccessory usbAccessory){
try
{
new Thread(){
public void run(){
mFileDescriptor = mUsbManager.openAccessory(usbAccessory);
if (mFileDescriptor != null)
{
FileDescriptor fd = mFileDescriptor.getFileDescriptor();
if(fd!=null){
mInputStream = new FileInputStream(fd);
mOutputStream = new FileOutputStream(fd);
if(mInputStream!=null && mOutputStream!=null){
mHarbourLightController = new HarbourLightController(mContext, mInputStream, mOutputStream);
mContext.sendBroadcast(Utility.creatBroadCastReceiverIn tent(USBAccessoryController.ACTION_USB_UPDATEDATA_FILEOUTPUTSTREAM, "open Accessory"));
// mHarbourLightController.openSession();
// mHarbourLightController.handshake();
mContext.sendBroadcast(Utility.creatBroadCastReceiverIn tent(HarbourLightController.ACTION_OPENSESSIONSUCCESS, "open Session success"));
mHarbourLightUpgradeCont roller = new HarbourLightUpgradeContr oller(mContext, mInputStream, mOutputStream);
mHarbourLightUpgradeCont roller.openSession();
int timeOut=0;
while (!mIsStopped && timeOut < 10000) {
mContext.sendBroadcast(Utility.creatBroadCastReceiverIn tent(ACTION_CONNECTIONSTATE_CHANGED, "wait connect"));
// if (mHarbourLightController.isConnected()) {
if(mHarbourLightUpgradeCont roller.isConnected()){
mConnectionState = ConnectionState.Connected;
mContext.sendBroadcast(Utility.creatBroadCastReceiverIn tent(ACTION_CONNECTIONSTATE_CHANGED, "connect"));
return;
}
try
{
Thread.sleep(500);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
timeOut += 500;
}
}
}
}else {
mContext.sendBroadcast(new Intent(ACTION_OPENACCESSORY_FAIL));
}
}
}.start();
}
catch (Exception e)
{
}
}
private void closeAccessory() {
try {
if (mFileDescriptor != null) {
mFileDescriptor.close();
}
if (mHarbourLightController != null) {
mHarbourLightController.closeSession();
}
if (mInputStream != null) {
mInputStream = null;
}
if (mOutputStream != null) {
mOutputStream = null;
}
mHarbourLightController.closeSession();
disConnect();
setIsRequrstingPermissio n(false);
mConnectionState=ConnectionState.Disconnected;
mContext.sendBroadcast(new Intent(ACTION_SHOWTOAST));
}
catch (final Exception ex) {
}
}
//--------------------------------------------------
//--------- BroadcastReceiver --------
//--------------------------------------------------
private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action=intent.getAction();
if(action.equalsIgnoreCase(ACTION_USB_PERMISSION)){
synchronized (this) {
UsbAccessory accessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
if(accessory==null){
accessory=getUsbAccessories()[0];
}
if(accessory!=null){
if(hasPermission(accessory)){
openAccessory(accessory);
}
}
}
}else if (action.equalsIgnoreCase(UsbManager.ACTION_USB_ACCESSORY_DETACHED)) {
closeAccessory();
}
}
};
}
与设备的通讯需要得到设备的识别 不然该设备是不会识别你的手机的