本文整理汇总了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;
}
示例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();
}
}
示例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();
}
示例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));
}
}
示例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);
}
}
示例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;
}
示例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();
}
}
}
示例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;
}
示例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);
}
}
示例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);
}
示例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();
}
}
示例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();
}
示例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();
}
示例14: serialConnectionForDevice
import android.hardware.usb.UsbManager; //导入方法依赖的package包/类
protected UsbDeviceConnection serialConnectionForDevice(UsbManager manager, UsbSerialDriver driver) {
return manager.openDevice(driver.getDevice());
}
示例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();
}