當前位置: 首頁>>代碼示例>>Java>>正文


Java UsbManager.openDevice方法代碼示例

本文整理匯總了Java中android.hardware.usb.UsbManager.openDevice方法的典型用法代碼示例。如果您正苦於以下問題:Java UsbManager.openDevice方法的具體用法?Java UsbManager.openDevice怎麽用?Java UsbManager.openDevice使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在android.hardware.usb.UsbManager的用法示例。


在下文中一共展示了UsbManager.openDevice方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: produceInitiator

import android.hardware.usb.UsbManager; //導入方法依賴的package包/類
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;
}
 
開發者ID:iyundong,項目名稱:InstantUpload,代碼行數:20,代碼來源:InitiatorFactory.java

示例2: onResume

import android.hardware.usb.UsbManager; //導入方法依賴的package包/類
@Override
protected void onResume() {
    super.onResume();
    changed=new HandleChange(3);
    calibCounter=0;
    Log.d(TAG, "Resumed, port=" + sPort);
    if (sPort == null) {
        mTitleTextView.setText("No serial device.");
    } else {
        usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
        List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(usbManager);
        if (availableDrivers.isEmpty()) {
            mTitleTextView.append("No drivers available\n");
            Log.d("SerialKost", "no drivers available");
            return;
        }

        UsbSerialDriver driver = availableDrivers.get(0);
        sPort = driver.getPorts().get(0);
        device=driver.getDevice();
        UsbDeviceConnection connection = usbManager.openDevice(device);
        if (connection == null) {
            mTitleTextView.setText("Opening device failed");

            PendingIntent mPermissionIntent;

            Log.i("SerialKost", "Setting PermissionIntent -> MainMenu");
            mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
            Log.i("SerialKost", "Setting IntentFilter -> MainMenu");
            IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
            Log.i("SerialKost", "Setting registerReceiver -> MainMenu");
            registerReceiver(mUsbReceiver, filter);
            Log.i("SerialKost", "Setting requestPermission -> MainMenu");
            usbManager.requestPermission(device, mPermissionIntent);
            return;
        }
        communicate();
    }
}
 
開發者ID:kost,項目名稱:DroidMeter,代碼行數:40,代碼來源:SerialConsoleActivity.java

示例3: startConnecting

import android.hardware.usb.UsbManager; //導入方法依賴的package包/類
private void startConnecting() {
	handleConnecting();
	try {
		UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
		deviceConnection = usbManager.openDevice(device);

		driver = UsbSerialDriverFactory.createDriver(device, deviceConnection);
		UsbEndpoint[] endpoints = driver.open();
		readEndpoint = endpoints[0];
		writeEndpoint = endpoints[1];

		driver.setParameters(115200, 8, UsbSerialDriver.STOPBITS_1, UsbSerialDriver.PARITY_NONE);
	} catch (Exception e) {
		Log.e(LOG_TAG, "Failed to connect to USB device(" + device.getDeviceName() + "). cause=" + e.getMessage());
		cleanUp();
		handleFailedToConnect(e);
		return;
	}

	// Start a thread for reading
	readThread = new UsbReadThread();
	readThread.start();

	Log.i(LOG_TAG, "USB device is connected! connection=" + getDescription());
	handleConnected();
}
 
開發者ID:Neofect,項目名稱:Communicator,代碼行數:27,代碼來源:UsbConnection.java

示例4: obtainFdFor

import android.hardware.usb.UsbManager; //導入方法依賴的package包/類
public static Future<UsbDeviceConnection> obtainFdFor(Context context, UsbDevice usbDevice) {
    UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
    if (!manager.hasPermission(usbDevice)) {
        AsyncFuture<UsbDeviceConnection> task = new AsyncFuture<>();
        registerNewBroadcastReceiver(context, usbDevice, task);
        manager.requestPermission(usbDevice, PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0));
        return task;
    } else {
        return new CompletedFuture<>(manager.openDevice(usbDevice));
    }
}
 
開發者ID:martinmarinov,項目名稱:AndroidDvbDriver,代碼行數:12,代碼來源:UsbPermissionObtainer.java

示例5: startSerialConnection

