本文整理汇总了Java中android.hardware.usb.UsbManager类的典型用法代码示例。如果您正苦于以下问题:Java UsbManager类的具体用法?Java UsbManager怎么用?Java UsbManager使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
UsbManager类属于android.hardware.usb包,在下文中一共展示了UsbManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: enumerate
import android.hardware.usb.UsbManager; //导入依赖的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()]);
}
示例2: onCreate
import android.hardware.usb.UsbManager; //导入依赖的package包/类
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(intent.getAction())) {
UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
Log.d(TAG, "USB DVB-T attached: " + usbDevice.getDeviceName());
Intent newIntent = new Intent(ACTION_DVB_DEVICE_ATTACHED);
newIntent.putExtra(UsbManager.EXTRA_DEVICE, usbDevice);
try {
startActivity(newIntent);
} catch (ActivityNotFoundException e) {
Log.d(TAG, "No activity found for DVB-T handling");
}
}
finish();
}
示例3: onCreate
import android.hardware.usb.UsbManager; //导入依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRed = new SeekText(R.id.seek_red, R.id.txt_red, 255);
mGreen = new SeekText(R.id.seek_green, R.id.txt_green, 255);
mBlue = new SeekText(R.id.seek_blue, R.id.txt_blue, 255);
mImageView = (ImageView) findViewById(R.id.img_preview);
mImageView.setBackgroundColor(Color.argb(0, 255, 255, 255));
mUsbManager = (UsbManager) getSystemService(USB_SERVICE);
mPermIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
mUsbThread = new HandlerThread("USB Handler");
mUsbThread.start();
mUsbHandler = new Handler(mUsbThread.getLooper());
mMainHandler = new Handler();
}
示例4: onNewIntent
import android.hardware.usb.UsbManager; //导入依赖的package包/类
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(intent.getAction())) {
UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
RobotLog.vv(TAG, "ACTION_USB_DEVICE_ATTACHED: %s", usbDevice.getDeviceName());
if (usbDevice != null) { // paranoia
// We might get attachment notifications before the event loop is set up, so
// we hold on to them and pass them along only when we're good and ready.
if (receivedUsbAttachmentNotifications != null) { // *total* paranoia
receivedUsbAttachmentNotifications.add(usbDevice);
passReceivedUsbAttachmentsToEventLoop();
}
}
}
}
示例5: mountDevice
import android.hardware.usb.UsbManager; //导入依赖的package包/类
private FileSystem mountDevice() {
Log.d("MountTask", "opening OTG disk...");
manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> devices = manager.getDeviceList();
Iterator<UsbDevice> deviceIterator = devices.values().iterator();
if (devices.size() < 1) {
Log.e("MountTask", "No device found...");
errorMessageId = R.string.pluginDisk;
} else if (deviceIterator.hasNext()) {
device = deviceIterator.next();
Log.d("MountTask", String.format("Found device: %04X:%04X, Class: %02X:%02X, at %s",
device.getVendorId(), device.getProductId(),
device.getDeviceClass(), device.getDeviceSubclass(),
device.getDeviceName()));
if (manager.hasPermission(device)) {
return claimInterface(device);
} else {
Log.e("MountTask", "No permission granted to access this device, requesting...");
manager.requestPermission(device,
PendingIntent.getBroadcast(context, 0, new Intent(MainActivity.ACTION_USB_PERMISSION), 0));
}
Log.d("MountTask", "No more devices found");
}
return null;
}
示例6: onReceive
import android.hardware.usb.UsbManager; //导入依赖的package包/类
public void onReceive(Context context, Intent intent) {
if (ACTION_USB_PERMISSION.equals(intent.getAction())) {
synchronized (this) {
final UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if (device != null) {
currentDevice = device;
Log.i(TAG, "permission granted for device: " + device);
// Try again to open device, now we have permission
final int connectUsbDeviceStatus = openDevice(device);
processConnectUsbDeviceStatus(connectUsbDeviceStatus);
}
} else {
Log.d(TAG, "permission denied for device: " + device);
processConnectUsbDeviceStatus(R.string.connect_usb_device_status_error_permission_denied);
}
}
}
}
示例7: onCreate
import android.hardware.usb.UsbManager; //导入依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!MainActivity.isNativeLibraryLoaded()) {
final String msg=getString(R.string.connect_usb_device_status_error_native_library_not_loaded);
Analytics.getInstance().logEvent(Analytics.CATEGORY_ANDROID_DEVICE,msg, "");
finish(ERROR_REASON_NATIVE_LIBRARY_NOT_LOADED,msg);
return;
}
setContentView(R.layout.progress);
usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
permissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
registerReceiver(usbReceiver,new IntentFilter(ACTION_USB_PERMISSION));
Log.d(TAG, "onCreate");
}
示例8: onReceive
import android.hardware.usb.UsbManager; //导入依赖的package包/类
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action != null) {
switch (action) {
case UsbManager.ACTION_USB_DEVICE_DETACHED:
final UsbDevice detDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
final String detMsg="Device DEtached";
Log.v(TAG,detMsg+" "+detDevice);
Analytics.getInstance().logEvent(Analytics.CATEGORY_RTLSDR_DEVICE,detMsg,detDevice.toString());
break;
case UsbManager.ACTION_USB_DEVICE_ATTACHED:
case UsbManager.ACTION_USB_ACCESSORY_ATTACHED:
final UsbDevice attDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
final String attMsg="Device atached";
Log.v(TAG,attMsg+" "+attDevice);
Analytics.getInstance().logEvent(Analytics.CATEGORY_RTLSDR_DEVICE,attMsg,attDevice.toString());
deviceAttached();
break;
default:
// Nothing to do
break;
} // END SWITCH
}
}
示例9: onCreate
import android.hardware.usb.UsbManager; //导入依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// FIXME: Observed exception "IllegalAccessException (@MainActivity:onCreate:16) {main}"
// Init some singletons which need the Context
Analytics.getInstance().init(this);
SettingsUtils.getInstance().init(this);
setContentView(R.layout.activity_main);
final ActionBar actionBar=getActionBar();
if (actionBar!=null) {
actionBar.setDisplayShowTitleEnabled(true);
//actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
}
final IntentFilter filter=new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
registerReceiver(usbReceiver,filter);
}
示例10: UsbHidDevice
import android.hardware.usb.UsbManager; //导入依赖的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;
}
}
}
示例11: onCreate
import android.hardware.usb.UsbManager; //导入依赖的package包/类
@Override
public void onCreate() {
super.onCreate();
if (DEBUG) Log.d(TAG, "onCreate");
mUsbManager = (UsbManager) getBaseContext().getSystemService(Context.USB_SERVICE);
mAudioManager = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
mPowerManager = (PowerManager) getBaseContext().getSystemService(Context.POWER_SERVICE);
mInputManager = (InputManager) getBaseContext().getSystemService(Context.INPUT_SERVICE);
ConfigStorage.SerialPortIdentifier portIdentifier =
ConfigStorage.readDefaultPort(getBaseContext());
if (DEBUG) Log.d(TAG, "onCreate, portIdentifier: " + portIdentifier);
if (portIdentifier != null) {
UsbSerialPort port = findUsbSerialPort(portIdentifier);
if (port == null) {
Log.w(TAG, "Unable to find usb serial port, make sure device is connected: "
+ portIdentifier);
return;
}
onUsbSerialPortChanged(port);
}
}
示例12: onCreate
import android.hardware.usb.UsbManager; //导入依赖的package包/类
@Override
public boolean onCreate() {
Context context = getContext();
updateSettings();
usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_USB_PERMISSION);
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
context.registerReceiver(mUsbReceiver, filter);
updateRoots();
return true;
}
示例13: onReceive
import android.hardware.usb.UsbManager; //导入依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
String deviceName = usbDevice.getDeviceName();
if (UsbStorageProvider.ACTION_USB_PERMISSION.equals(action)) {
boolean permission = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false);
if (permission) {
discoverDevice(usbDevice);
notifyRootsChanged();
notifyDocumentsChanged(getContext(), getRootId(usbDevice)+ROOT_SEPERATOR);
} else {
// so we don't ask for permission again
}
} else if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)
|| UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
updateRoots();
}
}
示例14: onNewIntent
import android.hardware.usb.UsbManager; //导入依赖的package包/类
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(intent.getAction())) {
UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (usbDevice != null) { // paranoia
// We might get attachment notifications before the event loop is set up, so
// we hold on to them and pass them along only when we're good and ready.
if (receivedUsbAttachmentNotifications != null) { // *total* paranoia
receivedUsbAttachmentNotifications.add(usbDevice);
passReceivedUsbAttachmentsToEventLoop();
}
}
}
}
示例15: onCreate
import android.hardware.usb.UsbManager; //导入依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usbManager = (UsbManager) getApplicationContext().getSystemService(Context.USB_SERVICE);
textView = (TextView)findViewById(R.id.mainTextView);
scrollView = (ScrollView)findViewById(R.id.mainScrollView);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
prepareFirmware();
}