本文整理匯總了Java中android.hardware.usb.UsbInterface類的典型用法代碼示例。如果您正苦於以下問題:Java UsbInterface類的具體用法?Java UsbInterface怎麽用?Java UsbInterface使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
UsbInterface類屬於android.hardware.usb包,在下文中一共展示了UsbInterface類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: AdbDevice
import android.hardware.usb.UsbInterface; //導入依賴的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;
}
示例2: enumerate
import android.hardware.usb.UsbInterface; //導入依賴的package包/類
public static UsbHidDevice[] enumerate(Context context, int vid, int pid) throws Exception {
UsbManager usbManager = (UsbManager) context.getApplicationContext().getSystemService(Context.USB_SERVICE);
if (usbManager == null) {
throw new Exception("no usb service");
}
Map<String, UsbDevice> devices = usbManager.getDeviceList();
List<UsbHidDevice> usbHidDevices = new ArrayList<>();
for (UsbDevice device : devices.values()) {
if ((vid == 0 || device.getVendorId() == vid) && (pid == 0 || device.getProductId() == pid)) {
for (int i = 0; i < device.getInterfaceCount(); i++) {
UsbInterface usbInterface = device.getInterface(i);
if (usbInterface.getInterfaceClass() == INTERFACE_CLASS_HID) {
UsbHidDevice hidDevice = new UsbHidDevice(device, usbInterface, usbManager);
usbHidDevices.add(hidDevice);
}
}
}
}
return usbHidDevices.toArray(new UsbHidDevice[usbHidDevices.size()]);
}
示例3: UsbHidDevice
import android.hardware.usb.UsbInterface; //導入依賴的package包/類
private UsbHidDevice(UsbDevice usbDevice, UsbInterface usbInterface, UsbManager usbManager) {
mUsbDevice = usbDevice;
mUsbInterface = usbInterface;
mUsbManager= usbManager;
for (int i = 0; i < mUsbInterface.getEndpointCount(); i++) {
UsbEndpoint endpoint = mUsbInterface.getEndpoint(i);
int dir = endpoint.getDirection();
int type = endpoint.getType();
if (mInUsbEndpoint == null && dir == UsbConstants.USB_DIR_IN && type == UsbConstants.USB_ENDPOINT_XFER_INT) {
mInUsbEndpoint = endpoint;
}
if (mOutUsbEndpoint == null && dir == UsbConstants.USB_DIR_OUT && type == UsbConstants.USB_ENDPOINT_XFER_INT) {
mOutUsbEndpoint = endpoint;
}
}
}
示例4: open
import android.hardware.usb.UsbInterface; //導入依賴的package包/類
/**
* open specific interface
* @param interfaceIndex
* @return
*/
public synchronized UsbInterface open(final int interfaceIndex) {
if (DEBUG) Log.i(TAG, "UsbControlBlock#open:" + interfaceIndex);
final UsbDevice device = mWeakDevice.get();
UsbInterface intf = null;
intf = mInterfaces.get(interfaceIndex);
if (intf == null) {
intf = device.getInterface(interfaceIndex);
if (intf != null) {
synchronized (mInterfaces) {
mInterfaces.append(interfaceIndex, intf);
}
}
}
return intf;
}
示例5: close
import android.hardware.usb.UsbInterface; //導入依賴的package包/類
/**
* close specified interface. USB device itself still keep open.
* @param interfaceIndex
*/
public synchronized void close() {
if (DEBUG) Log.i(TAG, "UsbControlBlock#close:");
if (mConnection != null) {
final int n = mInterfaces.size();
int key;
UsbInterface intf;
for (int i = 0; i < n; i++) {
key = mInterfaces.keyAt(i);
intf = mInterfaces.get(key);
mConnection.releaseInterface(intf);
}
mConnection.close();
mConnection = null;
final USBMonitor monitor = mWeakMonitor.get();
if (monitor != null) {
if (monitor.mOnDeviceConnectListener != null) {
final UsbDevice device = mWeakDevice.get();
monitor.mOnDeviceConnectListener.onDisconnect(device, this);
}
monitor.mCtrlBlocks.remove(getDevice());
}
}
}
示例6: forUsbInterface
import android.hardware.usb.UsbInterface; //導入依賴的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;
}
示例7: getAlternateSetting
import android.hardware.usb.UsbInterface; //導入依賴的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))));
}
示例8: onCreate
import android.hardware.usb.UsbInterface; //導入依賴的package包/類
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adb);
mLog = (TextView)findViewById(R.id.log);
mManager = (UsbManager)getSystemService(Context.USB_SERVICE);
// check for existing devices
for (UsbDevice device : mManager.getDeviceList().values()) {
UsbInterface intf = findAdbInterface(device);
if (setAdbInterface(device, intf)) {
break;
}
}
// listen for new devices
IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
registerReceiver(mUsbReceiver, filter);
}
示例9: setAdbInterface
import android.hardware.usb.UsbInterface; //導入依賴的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;
}
示例10: readDevice
import android.hardware.usb.UsbInterface; //導入依賴的package包/類
/**
* Enumerate the endpoints and interfaces on the connected device.
*
* @param device Device to query.
* @return String description of the device configuration.
*/
public static String readDevice(UsbDevice device) {
StringBuilder sb = new StringBuilder();
sb.append("Device Name: " + device.getDeviceName() + "\n");
sb.append(String.format(
"Device Class: %s -> Subclass: 0x%02x -> Protocol: 0x%02x\n",
nameForClass(device.getDeviceClass()),
device.getDeviceSubclass(), device.getDeviceProtocol()));
for (int i = 0; i < device.getInterfaceCount(); i++) {
UsbInterface intf = device.getInterface(i);
sb.append(String.format(Locale.US,
"-- Interface %d Class: %s -> Subclass: 0x%02x -> Protocol: 0x%02x\n",
intf.getId(),
nameForClass(intf.getInterfaceClass()),
intf.getInterfaceSubclass(),
intf.getInterfaceProtocol()));
sb.append(String.format(Locale.US, " -- Endpoint Count: %d\n",
intf.getEndpointCount()));
}
return sb.toString();
}
示例11: isCdcDevice
import android.hardware.usb.UsbInterface; //導入依賴的package包/類
public static boolean isCdcDevice(UsbDevice device)
{
int iIndex = device.getInterfaceCount();
for(int i=0;i<=iIndex-1;i++)
{
UsbInterface iface = device.getInterface(i);
if(iface.getInterfaceClass() == UsbConstants.USB_CLASS_CDC_DATA)
return true;
}
return false;
}
示例12: setupUsb
import android.hardware.usb.UsbInterface; //導入依賴的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;
}
示例13: close
import android.hardware.usb.UsbInterface; //導入依賴的package包/類
/**
* close specified interface. USB device itself still keep open.
*/
public synchronized void close() {
if (DEBUG) Log.i(TAG, "UsbControlBlock#close:");
if (mConnection != null) {
final int n = mInterfaces.size();
int key;
UsbInterface intf;
for (int i = 0; i < n; i++) {
key = mInterfaces.keyAt(i);
intf = mInterfaces.get(key);
mConnection.releaseInterface(intf);
}
mConnection.close();
mConnection = null;
final USBMonitor monitor = mWeakMonitor.get();
if (monitor != null) {
if (monitor.mOnDeviceConnectListener != null) {
final UsbDevice device = mWeakDevice.get();
monitor.mOnDeviceConnectListener.onDisconnect(device, this);
}
monitor.mCtrlBlocks.remove(getDevice());
}
}
}
示例14: onConnect
import android.hardware.usb.UsbInterface; //導入依賴的package包/類
@Override
public void onConnect() {
// Serial mode only
// TODO: find the interface and endpoint indexes no matter what mode it is
int ifIdx = 1;
int epInIdx = 1;
int epOutIdx = 0;
UsbInterface iface = usbDevice.getInterface(ifIdx);
if (usbConnection.claimInterface(iface, true)) {
logger.log("Interface claimed successfully\n");
} else {
logger.log("ERROR - can't claim interface\n");
return;
}
endpointIn = iface.getEndpoint(epInIdx);
endpointOut = iface.getEndpoint(epOutIdx);
super.onConnect();
}
示例15: setDevice
import android.hardware.usb.UsbInterface; //導入依賴的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;
}