import android.hardware.usb.UsbManager; //導入方法依賴的package包/類
private void startSerialConnection(UsbManager usbManager, UsbDevice device) {
    UsbDeviceConnection connection = usbManager.openDevice(device);
    serial = UsbSerialDevice.createUsbSerialDevice(device, connection);

    if (serial != null && serial.open()) {
        serial.setBaudRate(115200);
        serial.setDataBits(UsbSerialInterface.DATA_BITS_8);
        serial.setStopBits(UsbSerialInterface.STOP_BITS_1);
        serial.setParity(UsbSerialInterface.PARITY_NONE);
        serial.setFlowControl(UsbSerialInterface.FLOW_CONTROL_OFF);
        serial.setDTR(false);
    }
}
 
開發者ID:carlemil,項目名稱:RPLidarA2AndroidThingsAPI,代碼行數:14,代碼來源:RPLidarA2Api.java

示例6: acquire

import android.hardware.usb.UsbManager; //導入方法依賴的package包/類
public static UsbHidDriver acquire(UsbManager usbManager, UsbDevice device) {
    if (device != null) {
        final UsbDeviceConnection mConnection = usbManager.openDevice(device);

        return new UsbHidDriver(device, mConnection);
    }

    return null;
}
 
開發者ID:pazaan,項目名稱:600SeriesAndroidUploader,代碼行數:10,代碼來源:UsbHidDriver.java

示例7: closeDevice

import android.hardware.usb.UsbManager; //導入方法依賴的package包/類
/**
 * USBシリアルデバイスクローズ.
 */
