3

ペンドライブからファイルを読み取る必要があります。コードは次のとおりです。

private static final String ACTION_USB_PERMISSION = "android.hardware.usb.action.USB_ACCESSORY_ATTACHED";

private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
    @Override
    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);
                }
            }
        }
    }
};

私が使用するデバイスを検出するには:

mgr = (UsbManager) getSystemService(Context.USB_SERVICE);
devs = new HashMap<String, UsbDevice>();
devs = mgr.getDeviceList();

デバイスとインターフェイスを取得した後、次のようにエンドポイントを取得します。

public void getEndpointCount(View v) {
    itf = value.getInterface(0);
    endPointCount = itf.getEndpointCount();

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("No. of Endpoints: " + endPointCount)
            .setTitle("Endpoint Count").setCancelable(true)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
    AlertDialog alert = builder.create();
    alert.show();
    ((Button) findViewById(R.id.button4)).setEnabled(true);
}

public void getEndpointDirections(View v) {
    endPoint = null;


    UsbDeviceConnection conn = null;

    if (mgr.hasPermission(value)) {
        conn = mgr.openDevice(value);
        conn.claimInterface(itf, true);
    }

    String str = null;
    for (int i = 0; i < endPointCount; i++) {
        ((TextView) findViewById(R.id.tvLoopVal)).setText("" + i);
        endPoint = itf.getEndpoint(i);
        str = str + " Endpoint: " + i + " - " + endPoint.getDirection()
                + "\n";

if (endPoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
            ((TextView) findViewById(R.id.tvLoopVal)).setText("" + (i - 1));
            if (endPoint.getDirection() == UsbConstants.USB_DIR_IN) {
                Toast.makeText(this, "epin", Toast.LENGTH_SHORT).show();
                epIn = endPoint;
            } else {
                epOut = endPoint;
                Toast.makeText(this, "epout", Toast.LENGTH_SHORT).show();
            }
        }

    }

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(
            str + " Mac packet Size = " + endPointUsed.getMaxPacketSize())
            .setTitle("Endpoints Directions").setCancelable(true)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
    AlertDialog alert = builder.create();
    alert.show();

}

最後に、私が立ち往生しているエンドポイントを取得します。これは、データをペンドライブに読み書きするための次のステップです。私はこれについて手がかりを得ておらず、今どこを見るべきかについてのガイダンスが必要です.

また、USBデバイスをタブレットに接続していない場合でも、デバイスが1つ見つかったと表示され、OTGケーブルを介してペンドライブを接続すると、デバイスが2つ見つかったと表示されます!! それもおかしい

編集:

USBリクエストメソッドを使用してリクエストも初期化しましたが、それでもどのように機能するかわかりません

public void initializeRequest(View v) {
    UsbRequest uReq = new UsbRequest();
    Toast.makeText(this, "" + uReq.initialize(conn, epIn),
            Toast.LENGTH_SHORT).show();
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(
            " Is Request Initialized: = " + uReq.initialize(conn, epIn))
            .setTitle("Request Initialization").setCancelable(true)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
    AlertDialog alert = builder.create();
    alert.show();
    byte[] bb = new String("Hello").getBytes();
    ByteBuffer bBuffer = ByteBuffer.allocate(5);
    // bBuffer.put(bb);
    uReq.queue(bBuffer, 5);

    UsbRequest request = conn.requestWait();
    // Object obj = uReq.getClientData();
    // Log.v("Client Data", "" + obj.toString());
    if (request != null) {
        Toast.makeText(this, "Not null request", Toast.LENGTH_SHORT).show();
        Log.v("Response of request wait", ""
                + request.getClientData().toString());
        request.close();
    }
    uReq.close();

    // Log.v("Response of request wait", "" + conn.requestWait());
    // uReq.close();
}

エンドポイントが割り当てられていることを確認しましたが、このメソッドが実行されると、アプリケーションは待機するだけで何もしません。しばらくすると、Android からアプリを閉じるように求められます。requestWait() 関数のアプリケーションが何らかのイベントを待機するためですか?

編集

わかりました。ペン ドライブからの読み取りを忘れて、単に USB デバイスからオーディオ ストリームを取得する必要がある場合は、何も読み取らないマイクを例に挙げてみましょう。マイクが提供するものは何でもキャッチします。私はここで何をしましたか??

4

2 に答える 2

1

実際にデータを読み取っているようには見えません。デバイスからホストへの EndPoint がある場合は、 bulkTransferメソッドを使用してエンドポイントからデータを読み取ることができるはずです。

私は低レベルの USB 通信にはあまり詳しくありませんが、ファイル システムの解析を自分で処理する必要があると思います。ドライブの最初のセクター (512 バイト) を読み取り、そのセクターの最後の 2 バイトが 0x55,0xAA である場合、ドライブはおそらく FAT ファイル システムでフォーマットされており、そこから作業を進める必要があります。

于 2012-10-18T12:24:36.630 に答える