com.hoho.android.usbserial.driver.UsbSerialProber Java Examples

The following examples show how to use com.hoho.android.usbserial.driver.UsbSerialProber. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DevicesFragment.java    From SimpleUsbTerminal with MIT License 6 votes vote down vote up
void refresh() {
    UsbManager usbManager = (UsbManager) getActivity().getSystemService(Context.USB_SERVICE);
    UsbSerialProber usbDefaultProber = UsbSerialProber.getDefaultProber();
    UsbSerialProber usbCustomProber = CustomProber.getCustomProber();
    listItems.clear();
    for(UsbDevice device : usbManager.getDeviceList().values()) {
        UsbSerialDriver driver = usbDefaultProber.probeDevice(device);
        if(driver == null) {
            driver = usbCustomProber.probeDevice(device);
        }
        if(driver != null) {
            for(int port = 0; port < driver.getPorts().size(); port++)
                listItems.add(new ListItem(device, port, driver));
        } else {
            listItems.add(new ListItem(device, 0, null));
        }
    }
    listAdapter.notifyDataSetChanged();
}
 
Example #2
Source File: ReactUsbSerialModule.java    From react-native-usbserial with MIT License 6 votes vote down vote up
private UsbSerialDriver getUsbSerialDriver(int prodId, UsbManager manager) throws Exception {

        if (prodId == 0)
            throw new Error(new Error("The deviceObject is not a valid 'UsbDevice' reference"));

        List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager);

        // Reject if no driver is available
        if (availableDrivers.isEmpty())
            throw new Exception("No available drivers to communicate with devices");

        for (UsbSerialDriver drv : availableDrivers) {

            if (drv.getDevice().getProductId() == prodId)
                return drv;
        }

        // Reject if no driver exists for the current productId
        throw new Exception(String.format("No driver found for productId '%s'", prodId));
    }
 
Example #3
Source File: DevicesFragment.java    From usb-serial-for-android with GNU Lesser General Public License v2.1 6 votes vote down vote up
void refresh() {
    UsbManager usbManager = (UsbManager) getActivity().getSystemService(Context.USB_SERVICE);
    UsbSerialProber usbDefaultProber = UsbSerialProber.getDefaultProber();
    UsbSerialProber usbCustomProber = CustomProber.getCustomProber();
    listItems.clear();
    for(UsbDevice device : usbManager.getDeviceList().values()) {
        UsbSerialDriver driver = usbDefaultProber.probeDevice(device);
        if(driver == null) {
            driver = usbCustomProber.probeDevice(device);
        }
        if(driver != null) {
            for(int port = 0; port < driver.getPorts().size(); port++)
                listItems.add(new ListItem(device, port, driver));
        } else {
            listItems.add(new ListItem(device, 0, null));
        }
    }
    listAdapter.notifyDataSetChanged();
}
 
Example #4
Source File: SnifferDeviceService.java    From sniffer154 with GNU General Public License v3.0 6 votes vote down vote up
private void setupUsbDevice() {
	mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
	mSerialDevice = UsbSerialProber.acquire(mUsbManager);
	if (mSerialDevice == null) {
		Toast.makeText(this, "Cannot find USB device", Toast.LENGTH_SHORT)
				.show();
	} else {
		try {
			mSerialDevice.open();
		} catch (IOException e) {
			Log.e(TAG, "Error setting up device: " + e.getMessage(), e);
			try {
				mSerialDevice.close();
			} catch (IOException e2) {
				// Ignore.
			}
			mSerialDevice = null;
			return;
		}
	}
	mSerialIoManager = new SerialInputOutputManager(mSerialDevice,
			mListener);
	mExecutor.submit(mSerialIoManager);
}
 
Example #5
Source File: MainActivity.java    From Chorus-RF-Laptimer with MIT License 5 votes vote down vote up
private UsbDevice getAvailableUsbDevice() {
    UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);

    // Find all available drivers from attached devices.
    List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager);
    if (availableDrivers.isEmpty()) {
        return null;
    }

    // Open a connection to the first available driver.
    UsbSerialDriver driver = availableDrivers.get(0);
    return driver.getDevice();
}
 
Example #6
Source File: UsbDeviceNodeLoader.java    From tangobot with Apache License 2.0 5 votes vote down vote up
/**
 * Internal helper method that returns a single UsbSerialDriver from the android_usb_serial
 * library given a UsbDevice for which we already have access to
 */
