0

AndroidタブレットにUSBデバイスが接続されるたびにUSBデバイスを検出するAndroidアプリを開発しようとしています。しかし、私のアプレットは常に何も検出できません...そして、WebからUSB制御アプリをタブレットにダウンロードしましたが、USBデバイスを正常に検出しました。

私の考えでは、AndroidカーネルはUSBデバイスが接続されるたびに情報をブロードキャストし、フィルターはレシーバーの情報を取得します、私は正しいですか? 以下のソース コードは、私の最初の 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