Java 类android.hardware.usb.UsbManager 实例源码

项目:easyfilemanager    文件:UsbStorageProvider.java   
@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();
    }
}
项目:AndroidDvbDriver    文件:UsbDelegate.java   
@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();
}
项目:astrobee_android    文件:MainActivity.java   
@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();
}
项目:Team9261-2017-2018    文件:FtcRobotControllerActivity.java   
@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();
      }
    }
  }
}
项目:OTGDiskBackup    文件:MountTask.java   
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;
}
项目:Ships    文件:OpenDeviceActivity.java   
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);
        }
    }
}
}
项目:Ships    文件:OpenDeviceActivity.java   
@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");
}
项目:Ships    文件:MainActivity.java   
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
    }
}
项目:Ships    文件:MainActivity.java   
@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);
}
项目:UsbHid    文件:UsbHidDevice.java   
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()]);
}
项目:UsbHid    文件:UsbHidDevice.java   
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;
        }
    }
}
项目:bimdroid    文件:BmwIBusService.java   
@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);
    }
}
项目:easyfilemanager    文件:UsbStorageProvider.java   
@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;
}
项目:FTC2016    文件:FtcRobotControllerActivity.java   
@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();
      }
    }
  }
}
项目:AndroNut    文件:MainActivity.java   
@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();
}
项目:AndroidDvbDriver    文件:DvbUsbDeviceRegistry.java   
/**
 * Gets a {@link DvbDevice} if a supported DVB USB dongle is connected to the system.
 * If multiple dongles are connected, a {@link Collection} would be returned
 * @param context a context for obtaining {@link Context#USB_SERVICE}
 * @return a {@link Collection} of available {@link DvbDevice} devices. Can be empty.
 */
public static List<DvbDevice> getUsbDvbDevices(Context context) throws DvbException {
    List<DvbDevice> availableDvbDevices = new ArrayList<>();

    UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
    Collection<UsbDevice> availableDevices = manager.getDeviceList().values();
    DvbException lastException = null;
    for (UsbDevice usbDevice : availableDevices) {
        try {
            DvbDevice frontend = getDvbUsbDeviceFor(usbDevice, context);
            if (frontend != null) availableDvbDevices.add(frontend);
        } catch (DvbException e) {
            // Failed to initialize this device, try next and capture exception
            e.printStackTrace();
            lastException = e;
        }
    }
    if (availableDvbDevices.isEmpty()) {
        if (lastException != null) throw  lastException;
    }
    return availableDvbDevices;
}
项目:AndroidDvbDriver    文件:DeviceChooserActivity.java   
private void handleException(DvbException e) {
    UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
    Collection<UsbDevice> availableDevices = manager.getDeviceList().values();
    int[] productIds = new int[availableDevices.size()];
    int[] vendorIds = new int[availableDevices.size()];

    int id = 0;
    for (UsbDevice usbDevice : availableDevices) {
        productIds[id] = usbDevice.getProductId();
        vendorIds[id] = usbDevice.getVendorId();
        id++;
    }

    response.putExtra(CONTRACT_ERROR_CODE, e.getErrorCode().name());
    response.putExtra(CONTRACT_RAW_TRACE, StackTraceSerializer.serialize(e));
    response.putExtra(CONTRACT_USB_PRODUCT_IDS, productIds);
    response.putExtra(CONTRACT_USB_VENDOR_IDS, vendorIds);

    finishWith(RESULT_ERROR);
}
项目:Ftc2018RelicRecovery    文件:FtcRobotControllerActivity.java   
@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();
      }
    }
  }
}
项目:InstantUpload    文件:ControllerActivity.java   
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (ACTION_USB_PERMISSION.equals(action)) {
        synchronized (this) {
            usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
            if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                //user choose YES for your previously popup window asking for grant perssion for this usb device
                if(null != usbDevice){
                    performConnect(usbDevice);
                }
            }
            else {
                //user choose NO for your previously popup window asking for grant perssion for this usb device
                log("Permission denied for device" + usbDevice);
            }
        }
    }
}
项目:InstantUpload    文件:ControllerActivity.java   
public void getAllUsbDevices() {
    UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
    HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
    Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
    int i = 0;
    while(deviceIterator.hasNext()){
        i++;
        UsbDevice device = deviceIterator.next();
        log("--------");
        log("设备 : " + i);
        log("device id : " + device.getDeviceId());
        log("name : " + device.getDeviceName());
        log("class : " + device.getDeviceClass());
        log("subclass : " + device.getDeviceSubclass());
        log("vendorId : " + device.getVendorId());
        // log("version : " + device.getVersion() );
        log("serial number : " + device.getSerialNumber() );
        log("interface count : " + device.getInterfaceCount());
        log("device protocol : " + device.getDeviceProtocol());
        log("--------");

    }
}
项目:InstantUpload    文件:ControllerActivity.java   
void connectMTPDevice(){
    UsbManager usbManager = (UsbManager)getSystemService(Context.USB_SERVICE);
    Log.v("usb manager get", usbManager.toString());
    Map<String, UsbDevice> map = usbManager.getDeviceList();
    Set<String> set = map.keySet();

    if (set.size() == 0) {
        log("无法获取设备信息,请确保相机已经连接或者处于激活状态");
    }

    for (String s : set) {
        UsbDevice device = map.get(s);
        if( !usbManager.hasPermission(device) ){
            registerUsbPermission(device);
            return;
        }else {
            performConnect(device);
        }
    }
}
项目:InstantUpload    文件:InitiatorFactory.java   
static public BaselineInitiator produceInitiator(UsbDevice device, UsbManager usbManager) throws PTPException {
    BaselineInitiator bi;
    CameraDetector cd = new CameraDetector(device);
    if (cd.getSupportedVendorId() == CameraDetector.VENDOR_ID_CANON) {
        Log.d(TAG, "Device is CANON, open EOSInitiator");
        bi = new EosInitiator(device, usbManager.openDevice(device));
    } else if (cd.getSupportedVendorId() == CameraDetector.VENDOR_ID_NIKON) {
        Log.d(TAG, "Device is Nikon, open NikonInitiator");
        bi = new NikonInitiator(device, usbManager.openDevice(device));
    } else if (cd.getSupportedVendorId() == CameraDetector.VENDOR_ID_SONY) {
        Log.d(TAG, "Device is Sony, open SonyInitiator");
        bi = new SonyInitiator(device, usbManager.openDevice(device));
    } else /* if (cd.getSupportedVendorId() == CameraDetector.VENDOR_ID_OTHER) */ {
        Log.d(TAG, "Unkown device, open BaselineInitiator");
        bi = new BaselineInitiator (device, usbManager.openDevice(device));
    }

    return bi;
}
项目:RobotIGS    文件:FtcRobotControllerActivity.java   
@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();
      }
    }
  }
}
项目:AndProx    文件:MainActivity.java   
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (ACTION_USB_PERMISSION.equals(action) || ACTION_USB_PERMISSION_AUTOCONNECT.equals(action)) {
        synchronized (this) {
            UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

            if (!intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                Log.d(TAG, "permission denied for mDevice " + device);
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setMessage(R.string.permission_denied)
                        .setTitle(R.string.permission_denied_title);
                builder.show();
            } else if (ACTION_USB_PERMISSION_AUTOCONNECT.equals(action)) {
                // permission was granted, and now we need to hit up the connect button again
                btnConnect(null);
            }
        }
    }
}
项目:buildAPKsSamples    文件:AdbTestActivity.java   
@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);
}
项目:USBphpTunnel    文件:SerialConsoleActivity.java   
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    //
    StringBuilder sb = new StringBuilder();
    sb.append("INTENT Action: " + intent.getAction() + "\n");
    sb.append("URI: " + intent.toUri(Intent.URI_INTENT_SCHEME).toString() + "\n");
    String log = sb.toString();
    Log.d(TAG, log);
    //
    if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
        UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
        if ((device.getVendorId() == sPort.getDriver().getDevice().getVendorId()) &&
                (device.getProductId() == sPort.getDriver().getDevice().getProductId())) {
            // call your method that cleans up and closes application
            Log.d(TAG, "USB_DEVICE_DETACHED: pause SerialConsoleActivity");
            SerialConsoleActivity.this.onPause();
        }
    }
    if (new String("usbSerial.action.REBOOT").equals(action)) {
        Log.d(TAG, "REBOOT: delayed received");
        rebootNow();
    }


    }