protected UsbSerialDriver serialDriverForDevice(UsbDevice device, UsbManager usbManager) throws Exception {
    // Wrap the UsbDevice in the HoHo Driver
    List<UsbSerialDriver> driverList = UsbSerialProber.getDefaultProber().findAllDrivers(usbManager);
    // For now, continue only if we have a single driver in the list
    if (driverList.isEmpty()) {
        throw new Exception("No drivers found for the supplied USB device: " + device);
    }
    if (driverList.size() > 1) {
        log.warn("There are " + driverList.size() + " drivers found for the provided USB device: "
                + device + ". Will continue using the first one in the list");
    }
    return driverList.get(0);
}
 
Example #7
Source File: CustomProber.java    From SimpleUsbTerminal with MIT License 4 votes vote down vote up
static UsbSerialProber getCustomProber() {
    ProbeTable customTable = new ProbeTable();
    customTable.addProduct(0x16d0, 0x087e, CdcAcmSerialDriver.class); // e.g. Digispark CDC
    return new UsbSerialProber(customTable);
}
 
Example #8
Source File: TerminalFragment.java    From SimpleUsbTerminal with MIT License 4 votes vote down vote up
private void connect(Boolean permissionGranted) {
    UsbDevice device = null;
    UsbManager usbManager = (UsbManager) getActivity().getSystemService(Context.USB_SERVICE);
    for(UsbDevice v : usbManager.getDeviceList().values())
        if(v.getDeviceId() == deviceId)
            device = v;
    if(device == null) {
        status("connection failed: device not found");
        return;
    }
    UsbSerialDriver driver = UsbSerialProber.getDefaultProber().probeDevice(device);
    if(driver == null) {
        driver = CustomProber.getCustomProber().probeDevice(device);
    }
    if(driver == null) {
        status("connection failed: no driver for device");
        return;
    }
    if(driver.getPorts().size() < portNum) {
        status("connection failed: not enough ports at device");
        return;
    }
    usbSerialPort = driver.getPorts().get(portNum);
    UsbDeviceConnection usbConnection = usbManager.openDevice(driver.getDevice());
    if(usbConnection == null && permissionGranted == null && !usbManager.hasPermission(driver.getDevice())) {
        PendingIntent usbPermissionIntent = PendingIntent.getBroadcast(getActivity(), 0, new Intent(Constants.INTENT_ACTION_GRANT_USB), 0);
        usbManager.requestPermission(driver.getDevice(), usbPermissionIntent);
        return;
    }
    if(usbConnection == null) {
        if (!usbManager.hasPermission(driver.getDevice()))
            status("connection failed: permission denied");
        else
            status("connection failed: open failed");
        return;
    }

    connected = Connected.Pending;
    try {
        usbSerialPort.open(usbConnection);
        usbSerialPort.setParameters(baudRate, UsbSerialPort.DATABITS_8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
        SerialSocket socket = new SerialSocket(getActivity().getApplicationContext(), usbConnection, usbSerialPort);
        service.connect(socket);
        // usb connect is not asynchronous. connect-success and connect-error are returned immediately from socket.connect
        // for consistency to bluetooth/bluetooth-LE app use same SerialListener and SerialService classes
        onSerialConnect();
    } catch (Exception e) {
        onSerialConnectError(e);
    }
}
 
Example #9
Source File: SerialInterface_USBSerial.java    From PodEmu with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Initilize the device
 * @param context - application context
 * @return - true on success, false on failure
 */
public boolean init(Context context)
{
    PodEmuLog.debug("USBSerial: initialization started.");
    UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);

    // Find all available drivers from attached devices.
    List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(usbManager);
    if (availableDrivers.isEmpty())
    {
        PodEmuLog.debug("USBSerial: no devices found. Exiting...");
        return false;
    }

    // Open a connection to the first available driver.
    UsbSerialDriver driver = availableDrivers.get(0);
    connection = usbManager.openDevice(driver.getDevice());
    if (connection == null)
    {
        // You probably need to call UsbManager.requestPermission(driver.getDevice(), ..)
        PodEmuLog.log("USBSerial: Cannot establish serial connection! Exiting...");
        return false;
    }

    // Read some data! Most have just one port (port 0).
    List<UsbSerialPort> ports = driver.getPorts();
    port = ports.get(0);
    try {
        PodEmuLog.debug("USBSerial: openning connection with baud rate="+baudRate);
        port.open(connection);
        port.setParameters(baudRate, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
        PodEmuService.communicateSerialStatusChange();
        PodEmuLog.debug("USBSerial: connection succesfully open");
    }
    catch (IOException e)
    {
        // TODO Deal with error
        PodEmuLog.debug("USBSerial: unknown exception occured! See trace log below:");
        PodEmuLog.error(e.getMessage());
        return false;
    }

    return true;
}
 
Example #10
Source File: SerialConnector.java    From Arduino-Serial-Controller with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void initialize() {
		UsbManager manager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);
		
		List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager);
		if (availableDrivers.isEmpty()) {
			mListener.onReceive(Constants.MSG_SERIAL_ERROR, 0, 0, "Error: There is no available device. \n", null);
			return;
		}
		
		mDriver = availableDrivers.get(0);
		if(mDriver == null) {
			mListener.onReceive(Constants.MSG_SERIAL_ERROR, 0, 0, "Error: Driver is Null \n", null);
			return;
		}
		
		// Report to UI
		StringBuilder sb = new StringBuilder();
		UsbDevice device = mDriver.getDevice();
		sb.append(" DName : ").append(device.getDeviceName()).append("\n")
			.append(" DID : ").append(device.getDeviceId()).append("\n")
			.append(" VID : ").append(device.getVendorId()).append("\n")
			.append(" PID : ").append(device.getProductId()).append("\n")
			.append(" IF Count : ").append(device.getInterfaceCount()).append("\n");
		mListener.onReceive(Constants.MSG_DEVICD_INFO, 0, 0, sb.toString(), null);
		
		UsbDeviceConnection connection = manager.openDevice(device);
		if (connection == null) {
			mListener.onReceive(Constants.MSG_SERIAL_ERROR, 0, 0, "Error: Cannot connect to device. \n", null);
			return;
		}
		
		// Read some data! Most have just one port (port 0).
		mPort = mDriver.getPorts().get(0);
		if(mPort == null) {
			mListener.onReceive(Constants.MSG_SERIAL_ERROR, 0, 0, "Error: Cannot get port. \n", null);
			return;
		}
		
		try {
			mPort.open(connection);
			mPort.setParameters(9600, 8, 1, 0);		// baudrate:9600, dataBits:8, stopBits:1, parity:N
//			byte buffer[] = new byte[16];
//			int numBytesRead = mPort.read(buffer, 1000);
//			Log.d(TAG, "Read " + numBytesRead + " bytes.");
		} catch (IOException e) {
			// Deal with error.
			mListener.onReceive(Constants.MSG_SERIAL_ERROR, 0, 0, "Error: Cannot open port \n" + e.toString() + "\n", null);
		} finally {
		}
		
		// Everything is fine. Start serial monitoring thread.
		startThread();
	}
 
