public class

UsbVehicleInterface

extends BytestreamDataSource
implements VehicleInterface
java.lang.Object
   ↳ com.openxc.sources.BaseVehicleDataSource
     ↳ com.openxc.sources.ContextualVehicleDataSource
       ↳ com.openxc.sources.BytestreamDataSource
         ↳ com.openxc.interfaces.usb.UsbVehicleInterface

Class Overview

A vehicle data source reading measurements from an OpenXC USB device. This class looks for a USB device and expects to read OpenXC-compatible, newline separated JSON messages in USB bulk transfer packets. The device used (if different from the default) can be specified by passing an custom URI to the constructor. The expected format of this URI is defined in UsbDeviceUtilities. According to Android's USB device usage requirements, this class requests permission for the USB device from the user before accessing it. This may cause a pop-up dialog that the user must dismiss before the data source will become active.

Summary

Constants
String ACTION_USB_DEVICE_ATTACHED
String ACTION_USB_PERMISSION
[Expand]
Inherited Constants
From class com.openxc.sources.BytestreamDataSource
[Expand]
Inherited Fields
From class com.openxc.sources.BytestreamDataSource
Public Constructors
UsbVehicleInterface(SourceCallback callback, Context context, URI deviceUri)
Construct an instance of UsbVehicleInterface with a receiver callback and custom device URI.
UsbVehicleInterface(SourceCallback callback, Context context)
Construct an instance of UsbVehicleInterface with a receiver callback and the default device URI.
UsbVehicleInterface(Context context)
UsbVehicleInterface(Context context, String uriString)
Public Methods
static URI createUri(String uriString)
boolean isConnected()
Return true if the data source is actively connected to its target, be it a USB endpoint, a Bluetooth channel, a trace file, etc.
boolean setResource(String otherUri)
Change the resource used by the instance to connect to the interface, restarting any neccessary services.
synchronized void start()
void stop()
Unregister USB device intent broadcast receivers and stop waiting for a connection.
String toString()
Protected Methods
void connect()
Initiate a connection to the vehicle interface.
void disconnect()
Perform any cleanup necessary to disconnect from the interface.
String getTag()
Return a string suitable as a tag for logging.
int read(byte[] bytes)
Read data from the source into the given array.
boolean write(byte[] bytes)
[Expand]
Inherited Methods
From class com.openxc.sources.BytestreamDataSource
From class com.openxc.sources.ContextualVehicleDataSource
From class com.openxc.sources.BaseVehicleDataSource
From class java.lang.Object
From interface com.openxc.DataPipeline.Operator
From interface com.openxc.interfaces.VehicleInterface
From interface com.openxc.sinks.VehicleDataSink
From interface com.openxc.sources.VehicleDataSource
From interface java.lang.Runnable

Constants

public static final String ACTION_USB_DEVICE_ATTACHED

Constant Value: "com.ford.openxc.USB_DEVICE_ATTACHED"

public static final String ACTION_USB_PERMISSION

Constant Value: "com.ford.openxc.USB_PERMISSION"

Public Constructors

public UsbVehicleInterface (SourceCallback callback, Context context, URI deviceUri)

Construct an instance of UsbVehicleInterface with a receiver callback and custom device URI. If the device cannot be found at initialization, the object will block waiting for a signal to check again.

Parameters
callback An object implementing the SourceCallback that should receive data as it is received and parsed.
context The Activity or Service context, used to get access to the Android UsbManager.
deviceUri a USB device URI (see UsbDeviceUtilities for the format) to look for.
Throws
DataSourceException If the URI doesn't have the correct format

public UsbVehicleInterface (SourceCallback callback, Context context)

Construct an instance of UsbVehicleInterface with a receiver callback and the default device URI. The default device URI is specified in UsbDeviceUtilities.

Parameters
callback An object implementing the SourceCallback that should receive data as it is received and parsed.
context The Activity or Service context, used to get access to the Android UsbManager.
Throws
DataSourceException in exceptional circumstances, i.e. only if the default device URI is malformed.

public UsbVehicleInterface (Context context)

public UsbVehicleInterface (Context context, String uriString)

Public Methods

public static URI createUri (String uriString)

public boolean isConnected ()

Return true if the data source is actively connected to its target, be it a USB endpoint, a Bluetooth channel, a trace file, etc. The source is capable of providing new vehicle data. Returns true if connected, false otherwise.

public boolean setResource (String otherUri)

Change the resource used by the instance to connect to the interface, restarting any neccessary services.

Parameters
otherUri The new resource to use for the interface.
Returns
  • true if the resource was different and the interface was restarted.

public synchronized void start ()

public void stop ()

Unregister USB device intent broadcast receivers and stop waiting for a connection. This should be called before the object is given up to the garbage collector to avoid leaking a receiver in the Android framework.

public String toString ()

Protected Methods

protected void connect ()

Initiate a connection to the vehicle interface.

protected void disconnect ()

Perform any cleanup necessary to disconnect from the interface.

protected String getTag ()

Return a string suitable as a tag for logging.

protected int read (byte[] bytes)

Read data from the source into the given array. No more than bytes.length bytes will be read, and there is no guarantee that any bytes will be read at all.

Parameters
bytes the destination array for bytes from the data source.
Returns
  • the number of bytes that were actually copied into bytes.
Throws
IOException

protected boolean write (byte[] bytes)