private void closeDevice() {
    Iterator<UsbSerialDevice> index = mUsbSerialDevices.iterator();

    while (index.hasNext()) {
        UsbSerialDevice device = index.next();
        final UsbManager mUsbManager = (UsbManager) this.getSystemService(Context.USB_SERVICE);
        UsbDeviceConnection connection = mUsbManager.openDevice(device.getSerialPort().getDriver().getDevice());
        if (connection == null) {
            SerialInputOutputManager manager = device.getSerialInputOutputManager();
            if (manager != null) {
                manager.stop();
            }
            device.setSerialInputOutputManager(null);

            UsbSerialPort serialPort = device.getSerialPort();
            if (serialPort != null) {
                try {
                    serialPort.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            device.setSerialPort(null);
            setOnlineStatus(device.getServiceId(), false, false);
            index.remove();
        }
    }
}
 
開發者ID:DeviceConnect,項目名稱:DeviceConnect-Android,代碼行數:32,代碼來源:SmartMeterMessageService.java

示例8: openInterface

import android.hardware.usb.UsbManager; //導入方法依賴的package包/類
private UsbDeviceConnection openInterface(UsbManager manager,
        UsbDevice device, UsbInterface iface)
        throws UsbDeviceException {
    UsbDeviceConnection connection = manager.openDevice(device);
    if(connection == null) {
        throw new UsbDeviceException("Couldn't open a connection to " +
                "device -- user may not have given permission");
    }
    mInterface = iface;
    connection.claimInterface(mInterface, true);
    return connection;
}
 
開發者ID:bibhrajit,項目名稱:openxc-androidStudio,代碼行數:13,代碼來源:UsbVehicleInterface.java

示例9: connectUSB

import android.hardware.usb.UsbManager; //導入方法依賴的package包/類
public void connectUSB(UsbManager mgr, UsbDevice device) {
    UsbInterface device_interface = device.getInterface(0);
    _connection = mgr.openDevice(device);

    if (_connection != null) {
        _connection.claimInterface(device_interface, true);
        _endpoint_in = device_interface.getEndpoint(1);
        _endpoint_out = device_interface.getEndpoint(2);
        _max_packet_size = _endpoint_in.getMaxPacketSize();
        _packet_buffer = ByteBuffer.allocate(_max_packet_size);
        _packet_buffer.order(ByteOrder.LITTLE_ENDIAN);
        _result_buffer = ByteBuffer.allocate(4096);
    }
}
 
開發者ID:GeoSpark,項目名稱:scoperoid,代碼行數:15,代碼來源:Scope.java

示例10: UsbMidiDeviceAndroid

import android.hardware.usb.UsbManager; //導入方法依賴的package包/類
/**
 * Constructs a UsbMidiDeviceAndroid.
 * @param manager
 * @param device The USB device which this object is assocated with.
 */
UsbMidiDeviceAndroid(UsbManager manager, UsbDevice device) {
    mConnection = manager.openDevice(device);
    mEndpointMap = new SparseArray<UsbEndpoint>();
    mRequestMap = new HashMap<UsbEndpoint, UsbRequest>();
    mHandler = new Handler();
    mUsbDevice = device;
    mIsClosed = false;
    mHasInputThread = false;
    mNativePointer = 0;

    for (int i = 0; i < device.getInterfaceCount(); ++i) {
        UsbInterface iface = device.getInterface(i);
        if (iface.getInterfaceClass() != UsbConstants.USB_CLASS_AUDIO
                || iface.getInterfaceSubclass() != MIDI_SUBCLASS) {
            continue;
        }
        mConnection.claimInterface(iface, true);
        for (int j = 0; j < iface.getEndpointCount(); ++j) {
            UsbEndpoint endpoint = iface.getEndpoint(j);
            if (endpoint.getDirection() == UsbConstants.USB_DIR_OUT) {
                mEndpointMap.put(endpoint.getEndpointNumber(), endpoint);
            }
        }
    }
    // Start listening for input endpoints.
    // This function will create and run a thread if there is USB-MIDI endpoints in the
    // device. Note that because UsbMidiDevice is shared among all tabs and the thread
    // will be terminated when the device is disconnected, at most one thread can be created
    // for each connected USB-MIDI device.
    startListen(device);
}
 
開發者ID:mogoweb,項目名稱:365browser,代碼行數:37,代碼來源:UsbMidiDeviceAndroid.java

示例11: onResume

import android.hardware.usb.UsbManager; //導入方法依賴的package包/類
@Override
public void onResume() {
    super.onResume();
    if (currentTimeLeft != 0) {
        countDownTimerWithPause = new GameCountDownTimer(currentTimeLeft, UPDATE_INTERVAL, NOT_RUN_AFTER_CREATION);
    } else {
        countDownTimerWithPause = new GameCountDownTimer(GAME_TIME_DEFAULT, UPDATE_INTERVAL, NOT_RUN_AFTER_CREATION);
    }

    final UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
    List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(usbManager);
    if (availableDrivers.isEmpty()) {
        Toast.makeText(this, "USB devices Not found", Toast.LENGTH_SHORT).show();
    } else {
        UsbSerialDriver driver = availableDrivers.get(0);
        usbSerialPort = driver.getPorts().get(0);

        UsbDeviceConnection connection = usbManager.openDevice(usbSerialPort.getDriver().getDevice());
        if (connection == null) {
            return;
        }

        try {
            usbSerialPort.open(connection);
            usbSerialPort.setParameters(BAUD_RATE, UsbSerialPort.DATABITS_8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
        } catch (IOException e) {
            Log.e(TAG, "Error setting up device: " + e.getMessage(), e);
            try {
                usbSerialPort.close();
            } catch (IOException e2) {
                Log.e(TAG, "Close serial port", e2);
            }
            usbSerialPort = null;
            return;
        }
        onDeviceStateChange();
    }
}
 
開發者ID:handsomecode,項目名稱:smart-foosball,代碼行數:39,代碼來源:ScoreboardActivity.java

示例12: onResume

import android.hardware.usb.UsbManager; //導入方法依賴的package包/類
@Override
protected void onResume() {
    super.onResume();
    Log.d(TAG, "Resumed, port=" + sPort);
    if (sPort == null) {
        mTitleTextView.setText("No serial device.");
    } else {
        final UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);

        UsbDeviceConnection connection = usbManager.openDevice(sPort.getDriver().getDevice());
        if (connection == null) {
            mTitleTextView.setText("Opening device failed");
            return;
        }

        try {
            sPort.open(connection);
            sPort.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
        } catch (IOException e) {
            Log.e(TAG, "Error setting up device: " + e.getMessage(), e);
            mTitleTextView.setText("Error opening device: " + e.getMessage());
            try {
                sPort.close();
            } catch (IOException e2) {
                // Ignore.
            }
            sPort = null;
            return;
        }
        mTitleTextView.setText("Serial device: " + sPort.getClass().getSimpleName());
    }
    onDeviceStateChange();
}
 
開發者ID:rodolfo3,項目名稱:android-vusb-arduino,代碼行數:34,代碼來源:SerialConsoleActivity.java

示例13: onResume

import android.hardware.usb.UsbManager; //導入方法依賴的package包/類
@Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "Resumed, port=" + sPort);
        if (sPort == null) {
            mTitleTextView.setText(getString(R.string.no_device));
        } else {
            final UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);

            UsbDeviceConnection connection = usbManager.openDevice(sPort.getDriver().getDevice());
            if (connection == null) {
                mTitleTextView.setText(getString(R.string.no_open));
                return;
            }

            try {
                sPort.open(connection);
                sPort.setParameters(baud, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);

                showStatus(mDumpTextView, "CD  - Carrier Detect", sPort.getCD());
                showStatus(mDumpTextView, "CTS - Clear To Send", sPort.getCTS());
                showStatus(mDumpTextView, "DSR - Data Set Ready", sPort.getDSR());
                showStatus(mDumpTextView, "DTR - Data Terminal Ready", sPort.getDTR());
                showStatus(mDumpTextView, "DSR - Data Set Ready", sPort.getDSR());
                showStatus(mDumpTextView, "RI  - Ring Indicator", sPort.getRI());
                showStatus(mDumpTextView, "RTS - Request To Send", sPort.getRTS());
                String msg = "Baud rate : " + baud + "\n\n";
                mDumpTextView.append(msg);
// test: prints list of running AppProcess:
                /*
                ActivityManager activityManager = (ActivityManager) SerialConsoleActivity.this.getSystemService(ACTIVITY_SERVICE);
                List<ActivityManager.RunningAppProcessInfo> runningAppList = activityManager.getRunningAppProcesses();
                for (ActivityManager.RunningAppProcessInfo processInfo : runningAppList ){
                    String processName = processInfo.processName;
                    mDumpTextView.append(processName+"\n");
                }
                */
            } catch (IOException e) {
                Log.e(TAG, "Error setting up device: " + e.getMessage(), e);
                mTitleTextView.setText(getString(R.string.err_device) + " " + e.getMessage());
                try {
                    sPort.close();
                } catch (IOException e2) {
                    // Ignore.
                }
                sPort = null;
                return;
            }
            mTitleTextView.setText(getString(R.string.ok_device) + " " + sPort.getClass().getSimpleName() + " (" + sPort.getDriver().getDevice().getDeviceName() + ")");
        }
        onDeviceStateChange();
    }
 
