當前位置: 首頁>>代碼示例>>Java>>正文


Java UsbDeviceConnection類代碼示例

本文整理匯總了Java中android.hardware.usb.UsbDeviceConnection的典型用法代碼示例。如果您正苦於以下問題:Java UsbDeviceConnection類的具體用法?Java UsbDeviceConnection怎麽用?Java UsbDeviceConnection使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


UsbDeviceConnection類屬於android.hardware.usb包,在下文中一共展示了UsbDeviceConnection類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: AdbDevice

import android.hardware.usb.UsbDeviceConnection; //導入依賴的package包/類
public AdbDevice(AdbTestActivity activity, UsbDeviceConnection connection,
        UsbInterface intf) {
    mActivity = activity;
    mDeviceConnection = connection;
    mSerial = connection.getSerial();

    UsbEndpoint epOut = null;
    UsbEndpoint epIn = null;
    // look for our bulk endpoints
    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) {
                epOut = ep;
            } else {
                epIn = ep;
            }
        }
    }
    if (epOut == null || epIn == null) {
        throw new IllegalArgumentException("not all endpoints found");
    }
    mEndpointOut = epOut;
    mEndpointIn = epIn;
}
 
開發者ID:sdrausty,項目名稱:buildAPKsSamples,代碼行數:26,代碼來源:AdbDevice.java

示例2: setParameter

import android.hardware.usb.UsbDeviceConnection; //導入依賴的package包/類
public void setParameter(UsbDeviceConnection connection, byte[] params){

        connection.controlTransfer(USB_CONTROL_OUT,
                SET_LINE_CODING, //requestType
                0, //value
                0, //index
                params, // buffer
                params.length, // length
                TIMEOUT);

        // Enable DTR/RTS.
        connection.controlTransfer(USB_CONTROL_OUT,
                SET_CONTROL_LINE_STATE, //requestType
                0x02, //value
                0,  //index
                null, // buffer
                0, // length
                TIMEOUT);
    }
 
開發者ID:FaBoPlatform,項目名稱:FaBo-Serial-Kit,代碼行數:20,代碼來源:Arduino.java

示例3: startRtlSdrService

import android.hardware.usb.UsbDeviceConnection; //導入依賴的package包/類
private int startRtlSdrService(final UsbDevice device) {
    int result;
    final UsbDeviceConnection connection = usbManager.openDevice(device);

    if (connection == null) {
        Log.d(TAG, "Unknown error while opening device: " + device);
        result = R.string.connect_usb_device_status_error_unknown;
    } else {
        final int usbFd = connection.getFileDescriptor();
        final String uspfsPathInput = UsbUtils.deriveProperDeviceName(device.getDeviceName());

        if (rtlSdrService != null) {
            rtlSdrService.startRtlSdr(new StartRtlSdrRequest(arguments, usbFd, uspfsPathInput));
        }
        result = R.string.connect_usb_device_status_ok;
    }

    return result;
}
 
開發者ID:videgro,項目名稱:Ships,代碼行數:20,代碼來源:OpenDeviceActivity.java

示例4: forUsbInterface

import android.hardware.usb.UsbDeviceConnection; //導入依賴的package包/類
public static List<AlternateUsbInterface> forUsbInterface(UsbDeviceConnection deviceConnection, UsbInterface usbInterface) {
    byte[] rawDescriptors = deviceConnection.getRawDescriptors();

    List<AlternateUsbInterface> alternateSettings = new ArrayList<>(2);
    int offset = 0;
    while(offset < rawDescriptors.length) {
        int bLength = rawDescriptors[offset] & 0xFF;
        int bDescriptorType = rawDescriptors[offset + 1] & 0xFF;

        if (bDescriptorType == 0x04) {
            // interface descriptor, we are not interested
            int bInterfaceNumber = rawDescriptors[offset + 2] & 0xFF;
            int bAlternateSetting = rawDescriptors[offset + 3] & 0xFF;

            if (bInterfaceNumber == usbInterface.getId()) {
                alternateSettings.add(new AlternateUsbInterface(usbInterface, bAlternateSetting));
            }
        }

        // go to next structure
        offset += bLength;
    }

    if (alternateSettings.size() < 1) throw new IllegalStateException();
    return alternateSettings;
}
 
開發者ID:martinmarinov,項目名稱:AndroidDvbDriver,代碼行數:27,代碼來源:AlternateUsbInterface.java

示例5: getAlternateSetting

