Example usage for android.hardware.usb UsbDevice getDeviceId

List of usage examples for android.hardware.usb UsbDevice getDeviceId

Introduction

In this page you can find the example usage for android.hardware.usb UsbDevice getDeviceId.

Prototype

public int getDeviceId() 

Source Link

Document

Returns a unique integer ID for the device.

Usage

From source file:org.chromium.ChromeUsb.java

private void getDevices(CordovaArgs args, JSONObject params, final CallbackContext callbackContext)
        throws JSONException, UsbError {
    HashMap<String, UsbDevice> devices = mUsbManager.getDeviceList();
    JSONArray result = new JSONArray();
    for (UsbDevice device : devices.values()) {
        addDeviceToArray(result, device.getDeviceId(), device.getVendorId(), device.getProductId());
    }//from   www . j a  v  a  2  s  .c  o m
    if (params.optBoolean("appendFakeDevice", false)) {
        addDeviceToArray(result, FakeDevice.ID, FakeDevice.VID, FakeDevice.PID);
    }
    callbackContext.success(result);
}

From source file:org.chromium.ChromeUsb.java

private void openDevice(CordovaArgs args, JSONObject params, final CallbackContext callbackContext)
        throws JSONException, UsbError {
    // First recover the device object from Id.
    int devId = params.getInt("device");
    ConnectedDevice dev = null;//from w  w  w . ja va2 s  .  c  o m
    int vid = -1, pid = -1;
    {
        HashMap<String, UsbDevice> devices = mUsbManager.getDeviceList();
        UsbDevice usbDev = null;
        for (UsbDevice d : devices.values()) {
            if (d.getDeviceId() == devId) {
                usbDev = d;
                break;
            }
        }
        if (usbDev != null) {
            if (mUsbReceiver == null) {
                mUsbReceiver = new BroadcastReceiver() {
                    public void onReceive(Context context, Intent intent) {
                        String action = intent.getAction();
                        if (ACTION_USB_PERMISSION.equals(action)) {
                            synchronized (this) {
                                UsbDevice device = (UsbDevice) intent
                                        .getParcelableExtra(UsbManager.EXTRA_DEVICE);

                                if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                                    if (device != null) {
                                        UsbDeviceConnection usbConn = mUsbManager.openDevice(device);
                                        if (usbConn == null) {
                                            throw new UsbError(
                                                    "UsbManager.openDevice returned null opening " + device);
                                        }
                                        ConnectedDevice dev = new RealDevice(device, usbConn);
                                        int vid = device.getVendorId();
                                        int pid = device.getProductId();

                                        if (dev == null || vid < 0 || pid < 0) {
                                            throw new UsbError("Unknown device ID: " + device);
                                        }
                                        int handle = mNextConnectionId++;
                                        mConnections.put(handle, dev);
                                        JSONObject jsonHandle = new JSONObject();
                                        try {
                                            jsonHandle.put("handle", handle);
                                            jsonHandle.put("vendorId", vid);
                                            jsonHandle.put("productId", pid);
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                        callbackContext.success(jsonHandle);
                                    }
                                } else {
                                    Log.d(TAG, "permission denied for device " + device);
                                }
                            }
                        }
                    }
                };

                IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
                webView.getContext().registerReceiver(mUsbReceiver, filter);
            }

            mUsbManager.requestPermission(usbDev, mPermissionIntent);
        } else if (devId == FakeDevice.ID) {
            dev = new FakeDevice();
            vid = FakeDevice.VID;
            pid = FakeDevice.PID;

            if (dev == null || vid < 0 || pid < 0) {
                throw new UsbError("Unknown device ID: " + devId);
            }
            int handle = mNextConnectionId++;
            mConnections.put(handle, dev);
            JSONObject jsonHandle = new JSONObject();
            jsonHandle.put("handle", handle);
            jsonHandle.put("vendorId", vid);
            jsonHandle.put("productId", pid);
            callbackContext.success(jsonHandle);
        }
    }
}