0

我正在尝试开发一个 android 应用程序来检测 USB 设备,只要任何 USB 设备插入我的 android 平板电脑。但我的小程序总是无法检测到任何东西......我已经从网上下载了一个 USB 控制应用程序到我的平板电脑,它成功检测到 USB 设备。

正如我的想法,每当USB设备插入时,android内核都会广播信息,然后过滤器获取接收器的信息,对吗?下面的源代码是从我的第一个 android 应用修改的,所以它可能有一些不必要的代码。所有关于 USB 主机的源代码均来自 Developers.android.com。

public class MainActivity extends Activity implements OnClickListener {
TextView NTD;
TextView euros;
TextView usb1;
RadioButton ntoe;
RadioButton eton;
Button Convert;
UsbManager manager ;
UsbDevice intentUsbDv ;
UsbInterface mUsbIntf;

private static final String TAG = "UsbHostActivity";
private static final String ACTION_USB_PERMISSION =
        "android.mtp.MtpClient.action.USB_PERMISSION";
private PendingIntent mPermissionIntent;
private final static List<UsbDevice> mDevices = new ArrayList<UsbDevice>(); 

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {

    super.onCreate(icicle);
    setContentView(R.layout.activity_main);

    /*declare manager and filter*/
    manager = (UsbManager) getSystemService(Context.USB_SERVICE);

    mPermissionIntent = PendingIntent.getBroadcast(
            this, 0, new Intent(ACTION_USB_PERMISSION), 0);
    IntentFilter filter = new IntentFilter();
    filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
    filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
    filter.addAction(ACTION_USB_PERMISSION);
    /*text buttom declare*/
    NTD = (TextView)this.findViewById(R.id.NTD);
    euros = (TextView)this.findViewById(R.id.Euros);
    usb1 = (TextView)this.findViewById(R.id.usb1);
    ntoe = (RadioButton)this.findViewById(R.id.ntoe);
    ntoe.setChecked(true);
    eton = (RadioButton)this.findViewById(R.id.eton);
    Convert = (Button)this.findViewById(R.id.Convert);
    Convert.setOnClickListener(this);

    /*receiver register*/
    if( registerReceiver(mUsbReceiver, filter)==null)
    {
        euros.setText("1000.0");
    }

}

public void onClick(View v) {
    if (ntoe.isChecked()) {
       convertDollarsToEuros();
    }
    if (eton.isChecked()) {
       convertEurosToDollars();
    }

    USBtest();        
}
protected void convertDollarsToEuros() {
    double val;
    if(NTD.getText().length() == 0)
    {
        val = 0;
        NTD.setText("0.0");
    }
    else
        val = Double.parseDouble(NTD.getText().toString());
        // in a real app, we'd get this off the 'net
        euros.setText(Double.toString(val*0.67));
}
protected void convertEurosToDollars() {
    double val;
    if(euros.getText().length() == 0)
    {
        val = 0;
        euros.setText("0.0");
    }
    else
        val = Double.parseDouble(euros.getText().toString());
        // in a real app, we'd get this off the 'net
        NTD.setText(Double.toString(val/0.67));
}

private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

    public void onReceive(Context context, Intent intent) {
        NTD.setText("100.00");
        String action = intent.getAction();
        if (ACTION_USB_PERMISSION.equals(action)) {
            NTD.setText("10.0");
            synchronized (this) {
                UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

                if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                    if(device != null){
                        usb1.setText("found?!!");
                        usb1.setText(device.getDeviceName());
                        //call method to set up device communication
                        manager.requestPermission(device, mPermissionIntent);
                    }
                }
                else {
                    NTD.setText("1.0");
                    Log.d(TAG, "permission denied for device " + device);
                }
            }
        }
    }
};

protected void USBtest()
{
    UsbRequest usbReq;
    UsbEndpoint UsbEp = null;
    UsbDevice UsbDv = null;
    HashMap<String, UsbDevice> deviceList;
    Iterator<UsbDevice> itr ;

    if(manager == null)
        usb1.setText("fail!!!");
    else
    {
        deviceList = manager.getDeviceList();
        itr = deviceList.values().iterator();       
        while(itr.hasNext())
        {
            UsbDv = itr.next();
            if(UsbDv.getDeviceName().isEmpty())
                usb1.setText("No device");
            else
            {
                usb1.setText(UsbDv.getDeviceId());
            }
            //break;
         }          
         if(UsbDv==null){
             usb1.setText("None device");
         }
     }
};    

}

在资源文件“device_filter.xml”中

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <usb-device/>//accept all USB device
</resources>


in "manifest.xml":

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.Alex.testuiapp"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-feature android:name="android.hardware.usb.host" />
    <uses-sdk
    android:minSdkVersion="12"
    android:targetSdkVersion="18" />

    <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.Alex.testuiapp.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" />
        </intent-filter>
        <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
           android:resource="@xml/device_filter" />
        </activity>
    </application>
</manifest>

有人可以帮我吗?告诉我我的代码有什么问题或者我对 android 的想法有什么问题

根据下面的回复建议,我的平板电脑可能只有 OTG,有人知道如何识别设备是 USB OTG 还是 USB 主机?在文档中,我只能找到“开发人员选项,USB:调试,开发,设备ID,保持清醒,允许模拟,位置”

4

0 回答 0