import android.hardware.usb.UsbDeviceConnection; //導入依賴的package包/類
@Test
public void getAlternateSetting() throws Exception {
    UsbDeviceConnection usbDeviceConnection = mockConnectionWithRawDescriptors(new byte[] {
            18, 1, 0, 2, 0, 0, 0, 64, -38, 11, 56, 40, 0, 1, 1, 2, 3, 1, // Device Descriptor
            9, 2, 34, 0, 2, 1, 4, -128, -6, // Configuration Descriptor
            9, 4, 0, 0, 1, -1, -1, -1, 5, // Interface Descriptor for interface 0 with alternate setting 0
            9, 4, 0, 1, 1, -1, -1, -1, 5, // Interface Descriptor for interface 0 with alternate setting 1
            7, 5, -127, 2, 0, 2, 0, // Endpoint Descriptor
            9, 4, 1, 0, 0, -1, -1, -1, 5 // Interface Descriptor for interface 1 with alternate setting 0
    });

    // Interface 0 has alternate settings {0, 1}
    UsbInterface i0 = mockInterface(0);
    assertThat(AlternateUsbInterface.forUsbInterface(usbDeviceConnection, i0),
            equalTo(asList(new AlternateUsbInterface(i0, 0), new AlternateUsbInterface(i0, 1))));

    // Interface 1 has alternate settings {0}
    UsbInterface i1 = mockInterface(1);
    assertThat(AlternateUsbInterface.forUsbInterface(usbDeviceConnection, i1),
            equalTo(singletonList(new AlternateUsbInterface(i1, 0))));
}
 
開發者ID:martinmarinov,項目名稱:AndroidDvbDriver,代碼行數:22,代碼來源:AlternateUsbInterfaceTest.java

示例6: setAdbInterface

import android.hardware.usb.UsbDeviceConnection; //導入依賴的package包/類
private boolean setAdbInterface(UsbDevice device, UsbInterface intf) {
    if (mDeviceConnection != null) {
        if (mInterface != null) {
            mDeviceConnection.releaseInterface(mInterface);
            mInterface = null;
        }
        mDeviceConnection.close();
        mDevice = null;
        mDeviceConnection = null;
    }

    if (device != null && intf != null) {
        UsbDeviceConnection connection = mManager.openDevice(device);
        if (connection != null) {
            log("open succeeded");
            if (connection.claimInterface(intf, false)) {
                log("claim interface succeeded");
                mDevice = device;
                mDeviceConnection = connection;
                mInterface = intf;
                mAdbDevice = new AdbDevice(this, mDeviceConnection, intf);
                log("call start");
                mAdbDevice.start();
                return true;
            } else {
                log("claim interface failed");
                connection.close();
            }
        } else {
            log("open failed");
        }
    }

    if (mDeviceConnection == null && mAdbDevice != null) {
        mAdbDevice.stop();
        mAdbDevice = null;
    }
    return false;
}
 
開發者ID:sdrausty,項目名稱:buildAPKsSamples,代碼行數:40,代碼來源:AdbTestActivity.java

示例7: open

import android.hardware.usb.UsbDeviceConnection; //導入依賴的package包/類
@Override
public void open(UsbDeviceConnection connection) throws IOException {
    if (mConnection != null) {
        throw new IOException("Already open");
    }
    mConnection = connection;

    boolean opened = false;
    try {
        for (int i = 0; i < mDevice.getInterfaceCount(); i++) {
            if (connection.claimInterface(mDevice.getInterface(i), true)) {
                Log.d(TAG, "claimInterface " + i + " SUCCESS");
            } else {
                throw new IOException("Error claiming interface " + i);
            }
        }
        reset();
        opened = true;
    } finally {
        if (!opened) {
            close();
            mConnection = null;
        }
    }
}
 
開發者ID:msillano,項目名稱:USBphpTunnel,代碼行數:26,代碼來源:FtdiSerialDriver.java

示例8: fromDeviceConnection

import android.hardware.usb.UsbDeviceConnection; //導入依賴的package包/類
/**
 * Request the active configuration descriptor through the USB device connection.
 */
