本文整理汇总了Java中com.hoho.android.usbserial.driver.UsbSerialProber类的典型用法代码示例。如果您正苦于以下问题:Java UsbSerialProber类的具体用法?Java UsbSerialProber怎么用?Java UsbSerialProber使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
UsbSerialProber类属于com.hoho.android.usbserial.driver包,在下文中一共展示了UsbSerialProber类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: findUsbSerialPort
import com.hoho.android.usbserial.driver.UsbSerialProber; //导入依赖的package包/类
private UsbSerialPort findUsbSerialPort(ConfigStorage.SerialPortIdentifier identifier) {
final List<UsbSerialDriver> drivers =
UsbSerialProber.getDefaultProber().findAllDrivers(mUsbManager);
for (final UsbSerialDriver driver : drivers) {
for (final UsbSerialPort port : driver.getPorts()) {
UsbDevice device = port.getDriver().getDevice();
if (device != null
&& device.getVendorId() == identifier.getVendorId()
&& device.getProductId() == identifier.getProductId()
&& Objects.equals(device.getSerialNumber(), identifier.getSerialNumber())) {
return port;
}
}
}
return null;
}
示例2: onResume
import com.hoho.android.usbserial.driver.UsbSerialProber; //导入依赖的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: getUsbSerialDriver
import com.hoho.android.usbserial.driver.UsbSerialProber; //导入依赖的package包/类
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));
}
示例4: openUsbConnection
import com.hoho.android.usbserial.driver.UsbSerialProber; //导入依赖的package包/类
@Override
protected void openUsbConnection() throws IOException {
registerUsbPermissionBroadcastReceiver();
// Get UsbManager from Android.
UsbManager manager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);
//Get the list of available devices
List<UsbDevice> availableDevices = UsbSerialProber.getAvailableSupportedDevices(manager);
if(availableDevices.isEmpty()){
Log.d("USB", "No Devices found");
throw new IOException("No Devices found");
}
//Pick the first device
UsbDevice device = availableDevices.get(0);
if(manager.hasPermission(device)){
openUsbDevice(device);
}
else{
//TODO: complete implementation
// manager.requestPermission(device, usbPermissionIntent);
throw new IOException("No permission to access usb device " + device.getDeviceName());
}
}
示例5: LogKeeper
import com.hoho.android.usbserial.driver.UsbSerialProber; //导入依赖的package包/类
/*********************************************************************************
* Class methods
*********************************************************************************/
// Create new LogKeeper with Context
private LogKeeper(Context ctx){
mContext = ctx;
mEntries = new LinkedList<>();
// From serial driver
mManager = (UsbManager) ctx.getSystemService(Context.USB_SERVICE);
List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(mManager);
// Collect relevant intents
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
mContext.registerReceiver(mUsbReceiver, filter);
// Handle if already connected
if (!availableDrivers.isEmpty()) {
connectDevice();
}
}
示例6: bindToUsbDevice
import com.hoho.android.usbserial.driver.UsbSerialProber; //导入依赖的package包/类
/**
* Probes available USB devices and sets serialDriver and serialDriverUsbName if available.
*/
protected void bindToUsbDevice(){
UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceMap = manager.getDeviceList();
// Find the first available driver.
final UsbSerialDriver driver = UsbSerialProber.findFirstDevice(manager);
serialDriver = (CommonUsbSerialDriver)driver;
serialDriverUsbName = null;
if(serialDriver == null){
return;
}
UsbDevice device = serialDriver.getDevice();
//Reverse engineer the USB name for presence testing in the future.
for(String key : deviceMap.keySet()){
if(deviceMap.get(key).equals(device)){
serialDriverUsbName = key;
break;
}
}
if(serialDriverUsbName == null){
Log.e(TAG, "Could not find the USB name from the serial driver we just got... what?");
}
}
示例7: onResume
import com.hoho.android.usbserial.driver.UsbSerialProber; //导入依赖的package包/类
public void onResume() {
if (driver == null) {
driver = UsbSerialProber.acquire(usbManager);
// Log.d(TAG, "Resumed, mSerialDevice=" + driver);
if (driver == null) {
// No serial device.
} else {
try {
driver.open();
onDeviceStateChange();
} catch (IOException e) {
// Log.e(TAG, "Error setting up device: " + e.getMessage(), e);
try {
driver.close();
onDeviceStateChange();
} catch (IOException e2) {
// Ignore.
}
driver = null;
return;
}
// Cans has a device.
}
}
}
示例8: onResume
import com.hoho.android.usbserial.driver.UsbSerialProber; //导入依赖的package包/类
@Override
protected void onResume() {
super.onResume();
mSerialDevice = UsbSerialProber.acquire(mUsbManager);
Log.d(TAG, "Resumed, mSerialDevice=" + mSerialDevice);
if (mSerialDevice == null) {
mTitleTextView.setText("No serial device.");
} else {
try {
mSerialDevice.open();
} catch (IOException e) {
Log.e(TAG, "Error setting up device: " + e.getMessage(), e);
mTitleTextView.setText("Error opening device: " + e.getMessage());
try {
mSerialDevice.close();
} catch (IOException e2) {
// Ignore.
}
mSerialDevice = null;
return;
}
mTitleTextView.setText("Serial device: " + mSerialDevice);
}
onDeviceStateChange();
}
示例9: setupUsbDevice
import com.hoho.android.usbserial.driver.UsbSerialProber; //导入依赖的package包/类
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);
}
示例10: serialDriverForDevice
import com.hoho.android.usbserial.driver.UsbSerialProber; //导入依赖的package包/类
/**
* 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);
}
示例11: openUsbDevice
import com.hoho.android.usbserial.driver.UsbSerialProber; //导入依赖的package包/类
private void openUsbDevice(UsbDevice device) throws IOException {
// Get UsbManager from Android.
UsbManager manager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);
// Find the first available driver.
sDriver = UsbSerialProber.openUsbDevice(manager, device);
if (sDriver == null) {
Log.d("USB", "No Devices found");
throw new IOException("No Devices found");
} else {
Log.d("USB", "Opening using Baud rate " + mBaudRate);
try {
sDriver.open();
sDriver.setParameters(mBaudRate, 8, UsbSerialDriver.STOPBITS_1,
UsbSerialDriver.PARITY_NONE);
} catch (IOException e) {
Log.e("USB", "Error setting up device: " + e.getMessage(), e);
try {
sDriver.close();
} catch (IOException e2) {
// Ignore.
}
sDriver = null;
}
}
}
示例12: onResume
import com.hoho.android.usbserial.driver.UsbSerialProber; //导入依赖的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();
}
}
示例13: getConnections
import com.hoho.android.usbserial.driver.UsbSerialProber; //导入依赖的package包/类
@Override
public List<DeviceConnection> getConnections() {
// Get all the supported USB serial devices and return them.
ArrayList<DeviceConnection> connections = new ArrayList<DeviceConnection>();
for (UsbDevice device : UsbSerialProber.findSupportedDevices(manager)) {
connections.add(new Connection(context, manager, device));
}
return connections;
}
示例14: open
import com.hoho.android.usbserial.driver.UsbSerialProber; //导入依赖的package包/类
@Override
public void open() throws IOException {
// Get the UsbSerialDriver instance for this USB device.
List<UsbSerialDriver> drivers = UsbSerialProber.probeSingleDevice(manager, device);
if (drivers.size() < 1) throw new IOException("Could not create UsbSerialDriver for device.");
driver = drivers.get(0);
// Open the connection using 9600 baud.
driver.setParameters(9600, 8, 1, 0);
driver.open();
// Initialize the reader and writer for reading and writing over USB.
input = new BufferedReader(new InputStreamReader(new UsbSerialInputStream(driver)));
output = new BufferedWriter(new OutputStreamWriter(new UsbSerialOutputStream(driver)));
}
示例15: findDevice
import com.hoho.android.usbserial.driver.UsbSerialProber; //导入依赖的package包/类
/**
* Finds the Arduino USB device and asks the user for permission to use it.
*/
private boolean findDevice() {
UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(usbManager);
// Find the Arduino device.
for (UsbSerialDriver driver : availableDrivers) {
UsbDevice device = driver.getDevice();
int vendorId = device.getVendorId();
int productId = device.getProductId();
if ((vendorId == RCCarReceiverService.ARDUINO_USB_VENDOR_ID &&
productId == RCCarReceiverService.ARDUINO_UNO_USB_PRODUCT_ID) ||
(vendorId == RCCarReceiverService.ARDUINO_CLONE_VENDOR_ID &&
productId == RCCarReceiverService.ARDUINO_CLONE_PRODUCT_ID)) {
Log.d(TAG, "Found Arduino device!");
mUsbSerialDriver = driver;
// Request user's permission to access the device.
// Processing continues in mBroadcastReceiver after user grants permission.
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(RCCarReceiverService.ACTION_REQUEST_USB_PERMISSION), 0);
usbManager.requestPermission(device, pendingIntent);
return true;
}
}
Log.e(TAG, "No Arduino devices found");
Toast.makeText(this, "No Arduino devices found", Toast.LENGTH_LONG).show();
return false;
}