项目:sample-usbenum    文件:UsbActivity.java   
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_usb);

    mStatusView = (TextView) findViewById(R.id.text_status);
    mResultView = (TextView) findViewById(R.id.text_result);

    mUsbManager = getSystemService(UsbManager.class);

    // Detach events are sent as a system-wide broadcast
    IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED);
    registerReceiver(mUsbReceiver, filter);

    handleIntent(getIntent());
}
项目:FireFiles    文件:UsbStorageProvider.java   
@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;
}
项目:FireFiles    文件:UsbStorageProvider.java   
@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();
    }
}
项目:RepWifiApp    文件:MainActivity.java   
private void setUsbDeviceMonitor() {
    detachReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_DETACHED)) {
                handleUsbEvent(true);
            } else if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) {
                handleUsbEvent(false);
            }
        }
    };

    IntentFilter filter = new IntentFilter();
    filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
    registerReceiver(detachReceiver, filter);

    IntentFilter filt2 = new IntentFilter();
    filt2.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
    registerReceiver(detachReceiver, filt2);
}
项目:simple-share-android    文件:UsbStorageProvider.java   
@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;
}
项目:simple-share-android    文件:UsbStorageProvider.java   
@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();
    }
}
项目:phonk    文件:AdkPort.java   
public void setup(Context context) {

        mManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
        UsbAccessory[] accessoryList = mManager.getAccessoryList();
        PendingIntent mPermissionIntent = PendingIntent.getBroadcast(context, 0,
                new Intent(ACTION_USB_PERMISSION), 0);
        IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
        context.registerReceiver(mUsbReceiver, filter);
        mManager.requestPermission(accessoryList[0], mPermissionIntent);


        if (accessoryList[0] != null) {

            mAccessory = accessoryList[0];
            if (mManager.hasPermission(mAccessory)) {
                openAccessory(mAccessory);
            }
        }

    }