public static ConfigurationDescriptor fromDeviceConnection(UsbDeviceConnection connection)
        throws IllegalArgumentException, ParseException {
    //Create a sufficiently large buffer for incoming data
    byte[] buffer = new byte[LENGTH];

    connection.controlTransfer(REQUEST_TYPE, REQUEST, REQ_VALUE, REQ_INDEX,
            buffer, LENGTH, TIMEOUT);

    //Do a short read to determine descriptor length
    int totalLength = headerLengthCheck(buffer);
    //Obtain the full descriptor
    buffer = new byte[totalLength];
    connection.controlTransfer(REQUEST_TYPE, REQUEST, REQ_VALUE, REQ_INDEX,
            buffer, totalLength, TIMEOUT);

    return parseResponse(buffer);
}
 
開發者ID:androidthings,項目名稱:sample-usbenum,代碼行數:21,代碼來源:ConfigurationDescriptor.java

示例9: setupUsb

import android.hardware.usb.UsbDeviceConnection; //導入依賴的package包/類
protected void setupUsb(UsbDevice device) {
    UsbInterface inf = device.getInterface(0);
    UsbDeviceConnection conn = mUsbManager.openDevice(device);
    if (conn == null) {
        Log.wtf("MainActivity", "unable to open device?");
        return;
    }

    if (!conn.claimInterface(inf, true)) {
        conn.close();
        Log.wtf("MainActivity", "unable to claim interface!");
        return;
    }

    mBlinkDevice = device;
    mBlinkConn = conn;
}
 
開發者ID:nasa,項目名稱:astrobee_android,代碼行數:18,代碼來源:MainActivity.java

示例10: communicate

