2012-12-20 27 views
17

bulkTransfer işlevini kullanarak bir kamera ve bir Android tablet aygıtı arasında veri/komut alışverişi yapmak istiyorum. Bu Etkinliği yazdım, ancak bulkTransfer yöntemi -1 (hata durumu) değerini döndürür. Neden bu hatayı geri veriyor?USB bulkTransfer Android tablet ve kamera arasında

public class MainActivity extends Activity { 

    private TextView text; 
    private int TIMEOUT = 1000; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     text = (TextView) findViewById(R.id.txt); 
     usbTest(); 
    } 

    private void usbTest() { 
     UsbDevice device = (UsbDevice) getIntent().getParcelableExtra(
                     UsbManager.EXTRA_DEVICE); 
     if (device == null) 
      text.setText("device null"); 
     else 
      text.setText("device not null"); 

     UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE); 
     HashMap<String, UsbDevice> deviceList = manager.getDeviceList(); 
     text.setText(text.getText() + "\nDevices connected: " 
        + deviceList.values().size()); 
     Iterator<UsbDevice> deviceIterator = deviceList.values().iterator(); 

     while (deviceIterator.hasNext()) { 
      device = deviceIterator.next(); 
      text.setText(text.getText() + "\nDevice name: " 
         + device.getDeviceName()); 
      text.setText(text.getText() + "\nDevice protocol: " 
         + device.getDeviceProtocol()); 
      text.setText(text.getText() + "\nDevice id: " 
         + device.getDeviceId()); 
      text.setText(text.getText() + "\nDevice product id: " 
         + device.getProductId()); 
      text.setText(text.getText() + "\nDevice vendor id: " 
         + device.getVendorId()); 
      text.setText(text.getText() + "\nDevice class: " 
         + device.getDeviceClass()); 
      text.setText(text.getText() + "\nDevice subclass: " 
         + device.getDeviceSubclass()); 
      text.setText(text.getText() + "\nDevice interface count: " 
         + device.getInterfaceCount()); 
      text.setText(text.getText() + "\n\n"); 
     } 

     // communicate with device 
     UsbInterface intf = device.getInterface(0); 
     UsbEndpoint endpoint = intf.getEndpoint(0); 
     UsbDeviceConnection connection = manager.openDevice(device); 
     connection.claimInterface(intf, true); 

     for (int i = 0; i < intf.getEndpointCount(); i++) { 
      UsbEndpoint ep = intf.getEndpoint(i); 
      if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) { 
       if (ep.getDirection() == UsbConstants.USB_DIR_OUT) { 
        endpoint = ep; 
        text.setText("Found: "+i); 
       } 
      } 
     } 


     // byte[] opensession = { 0x0C, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 
     // 0x10, 
     // 0x00, 0x00, 0x00, 0x00 }; 
     // connection.bulkTransfer(endpoint, opensession, opensession.length, 
     // TIMEOUT); 

     byte[] getEvent = { 0x0C, 0x00, 0x00, 0x00, 0x01, 0x00, toByte(0xC7), 
      toByte(0x90), 0x00, 0x00, 0x00, 0x00 }; 
     int status = connection.bulkTransfer(endpoint, getEvent, 
              getEvent.length, TIMEOUT); 
     //text.setText("Status: " + status); 

     byte[] capture = { 0x14, 0x00, 0x00, 0x00, 0x10, 0x00, 0x0E, 0x10, 
      0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
      0x00, 0x00 }; 
     connection.bulkTransfer(endpoint, capture, capture.length, TIMEOUT); 

     // teminate communication 
     BroadcastReceiver mUsbReceiver = new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       String action = intent.getAction(); 
       if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) { 
        UsbDevice device = (UsbDevice) intent 
        .getParcelableExtra(UsbManager.EXTRA_DEVICE); 
        if (device != null) { 
         // call your method that cleans up and closes 
         // communication with the device 
        } 
       } 
      } 
     }; 

    } 

    public static byte toByte(int c) { 
     return (byte) (c <= 0x7f ? c : ((c % 0x80) - 0x80)); 
    } 

} 
+0

Cihaz uygun şekilde numaralandı mı? Uç noktalarda 0 bitmiş cihazlarla toplu aktarım gerçekleştiremezsiniz; sadece transferleri kontrol eder. Bu nedenle, cihazı uygun bir uç noktaya atamanız gerekir. –

cevap

0

Arabirimi ve bitiş noktasını doğru aradığınızdan şüpheleniyorum.

aşağıdaki açıklamaya bakınız.

Öneri: son nokta geçerli olup olmadığını test edin ve aygıt tanımlayıcısıyla eşleştirmeyi deneyin. Bir arabirimin birden fazla ayarı olabilir. AFAIK, kamera için, kamera arayüzü için üç alternatif ayar olmalıdır.

  • Sıfır Bant Genişliği - hayır bitiş noktaları iç - en olasılıkla alternatif 0
  • Isochronous ayarı (eğer kamera desteği o) -
  • Toplu içindeki isochronous son nokta - Toplu uç nokta iç < - yeri Eğer ilgi

(i) Ses Sınıfı gördüğünüz gibi ben (bu USB üzerinden tüm akış protokolü uygulanabilir olmalı, gerçek USB kamera şartname bilmiyorum ama)

Toplu alternatif ayarı aramalı ve daha sonra içindeki uç noktadaki iletişimi gerçekleştirmelisiniz.

0

belki USB arabirimi doğru değil bu işlev

UsbInterface intf = device.getInterface(0); 
UsbEndpoint endpoint = intf.getEndpoint(0); 

geliyor doğru uç noktasını

int status = connection.bulkTransfer(endpoint, getEvent, 
            getEvent.length, TIMEOUT); 

geçmediğine düşünüyorum. Lütfen dizin 0 parametresinin doğru olup olmadığını kontrol edin.