6

最初に、私は Android 開発にまったく慣れていないので、USB は最初のアプリには少し複雑かもしれませんが、最初にアプリを作成したい唯一の理由です。

USB 経由で Garmin GPS と通信したいと考えています。私は過去にPCから成功しましたが、Garminが提供するドライバーを介していました.

私が知る限り、Android 用のドライバーは存在しないため、USB に直接書き込む必要があります。

Garmin は次のドキュメントを公開しています。

http://www8.garmin.com/support/pdf/USBAddendum.pdf

基本的に、バルクアウトで送信する必要があると言われています: 00 00 00 00 05 00 00 00 00 00 00 00

デバイスに転送の準備を指示します。そうすると、一括転送が -1 で失敗します。タイムアウトが 0 の場合、bulktransfer は返されません。GPSからの応答がないためだと思います。

以下に私のコードを含めました。コードが GPS を検出して開きます。しかし、最初のバルク転送は決して完了しません。一括送信エンドポイントに送信していることは確かです。誰でも始められますか?

    public class MainActivity extends Activity {

     private static final String TAG = "TestGarmin";
     private UsbManager mUsbManager;
     private UsbDevice mDevice;
     private UsbDeviceConnection mConnection;
     private UsbEndpoint mEndpointIntr;
     private UsbEndpoint mEndpointBulkOut;
     private UsbEndpoint mEndpointBulkIn;
     private static int TIMEOUT = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         mUsbManager = (UsbManager)getSystemService(Context.USB_SERVICE);
    }

    @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 onResume() {
            super.onResume();

            Intent intent = getIntent();
            Log.d(TAG, "intent: " + intent);
            String action = intent.getAction();
            String s = UsbManager.ACTION_USB_DEVICE_ATTACHED;           


            UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
            if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) 
                setDevice(device);

     }

     private void setDevice(UsbDevice device) {
            Log.d(TAG, "setDevice " + device);
            if (device.getInterfaceCount() != 1) {
                Log.e(TAG, "could not find interface");
                return;
            }
            UsbInterface intf = device.getInterface(0);
            // device should have three endpoints
            if (intf.getEndpointCount() != 3) {
                Log.e(TAG, "could not find endpoint");
                return;
            }           
            // endpoint 0 should be of type interrupt
            UsbEndpoint ep0 = intf.getEndpoint(0);
            if (ep0.getType() != UsbConstants.USB_ENDPOINT_XFER_INT) {
                Log.e(TAG, "endpoint 0 is not interrupt type");
                return;
            }


            if (ep0.getDirection() != UsbConstants.USB_DIR_IN) {

                Log.e(TAG, "endpoint 0 is not an input endpoint");
                return;             
            }


            mEndpointIntr = ep0;

            // endpoint 1 should be of type bulk
            UsbEndpoint ep1 = intf.getEndpoint(1);
            if (ep1.getType() != UsbConstants.USB_ENDPOINT_XFER_BULK) {
                Log.e(TAG, "endpoint 1 is not bulk type");
                return;
            }

            if (ep1.getDirection() != UsbConstants.USB_DIR_OUT) {

                Log.e(TAG, "endpoint 1 is not an output endpoint");
                return;             
            }

            mEndpointBulkOut = ep1;

            // endpoint 2 should be of type bulk
            UsbEndpoint ep2 = intf.getEndpoint(2);
            if (ep2.getType() != UsbConstants.USB_ENDPOINT_XFER_BULK) {
                Log.e(TAG, "endpoint 2 is not bulk type");
                return;
            }

            if (ep2.getDirection() != UsbConstants.USB_DIR_IN) {

                Log.e(TAG, "endpoint 2 is not an output endpoint");
                return;             
            }




            mEndpointBulkOut = ep2;


            mDevice = device;

            if (device != null) {
                UsbDeviceConnection connection = mUsbManager.openDevice(device);
                if (connection != null && connection.claimInterface(intf, true)) {
                    Log.d(TAG, "open SUCCESS");
                    mConnection = connection;

                    byte[] init = {0x00,0x00,0x00,0x00,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

                    //14 00 00 00 FE 00 00 00 00 00 00 00
                    int x = connection.bulkTransfer(mEndpointBulkOut,init, init.length, 100);

                    Log.e(TAG, "BulTransfer returned " + x);




                } else {
                    Log.d(TAG, "open FAIL");
                    mConnection = null;
                }
             }
        }

}
4

