通过USB从Android应用程序将数据发送到PC

问题描述:

我想创建一个应用程序来通过USB从Android应用程序发送数据到PC。我的code如下:

I would like to create an App to send data through USB from Android App to PC. My Code as follows :

    package com.sample.dummy.app.senddatathoughserialport;

    import java.io.UnsupportedEncodingException;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.hardware.usb.UsbConstants;
    import android.hardware.usb.UsbDeviceConnection;
    import android.hardware.usb.UsbEndpoint;
    import android.hardware.usb.UsbInterface;
    import android.hardware.usb.UsbManager;
    import android.hardware.usb.UsbDevice;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;

public class MainActivity extends Activity 
{


@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final TextView tv = (TextView)findViewById(R.id.multiAutoCompleteTextView1);
    Button sButton = (Button) findViewById(R.id.button1);
    final UsbManager manager = (UsbManager) this.getSystemService(Context.USB_SERVICE);
    Toaster(""+manager);

    // -- register click event with first button ---
    sButton.setOnClickListener(new View.OnClickListener() 
    {
       @SuppressWarnings({ "unused" })
       public void onClick(View v) 
       {         
            try
            {
                Intent intent = new Intent("android.hardware.usb.action.USB_DEVICE_ATTACHED");
                UsbDevice mDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.ACTION_USB_DEVICE_ATTACHED);
                Toaster(""+mDevice);
                final UsbDeviceConnection connection = manager.openDevice(mDevice); 

                UsbInterface usbIf = null;
                Toaster("Its ok here 1");
                int count = mDevice.getInterfaceCount();
                Toaster("Its ok here 1");
                for(int i=0; i< count; i++)
                {
                    usbIf = mDevice.getInterface(i);
                }

                UsbEndpoint epIN = null;
                UsbEndpoint epOUT = null;
                Toaster("It's ok here 2");
                for (int i = 0; i < usbIf.getEndpointCount(); i++) 
                {            
                   if (usbIf.getEndpoint(i).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)
                   {                 
                        if (usbIf.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_IN)                         
                        epIN = usbIf.getEndpoint(i);
                        else
                        epOUT = usbIf.getEndpoint(i);
                   } 
                   else 
                   {
                        Log.d("USB","Not Bulk");
                        Toaster("Thedaa log daggara");
                   }
                }

                String get = tv.getText().toString();
                try
                {
                    byte[] str = get.getBytes(get);
                    connection.bulkTransfer(epOUT, str, str.length, 500);
                } 
                catch (UnsupportedEncodingException e) 
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Toaster("Data sending failed!");
                }
                finally
                {
                    Toaster("Data sent through USB !");
                }
            }
            catch(Exception e)
            {
                Log.d("Failed", e.toString());  
                Toaster("Attempt Failed !!");
            }
       }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
public void Toaster(String string)
{
    Toast.makeText(this, string,Toast.LENGTH_LONG).show();
}}

和我的Andr​​oid清单XML文件如下:

And my Android Manifest Xml file is as follows:

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 package="com.sample.dummy.app.senddatathoughserialport"
                                 android:versionCode="1"
                                android:versionName="1.0" >
 <uses-feature android:name="android.hardware.usb.host" />
 <uses-sdk android:minSdkVersion="12" />
 <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.sample.dummy.app.senddatathoughserialport.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>  
        <intent-filter>
            <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
            <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
        </intent-filter>

        <meta-data
            android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
            android:resource="@xml/device_filter" />
        <meta-data
            android:name="android.hardware.usb.action.USB_DEVICE_DETACHED"
            android:resource="@xml/device_filter" />        
    </activity>        
</application></manifest>

我得到的问题UsbDevice mDevice;它总是显示为空值 请给我的助手,使之成为成功的应用程序。

i'm getting problem with UsbDevice mDevice; which always shows value of null Please give me assistant to make it a successful app.