開發者ID:msillano,項目名稱:USBphpTunnel,代碼行數:53,代碼來源:SerialConsoleActivity.java

示例14: serialConnectionForDevice

import android.hardware.usb.UsbManager; //導入方法依賴的package包/類
protected UsbDeviceConnection serialConnectionForDevice(UsbManager manager, UsbSerialDriver driver) {
    return manager.openDevice(driver.getDevice());
}
 
開發者ID:ekumenlabs,項目名稱:tangobot,代碼行數:4,代碼來源:UsbDeviceNodeLoader.java

示例15: AndroidUsb4JArduino

import android.hardware.usb.UsbManager; //導入方法依賴的package包/類
public AndroidUsb4JArduino(AndroidUsbProtocolConfiguration configuration) {
	// Get UsbManager from Android.
	Context context = configuration.getContext();
	UsbManager manager = (UsbManager)context.getSystemService(Context.USB_SERVICE);

       usblist = UsbSerialProber.getDefaultProber().findAllDrivers(manager);
       /*
       if(usblist.size()==0){
           new AlertDialog.Builder(context)
                   .setTitle("Arduino Not Connected!!!")
                   .setMessage("Waiting for you to connect Because i cannot")
                   .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int which) {
                           // continue with delete
                       }
                   })
                   .setIcon(android.R.drawable.ic_dialog_alert)
                   .show();
       }*/

       while(usblist.size()==0){
           SystemClock.sleep(1500);
           usblist = UsbSerialProber.getDefaultProber().findAllDrivers(manager);
       }

       UsbSerialDriver usbserial = usblist.get(0);
               UsbDeviceConnection connection = manager.openDevice(usbserial.getDevice());
               if (connection == null) {
                   Log.d("mee", "No Connection");
                   return;
               }
               port = usbserial.getPorts().get(0);
               try {
                   port.open(connection);
                   port.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
               } catch (IOException e) {
                   Log.d("mee","Port not opened");
                   e.printStackTrace();
               }
             //Toast.makeText(context,"Connected Now!!",Toast.LENGTH_SHORT);
			reader = new Thread(this);
			reader.start();
}
 
開發者ID:meedeepak,項目名稱:Arduino-android-serial-communication,代碼行數:44,代碼來源:AndroidUsb4JArduino.java


注:本文中的android.hardware.usb.UsbManager.openDevice方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。