项目:mi-firma-android    文件:AnyUSBDevice.java   
/** Constructor.
 * @param usbDev Dispositivo USB
 * @param UsbManager Gestor de dispositivos USB de Android
 * @throws UsbDeviceException */
protected AnyUSBDevice(final UsbManager usbMan, final UsbDevice usbDev) throws UsbDeviceException {
    super();

    this.usbManager = usbMan;
    this.usbDevice = usbDev;

    this.openDevice();
    if(this.usbDeviceConnection == null){
        throw new UsbDeviceException("usbDeviceConnection no puede ser NULL"); //$NON-NLS-1$
    }

    if(this.usbDevice.getInterfaceCount() > 0){
        this.usbInterface = this.usbDevice.getInterface(0);
    }
}
项目:astrobee_android    文件:MainActivity.java   
@Override
protected void onStart() {
    super.onStart();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
    intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
    intentFilter.addAction(ACTION_USB_PERMISSION);
    registerReceiver(mUsbReceiver, intentFilter);

    mUsbHandler.post(new Runnable() {
        @Override
        public void run() {
            openCamera();
        }
    });

    // TODO(tfmorse): turning on screen after starting causes buttons to be wrong
}
项目:Android-Bridge-App    文件:USBConnectionManager.java   
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (action.equalsIgnoreCase(UsbManager.ACTION_USB_ACCESSORY_DETACHED)) { //Check if change in USB state
        BridgeApplication.getInstance().getBus().post(new USBConnectionEvent(false));
        Log.d(TAG, "ACTION_USB_ACCESSORY_DETACHED");
        return;
    }
    if (action.equalsIgnoreCase("android.hardware.usb.action.USB_STATE")) { //Check if change in USB state
        if (intent.getExtras().getBoolean("connected")) {
            Log.d(TAG, "USB_STATE CONNECTED");
            BridgeApplication.getInstance().getBus().post(new USBConnectionEvent(true));
        } else {
            Log.d(TAG, "USB_STATE DISCONNECTED");
            BridgeApplication.getInstance().getBus().post(new USBConnectionEvent(false));
        }
    }
}
项目:Android-Bridge-App    文件:USBConnectionManager.java   
public void checkForDJIAccessory() {
    mUsbManager = (UsbManager) BridgeApplication.getInstance().getSystemService(Context.USB_SERVICE);
    UsbAccessory[] accessoryList = mUsbManager.getAccessoryList();
    if (accessoryList != null
        && accessoryList.length > 0
        && !TextUtils.isEmpty(accessoryList[0].getManufacturer())
        && accessoryList[0].getManufacturer().equals("DJI")) {
        BridgeApplication.getInstance().getBus().post(new RCConnectionEvent(true));
        //Check permission
        mAccessory = accessoryList[0];
        if (mUsbManager.hasPermission(mAccessory)) {
            Log.d(TAG, "RC CONNECTED");
        } else {
            Log.d(TAG, "NO Permission to USB Accessory");
            DJIRemoteLogger.e(TAG, "NO Permission to USB Accessory");
            //mUsbManager.requestPermission(mAccessory, null);
        }
    } else {
        BridgeApplication.getInstance().getBus().post(new RCConnectionEvent(false));
        Log.d(TAG, "RC DISCONNECTED");
    }
}
项目:OkUSB    文件:USB.java   
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
        afterGetUsbPermission(null);
    } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
        disConnectDevice();
    } else if (ACTION_USB_PERMISSION.equals(action)) {
        synchronized (this) {
            UsbDevice usbDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
            if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                //user choose YES for your previously popup window asking for grant perssion for this usb device
                if (onUsbChangeListener != null) {
                    onUsbChangeListener.onPermissionGranted();
                }
                if (null != usbDevice) {
                    afterGetUsbPermission(usbDevice);
                }
            } else {
                //user choose NO for your previously popup window asking for grant perssion for this usb device
                if (onUsbChangeListener != null) {
                    onUsbChangeListener.onPermissionRefused();
                }
            }
        }
    }
}
项目:MobileAppForPatient    文件:PCLinkLibraryDemoActivity.java   
/**
    * 當接收到usb時,會由onResume進入
 * When receiving usb, it will be entered by onResume
    */
@Override
protected void onResume() {
    super.onResume();

       boolean fromPl2303 = false;
    Bundle bundle = getIntent().getExtras();
    if(bundle != null && bundle.containsKey(PCLinkLibraryDemoConstant.FromPL2303)) {
        fromPl2303 = bundle.getBoolean(PCLinkLibraryDemoConstant.FromPL2303); 
    }

    // 由usb attach呼叫
    //Called by usb attach
    if(getIntent().getAction() != null && getIntent().getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED) ||
            fromPl2303 == true) {
        initDetachedUsbListener();
        initBaudRate();
        initUI(true);
    }
    else {
        initUI(false);
        this.queryBluetoothDevicesAndSetToUi();
    }

    clearSharePreferences();
}
项目:5094-2016-2017    文件:FtcRobotControllerActivity.java   
@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();
      }
    }
  }
}