在我们的公司,我们使用下面的code与特定ID打开设备。同时它仅适用于OTG兼容的设备。在你的清单,你不需要USB_DETACHED,作为意向性过滤器清单是为了打开/启动你的应用程序,除非如果你想打开自己的apk当USB断开连接。只需拨打ConnectUSB上点击;

In our Company we use the following code to open a device with specific ID. Also It only works on OTG compatible devices. In your Manifest you don't need USB_DETACHED, as the intent-filters in manifest are meant to open/launch you app, unless if you want to open your apk when usb detach. Just call ConnectUSB on click;

    UsbInterface usbInterface;
    UsbEndpoint usbEndpointIN, usbEndpointOUT;
    UsbDeviceConnection usbDeviceConnection;
    UsbDevice deviceFound = null;
    USB USB;

    ArrayList<String> listInterface;
    ArrayList<UsbInterface> listUsbInterface;
    ArrayList<String> listEndPoint;
    ArrayList<UsbEndpoint> listUsbEndpoint;

    private static final int targetVendorID = 8260;
    private static final int ProductID = 1000;    

        private boolean connectUsb() {
            boolean result = false;     
            switch(checkDeviceInfo())
            {
                case ProductID:
                        result = StartUSB();
                    break;
            }
            return result;
        }

        private int checkDeviceInfo() {

            deviceFound = null;

            UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
            HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
            Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();

            while (deviceIterator.hasNext()) {
                UsbDevice device = deviceIterator.next();

                if(device.getVendorId()==targetVendorID) {
                    deviceFound = device;

                    switch(device.getProductId()) {
                        case ProductID:
                            GetInterface(deviceFound);
                            GetEndpoint(deviceFound);
                            return ProductID;

                        default:
                            Toast.makeText(this, getString(R.string.err_unknow_device), Toast.LENGTH_LONG).show();
                            break;
                    }
                    return -1;
                }
            }
            return -1;
        }

        private void GetInterface(UsbDevice d) {
            listInterface = new ArrayList<String>();
            listUsbInterface = new ArrayList<UsbInterface>();
            for(int i=0; i<d.getInterfaceCount(); i++){
                UsbInterface usbif = d.getInterface(i);
                listInterface.add(usbif.toString());
                listUsbInterface.add(usbif);
            }

            if(d.getInterfaceCount() > 0) 
            {   
                usbInterface = listUsbInterface.get(1);
            }
            else usbInterface = null;
        }

        private void GetEndpoint(UsbDevice d) {
            int EndpointCount = usbInterface.getEndpointCount();    
            listEndPoint = new ArrayList<String>();
            listUsbEndpoint = new ArrayList<UsbEndpoint>();

            for(int i=0; i<usbInterface.getEndpointCount(); i++) {
                UsbEndpoint usbEP = usbInterface.getEndpoint(i);
                listEndPoint.add(usbEP.toString());
                listUsbEndpoint.add(usbEP);
            }

            // deixar fixo para TxBlock USB
            if(EndpointCount > 0) {
                usbEndpointIN = usbInterface.getEndpoint(0);
                usbEndpointOUT = usbInterface.getEndpoint(1);
            }           
            else {
                usbEndpointIN = null;
                usbEndpointOUT = null;
            }
        }

        private boolean StartUSB() {
            boolean result = false;
            UsbDevice deviceToRead = deviceFound;
            UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);

            Boolean permitToRead = manager.hasPermission(deviceToRead);

            if(permitToRead) {
                result = OpenDevice(deviceToRead);
            }else {
                Toast.makeText(this, 
                        getString(R.string.err_no_permission) + permitToRead, 
                        Toast.LENGTH_LONG).show();
            }

            return result;
        }

        private boolean OpenDevice(UsbDevice device){

            boolean forceClaim = true;

            UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
            usbDeviceConnection = manager.openDevice(device);

            if(usbDeviceConnection != null){
                usbDeviceConnection.claimInterface(usbInterface, forceClaim);
                return true; 
            }else{
                Toast.makeText(this, 
                        getString(R.string.err_no_open_device), 
                        Toast.LENGTH_LONG).show();
            }

            return false;
        }