import android.hardware.usb.UsbDeviceConnection; //導入依賴的package包/類
private void communicate () {
    UsbDeviceConnection connection = usbManager.openDevice(device);
    try {
        sPort.open(connection);
        sPort.setParameters(2400, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
        sPort.setRTS(false);
        sPort.setDTR(true);

    } catch (IOException e) {
        Log.e(TAG, "Error setting up device: " + e.getMessage(), e);
        mTitleTextView.setText("Error opening device: " + e.getMessage());
        try {
            sPort.close();
        } catch (IOException e2) {
            // Ignore.
        }
        sPort = null;
        return;
    }
    mTitleTextView.setText("Serial device: " + sPort.getClass().getSimpleName());

    onDeviceStateChange();
}
 
開發者ID:kost,項目名稱:DroidMeter,代碼行數:24,代碼來源:SerialConsoleActivity.java

示例11: onResume

import android.hardware.usb.UsbDeviceConnection; //導入依賴的package包/類
@Override
protected void onResume() {
    super.onResume();
    changed=new HandleChange(3);
    calibCounter=0;
    Log.d(TAG, "Resumed, port=" + sPort);
    if (sPort == null) {
        mTitleTextView.setText("No serial device.");
    } else {
        usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
        List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(usbManager);
        if (availableDrivers.isEmpty()) {
            mTitleTextView.append("No drivers available\n");
            Log.d("SerialKost", "no drivers available");
            return;
        }

        UsbSerialDriver driver = availableDrivers.get(0);
        sPort = driver.getPorts().get(0);
        device=driver.getDevice();
        UsbDeviceConnection connection = usbManager.openDevice(device);
        if (connection == null) {
            mTitleTextView.setText("Opening device failed");

            PendingIntent mPermissionIntent;

            Log.i("SerialKost", "Setting PermissionIntent -> MainMenu");
            mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
            Log.i("SerialKost", "Setting IntentFilter -> MainMenu");
            IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
            Log.i("SerialKost", "Setting registerReceiver -> MainMenu");
            registerReceiver(mUsbReceiver, filter);
            Log.i("SerialKost", "Setting requestPermission -> MainMenu");
            usbManager.requestPermission(device, mPermissionIntent);
            return;
        }
        communicate();
    }
}
 
開發者ID:kost,項目名稱:DroidMeter,代碼行數:40,代碼來源:SerialConsoleActivity.java

示例12: createUsbSerialDevice

import android.hardware.usb.UsbDeviceConnection; //導入依賴的package包/類
public static UsbSerialDevice createUsbSerialDevice(UsbDevice device, UsbDeviceConnection connection, int iface)
  {
/*
 * It checks given vid and pid and will return a custom driver or a CDC serial driver.
 * When CDC is returned open() method is even more important, its response will inform about if it can be really
 * opened as a serial device with a generic CDC serial driver
 */
      int vid = device.getVendorId();
      int pid = device.getProductId();

      if(FTDISioIds.isDeviceSupported(vid, pid))
          return new FTDISerialDevice(device, connection, iface);
      else if(CP210xIds.isDeviceSupported(vid, pid))
          return new CP2102SerialDevice(device, connection, iface);
      else if(PL2303Ids.isDeviceSupported(vid, pid))
          return new PL2303SerialDevice(device, connection, iface);
      else if(CH34xIds.isDeviceSupported(vid, pid))
          return new CH34xSerialDevice(device, connection, iface);
      else if(isCdcDevice(device))
          return new CDCSerialDevice(device, connection, iface);
      else
          return null;
  }
 
開發者ID:GIGATeam,項目名稱:UsbExtension,代碼行數:24,代碼來源:UsbSerialDevice.java

示例13: connectSerial

import android.hardware.usb.UsbDeviceConnection; //導入依賴的package包/類
public Boolean connectSerial(UsbDevice device, Integer baud) {
    UsbDeviceConnection connection = connectDevice(device);
    UsbSerialDevice serialPort = UsbSerialDevice.createUsbSerialDevice(device, connection);
    _workingDevice = device;
    if(!serialPort.open()){
        return false;
    }else {
        _serialPort = serialPort;
        serialPort.setBaudRate(baud);
        serialPort.setDataBits(UsbSerialInterface.DATA_BITS_8);
        serialPort.setStopBits(UsbSerialInterface.STOP_BITS_1);
        serialPort.setParity(UsbSerialInterface.PARITY_NONE);
        serialPort.setFlowControl(UsbSerialInterface.FLOW_CONTROL_OFF);
        serialPort.read(mCallback);
        return true;
    }
}
 
開發者ID:GIGATeam,項目名稱:UsbExtension,代碼行數:18,代碼來源:UsbContext.java

示例14: setDevice

import android.hardware.usb.UsbDeviceConnection; //導入依賴的package包/類
private boolean setDevice(UsbDevice device) {
    Logger.d("setDevice " + device);
    clearDevice();
    if (null == device) {
        return false;
    }
    if (device.getVendorId() != mVendorId) {
        printDevice(device);
        Logger.i("Not a target vendor: expecting %d", mVendorId);
        return false;
    }
    if (device.getProductId() != mProductId) {
        printDevice(device);
        Logger.i("Not a target product: expecting %d", mProductId);
        return false;
    }
    if (!mUsbManager.hasPermission(device)) {
        Logger.d("request permission");
        mUsbManager.requestPermission(device, mPermissionIntent);
        return false;
    }
    printDevice(device);
    try {
        UsbInterface usbinterface = device.getInterface(0);
        UsbDeviceConnection connection = mUsbManager.openDevice(device);
        if (!connection.claimInterface(usbinterface, true)) {
            return false;
        }
        mDevice = device;
        mConnection = connection;
        Logger.d("open SUCCESS");
        if (null != mOnDeviceListener) {
            mOnDeviceListener.onAttached();
        }
        return true;
    } catch (Exception e) {
        Logger.e(e, e.getLocalizedMessage());
    }
    return false;
}
 
開發者ID:dena-csr,項目名稱:bootloadHID-android,代碼行數:41,代碼來源:UsbWriter.java

示例15: setUsbInterface

import android.hardware.usb.UsbDeviceConnection; //導入依賴的package包/類
private boolean setUsbInterface(android.hardware.usb.UsbDevice device, UsbInterface intf) {
    if (mDeviceConnection != null) {
        if (mInterface != null) {
            mDeviceConnection.releaseInterface(mInterface);
            mInterface = null;
        }
        mDeviceConnection.close();
        mDevice = null;
        mDeviceConnection = null;
    }

    if (device != null && intf != null) {
        UsbDeviceConnection connection = mUsbManager.openDevice(device);
        if (connection != null) {
            if (connection.claimInterface(intf, true)) {
                mDevice = device;
                mDeviceConnection = connection;
                mInterface = intf;
                mFcDevice = new FcUsbDevice(this, mDeviceConnection, intf,
                        mXmlObjects);
                mFcDevice.getObjectTree().setXmlObjects(mXmlObjects);

                mFcDevice.start();
                return true;
            } else {
                connection.close();
            }
        }
    }

    if (mDeviceConnection == null && mFcDevice != null) {
        mFcDevice.stop();
        mFcDevice = null;
    }
    return false;
}
 
開發者ID:MarcProe,項目名稱:lp2go,代碼行數:37,代碼來源:MainActivity.java


注:本文中的android.hardware.usb.UsbDeviceConnection類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。