parent
7747d0f876
commit
17b8ba745b
@ -0,0 +1,163 @@
|
||||
package hendrikschutter.com.externgnss;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
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.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.hoho.android.usbserial.util.SerialInputOutputManager;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class SerialUSB implements SerialInputOutputManager.Listener{
|
||||
|
||||
private MainActivity mainActivity = null;
|
||||
|
||||
private enum UsbPermission { Unknown, Requested, Granted, Denied }
|
||||
|
||||
private static final String INTENT_ACTION_GRANT_USB = BuildConfig.APPLICATION_ID + ".GRANT_USB";
|
||||
private static final int WRITE_WAIT_MILLIS = 2000;
|
||||
private static final int READ_WAIT_MILLIS = 2000;
|
||||
|
||||
private int deviceId, portNum, baudRate;
|
||||
private boolean withIoManager;
|
||||
|
||||
private BroadcastReceiver broadcastReceiver = null;
|
||||
//private final Handler mainLooper;
|
||||
//private TextView receiveText;
|
||||
//private ControlLines controlLines;
|
||||
|
||||
private SerialInputOutputManager usbIoManager;
|
||||
private UsbSerialPort usbSerialPort;
|
||||
private UsbPermission usbPermission = UsbPermission.Unknown;
|
||||
private boolean connected = false;
|
||||
|
||||
public SerialUSB(MainActivity mainActivity){
|
||||
this.mainActivity = mainActivity;
|
||||
|
||||
// Find all available drivers from attached devices.
|
||||
UsbManager usbManager = (UsbManager) this.mainActivity.getSystemService(Context.USB_SERVICE);
|
||||
UsbSerialProber usbDefaultProber = UsbSerialProber.getDefaultProber();
|
||||
|
||||
if (usbManager.getDeviceList().values().size() > 0){
|
||||
UsbDevice device = (UsbDevice) usbManager.getDeviceList().values().toArray()[0];
|
||||
UsbSerialDriver driver = usbDefaultProber.probeDevice(device);
|
||||
if(driver == null) {
|
||||
Log.i("SerialUSB", "driver is null");
|
||||
}
|
||||
if(driver != null) {
|
||||
Log.i("SerialUSB", "ExternGNSS: device/driver found!");
|
||||
this.deviceId = device.getDeviceId();
|
||||
this.portNum = 0;
|
||||
this.baudRate = 9600;
|
||||
this.withIoManager = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
broadcastReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if(INTENT_ACTION_GRANT_USB.equals(intent.getAction())) {
|
||||
usbPermission = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false) ? UsbPermission.Granted : UsbPermission.Denied;
|
||||
connect();
|
||||
} else {
|
||||
Log.i("SerialUSB", "USB permissions are not set");
|
||||
}
|
||||
}
|
||||
};
|
||||
connect();
|
||||
this.mainActivity.registerReceiver(broadcastReceiver, new IntentFilter(INTENT_ACTION_GRANT_USB));
|
||||
}
|
||||
|
||||
private void connect() {
|
||||
Log.i("connect", "starting to connect ...");
|
||||
UsbDevice device = null;
|
||||
UsbManager usbManager = (UsbManager) this.mainActivity.getSystemService(Context.USB_SERVICE);
|
||||
for(UsbDevice v : usbManager.getDeviceList().values())
|
||||
if(v.getDeviceId() == deviceId)
|
||||
device = v;
|
||||
if(device == null) {
|
||||
Log.i("connect", "connection failed: device not found");
|
||||
return;
|
||||
}
|
||||
UsbSerialDriver driver = UsbSerialProber.getDefaultProber().probeDevice(device);
|
||||
/*
|
||||
if(driver == null) {
|
||||
driver = CustomProber.getCustomProber().probeDevice(device);
|
||||
}
|
||||
*/
|
||||
if(driver == null) {
|
||||
Log.i("connect", "connection failed: no driver for device");
|
||||
return;
|
||||
}
|
||||
if(driver.getPorts().size() < portNum) {
|
||||
Log.i("connect", "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(this.mainActivity, 0, new Intent(INTENT_ACTION_GRANT_USB), 0);
|
||||
usbManager.requestPermission(driver.getDevice(), usbPermissionIntent);
|
||||
return;
|
||||
}
|
||||
if(usbConnection == null) {
|
||||
if (!usbManager.hasPermission(driver.getDevice()))
|
||||
Log.i("connect", "connection failed: permission denied");
|
||||
else
|
||||
Log.i("connect", "connection failed: open failed");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
usbSerialPort.open(usbConnection);
|
||||
usbSerialPort.setParameters(baudRate, 8, 1, UsbSerialPort.PARITY_NONE);
|
||||
if(withIoManager) {
|
||||
usbIoManager = new SerialInputOutputManager(usbSerialPort, this);
|
||||
usbIoManager.start();
|
||||
}
|
||||
|
||||
Log.i("connect", "connected");
|
||||
connected = true;
|
||||
//controlLines.start();
|
||||
} catch (Exception e) {
|
||||
Log.i("connect", "connection failed: " + e.getMessage());
|
||||
disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
private void disconnect() {
|
||||
connected = false;
|
||||
//controlLines.stop();
|
||||
if(usbIoManager != null) {
|
||||
usbIoManager.setListener(null);
|
||||
usbIoManager.stop();
|
||||
}
|
||||
usbIoManager = null;
|
||||
try {
|
||||
usbSerialPort.close();
|
||||
} catch (IOException ignored) {}
|
||||
usbSerialPort = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNewData(byte[] data) {
|
||||
Log.i("connect", "received data: " + data.length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRunError(Exception e) {
|
||||
Log.e("SerialUSB", "runtime error: " + e.getMessage());
|
||||
this.disconnect();
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<usb-device vendor-id="1027" product-id="24577"/> <!-- 0x6001: FT232R -->
|
||||
</resources>
|
Loading…
Reference in new issue