Skip to content

WIP Usb serial: using usb-serial-for-android #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ClientLib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ dependencies {
//MP4 generation library
api 'com.googlecode.mp4parser:isoparser:1.1.7'

api 'org.droidplanner.android:usb-serial-android:0.1.0'
implementation 'com.github.mik3y:usb-serial-for-android:3.5.1'

debugApi project(':Mavlink')
// sitlCompile project(':Mavlink')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.util.Log;

import com.hoho.android.usbserial.driver.UsbSerialDriver;
import com.hoho.android.usbserial.driver.UsbSerialPort;
import com.hoho.android.usbserial.driver.UsbSerialProber;

import com.o3dr.services.android.lib.gcs.link.LinkConnectionStatus;
Expand Down Expand Up @@ -113,14 +115,14 @@ protected void openUsbConnection(Bundle extras) throws IOException {
UsbManager manager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);

//Get the list of available devices
List<UsbDevice> availableDevices = UsbSerialProber.getAvailableSupportedDevices(manager);
List<UsbSerialDriver> availableDevices = UsbSerialProber.getDefaultProber().findAllDrivers(manager);
if (availableDevices.isEmpty()) {
Log.d(TAG, "No Devices found");
throw new IOException("No Devices found");
}

//Pick the first device
UsbDevice device = availableDevices.get(0);
UsbDevice device = availableDevices.get(0).getDevice();
if (manager.hasPermission(device)) {
openUsbDevice(device, extras);
} else {
Expand All @@ -138,27 +140,43 @@ private void openUsbDevice(UsbDevice device, Bundle extras) throws IOException {
UsbManager manager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);

// Find the first available driver.
final UsbSerialDriver serialDriver = UsbSerialProber.openUsbDevice(manager, device);

if (serialDriver == null) {
final UsbSerialDriver serialDriver = UsbSerialProber.getDefaultProber().probeDevice(device);
if(serialDriver == null) {
Log.d(TAG, "No Devices found");
throw new IOException("No Devices found");
} else {
Log.d(TAG, "Opening using Baud rate " + mBaudRate);
try {
serialDriver.open();
serialDriver.setParameters(mBaudRate, 8, UsbSerialDriver.STOPBITS_1, UsbSerialDriver.PARITY_NONE);
throw new IOException("Serial driver is null");
}
if(serialDriver.getPorts().size() < 0) {
throw new IOException("Serial port num doesn't exists");
}

serialDriverRef.set(serialDriver);
Log.d(TAG, "port count " + serialDriver.getPorts().size());
final UsbSerialPort usbSerialPort = serialDriver.getPorts().get(0);
UsbDeviceConnection usbConnection = manager.openDevice(serialDriver.getDevice());

onUsbConnectionOpened(extras);
} catch (IOException e) {
Log.e(TAG, "Error setting up device: " + e.getMessage(), e);
try {
serialDriver.close();
} catch (IOException e2) {
// Ignore.
}
if(usbConnection == null) {
if (!manager.hasPermission(serialDriver.getDevice()))
throw new IOException("connection failed: permission denied");
else
throw new IOException("connection failed: open failed");
}

try {
Log.d(TAG, "Opening using Baud rate " + mBaudRate);
usbSerialPort.open(usbConnection);
usbSerialPort.setParameters(mBaudRate, 8, 1, UsbSerialPort.PARITY_NONE);
// if(withIoManager) {
// usbIoManager = new SerialInputOutputManager(usbSerialPort, this);
// usbIoManager.start();
// }
serialDriverRef.set(serialDriver);

onUsbConnectionOpened(extras);
} catch (Exception e) {
Log.e(TAG, "Error setting up device: " + e.getMessage(), e);
try {
usbSerialPort.close();
} catch (IOException e2) {
// Ignore.
}
}
}
Expand All @@ -173,7 +191,7 @@ protected int readDataBlock(byte[] readData) throws IOException {

int iavailable = 0;
try {
iavailable = serialDriver.read(readData, 200);
iavailable = serialDriver.getPorts().get(0).read(readData, 200);
} catch (NullPointerException e) {
final String errorMsg = "Error Reading: " + e.getMessage()
+ "\nAssuming inaccessible USB device. Closing connection.";
Expand All @@ -194,7 +212,7 @@ protected void sendBuffer(byte[] buffer) {
final UsbSerialDriver serialDriver = serialDriverRef.get();
if (serialDriver != null) {
try {
serialDriver.write(buffer, 500);
serialDriver.getPorts().get(0).write(buffer, 500);
} catch (IOException e) {
Log.e(TAG, "Error Sending: " + e.getMessage(), e);
}
Expand All @@ -208,7 +226,7 @@ protected void closeUsbConnection() throws IOException {
final UsbSerialDriver serialDriver = serialDriverRef.getAndSet(null);
if (serialDriver != null) {
try {
serialDriver.close();
serialDriver.getPorts().get(0).close();
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
}
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ allprojects {
google()
mavenLocal()
jcenter()
maven { url 'https://jitpack.io' }
// maven {
// url getMavenRepoUrl()
// credentials {
Expand Down