1 に答える 1

3

エンドポイントを混同するのは問題でした。これが修正されたコードです。現在、応答の取得に問題がありますが、おそらく別の投稿を作成します。

public class MainActivity extends Activity {

 private static final String TAG = "TestGarmin";
 private UsbManager mUsbManager;
 private UsbDevice mDevice;
 private UsbDeviceConnection mConnection;
 private UsbEndpoint mEndpointIntr;
 private UsbEndpoint mEndpointBulkOut;
 private UsbEndpoint mEndpointBulkIn;
 private static int TIMEOUT = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     mUsbManager = (UsbManager)getSystemService(Context.USB_SERVICE);
}

@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 onResume() {
        super.onResume();

        Intent intent = getIntent();
        Log.d(TAG, "intent: " + intent);
        String action = intent.getAction();
        String s = UsbManager.ACTION_USB_DEVICE_ATTACHED;           


        UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
        if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) 
            setDevice(device);

 }

 private void setDevice(UsbDevice device) {
        Log.d(TAG, "setDevice " + device);
        if (device.getInterfaceCount() != 1) {
            Log.e(TAG, "could not find interface");
            return;
        }
        UsbInterface intf = device.getInterface(0);
        // device should have three endpoints
        if (intf.getEndpointCount() != 3) {
            Log.e(TAG, "could not find endpoint");
            return;
        }           
        // endpoint 0 should be of type interrupt
        UsbEndpoint ep0 = intf.getEndpoint(0);
        if (ep0.getType() != UsbConstants.USB_ENDPOINT_XFER_INT) {
            Log.e(TAG, "endpoint 0 is not interrupt type");
            return;
        }


        if (ep0.getDirection() != UsbConstants.USB_DIR_IN) {

            Log.e(TAG, "endpoint 0 is not an input endpoint");
            return;             
        }


        mEndpointIntr = ep0;

        /*********   Endpoint 0 Bulk Out ************/
        // endpoint 1 should be of type bulk
        UsbEndpoint ep1 = intf.getEndpoint(1);
        if (ep1.getType() != UsbConstants.USB_ENDPOINT_XFER_BULK) {
            Log.e(TAG, "endpoint 1 is not bulk type");
            return;
        }

        if (ep1.getDirection() != UsbConstants.USB_DIR_OUT) {

            Log.e(TAG, "endpoint 1 is not an output endpoint");
            return;             
        }

        mEndpointBulkOut = ep1;


        /*************   Endpoint 3 Bulk in *************/
        // endpoint 2 should be of type bulk
        UsbEndpoint ep2 = intf.getEndpoint(2);
        if (ep2.getType() != UsbConstants.USB_ENDPOINT_XFER_BULK) {
            Log.e(TAG, "endpoint 2 is not bulk type");
            return;
        }

        if (ep2.getDirection() != UsbConstants.USB_DIR_IN) {

            Log.e(TAG, "endpoint 2 is not an input endpoint");
            return;             
        }                


        mEndpointBulkIn = ep2;


        mDevice = device;

        if (device != null) {
            UsbDeviceConnection connection = mUsbManager.openDevice(device);
            if (connection != null && connection.claimInterface(intf, true)) {
                Log.d(TAG, "open SUCCESS");
                mConnection = connection;

                byte[] init = {0x00,0x00,0x00,0x00,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00};





                //14 00 00 00 FE 00 00 00 00 00 00 00
                int x = connection.bulkTransfer(mEndpointBulkOut,init, init.length, 100);





                Log.e(TAG, "BulTransfer returned " + x);






            } else {
                Log.d(TAG, "open FAIL");
                mConnection = null;
            }
         }
    }

}
于 2013-04-12T15:15:35.887 に答える