草庐IT

android - 批量传输返回 -1

coder 2023-12-16 原文

首先让我声明,我是 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;
                }
             }
        }

}

最佳答案

这是我混淆端点的问题。这是更正后的代码,现在我在获得回复时遇到问题,但我可能会创建另一篇文章。

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;
            }
         }
    }

}

关于android - 批量传输返回 -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15963402/

有关android - 批量传输返回 -1的更多相关文章

  1. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

    为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

  2. ruby - 检查字符串是否包含散列中的任何键并返回它包含的键的值 - 2

    我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案

  3. ruby - Ruby 中的隐式返回值是怎么回事? - 2

    所以我开始关注ruby​​,很多东西看起来不错,但我对隐式return语句很反感。我理解默认情况下让所有内容返回self或nil但不是语句的最后一个值。对我来说,它看起来非常脆弱(尤其是)如果你正在使用一个不打算返回某些东西的方法(尤其是一个改变状态/破坏性方法的函数!),其他人可能最终依赖于一个返回对方法的目的并不重要,并且有很大的改变机会。隐式返回有什么意义?有没有办法让事情变得更简单?总是有返回以防止隐含返回被认为是好的做法吗?我是不是太担心这个了?附言当人们想要从方法中返回特定的东西时,他们是否经常使用隐式返回,这不是让你组中的其他人更容易破坏彼此的代码吗?当然,记录一切并给出

  4. ruby-on-rails - ruby 日期方程不返回预期的真值 - 2

    为什么以下不同?Time.now.end_of_day==Time.now.end_of_day-0.days#falseTime.now.end_of_day.to_s==Time.now.end_of_day-0.days.to_s#true 最佳答案 因为纳秒数不同:ruby-1.9.2-p180:014>(Time.now.end_of_day-0.days).nsec=>999999000ruby-1.9.2-p180:015>Time.now.end_of_day.nsec=>999999998

  5. ruby - 从 String#split 返回的零长度字符串 - 2

    在Ruby1.9.3(可能还有更早的版本,不确定)中,我试图弄清楚为什么Ruby的String#split方法会给我某些结果。我得到的结果似乎与我的预期相反。这是一个例子:"abcabc".split("b")#=>["a","ca","c"]"abcabc".split("a")#=>["","bc","bc"]"abcabc".split("c")#=>["ab","ab"]在这里,第一个示例返回的正是我所期望的。但在第二个示例中,我很困惑为什么#split返回零长度字符串作为返回数组的第一个值。这是什么原因呢?这是我所期望的:"abcabc".split("a")#=>["bc"

  6. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  7. ruby - 为什么 Integer.respond_to?( :even? ) 返回 false? - 2

    我一直在研究RubyKoans,我发现about_open_classes.rbkoan很有趣。特别是他们修改Integer#even?方法的最后一个测试。我想尝试一下这个概念,所以我打开了Irb并尝试运行Integer.respond_to?(:even?),但令我惊讶的是我得到了错误。然后我尝试了Fixnum.respond_to?(:even?)并得到了错误。我还尝试了Integer.respond_to?(:respond_to?)并得到了true,当我执行2.even?时,我也得到了true。我不知道发生了什么。谁能告诉我缺少什么? 最佳答案

  8. ruby - Time.to_i 是否总是以 UTC 返回自 EPOCH 以来的秒数? - 2

    无论时间在哪个时区表示,时区差异是否总是被忽略?直觉上,对于那些使用UTC+2的人来说,从EPOCH开始经过的秒数应该更高。然而,事实并非如此。 最佳答案 Epoch基于utc时区https://en.wikipedia.org/wiki/Unix_time它与您当前所在的时区无关。 关于ruby-Time.to_i是否总是以UTC返回自EPOCH以来的秒数?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.

  9. ruby-on-rails - Ruby 流量控制 : throw an exception, 返回 nil 还是让它失败? - 2

    我在思考流量控制的最佳实践。我应该走哪条路?1)不要检查任何东西并让程序失败(更清晰的代码,自然的错误消息):defself.fetch(feed_id)feed=Feed.find(feed_id)feed.fetchend2)通过返回nil静默失败(但是,“CleanCode”说,你永远不应该返回null):defself.fetch(feed_id)returnunlessfeed_idfeed=Feed.find(feed_id)returnunlessfeedfeed.fetchend3)抛出异常(因为不按id查找feed是异常的):defself.fetch(feed_id

  10. ruby-on-rails - 如何让 Rails View 返回其关联的操作名称? - 2

    我有一个非常简单的Controller来管理我的Rails应用程序中的静态页面:classPagesController我怎样才能让View模板返回它自己的名字,这样我就可以做这样的事情:#pricing.html.erb#-->"Pricing"感谢您的帮助。 最佳答案 4.3RoutingParametersTheparamshashwillalwayscontainthe:controllerand:actionkeys,butyoushouldusethemethodscontroller_nameandaction_nam

随机推荐