Example #11
Source File: CustomProber.java    From usb-serial-for-android with GNU Lesser General Public License v2.1 4 votes vote down vote up
static UsbSerialProber getCustomProber() {
    ProbeTable customTable = new ProbeTable();
    customTable.addProduct(0x16d0, 0x087e, CdcAcmSerialDriver.class); // e.g. Digispark CDC
    return new UsbSerialProber(customTable);
}
 
Example #12
Source File: TerminalFragment.java    From usb-serial-for-android with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void connect() {
    UsbDevice device = null;
    UsbManager usbManager = (UsbManager) getActivity().getSystemService(Context.USB_SERVICE);
    for(UsbDevice v : usbManager.getDeviceList().values())
        if(v.getDeviceId() == deviceId)
            device = v;
    if(device == null) {
        status("connection failed: device not found");
        return;
    }
    UsbSerialDriver driver = UsbSerialProber.getDefaultProber().probeDevice(device);
    if(driver == null) {
        driver = CustomProber.getCustomProber().probeDevice(device);
    }
    if(driver == null) {
        status("connection failed: no driver for device");
        return;
    }
    if(driver.getPorts().size() < portNum) {
        status("connection failed: not enough ports at device");
        return;
    }
    usbSerialPort = driver.getPorts().get(portNum);
    UsbDeviceConnection usbConnection = usbManager.openDevice(driver.getDevice());
    if(usbConnection == null && usbPermission == UsbPermission.Unknown && !usbManager.hasPermission(driver.getDevice())) {
        usbPermission = UsbPermission.Requested;
        PendingIntent usbPermissionIntent = PendingIntent.getBroadcast(getActivity(), 0, new Intent(INTENT_ACTION_GRANT_USB), 0);
        usbManager.requestPermission(driver.getDevice(), usbPermissionIntent);
        return;
    }
    if(usbConnection == null) {
        if (!usbManager.hasPermission(driver.getDevice()))
            status("connection failed: permission denied");
        else
            status("connection failed: open failed");
        return;
    }

    try {
        usbSerialPort.open(usbConnection);
        usbSerialPort.setParameters(baudRate, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
        if(withIoManager) {
            usbIoManager = new SerialInputOutputManager(usbSerialPort, this);
            Executors.newSingleThreadExecutor().submit(usbIoManager);
        }
        status("connected");
        connected = true;
        controlLines.start();
    } catch (Exception e) {
        status("connection failed: " + e.getMessage());
        disconnect();
    }
}