# UartDeviceDriver

Summary:[Inherited Constants](https://developer.android.com/reference/com/google/android/things/userdriver/pio/UartDeviceDriver#inhconstants)\|[Methods](https://developer.android.com/reference/com/google/android/things/userdriver/pio/UartDeviceDriver#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/com/google/android/things/userdriver/pio/UartDeviceDriver#inhmethods)\|[\[Expand All\]](https://developer.android.com/reference/com/google/android/things/userdriver/pio/UartDeviceDriver#)  

# UartDeviceDriver

`
public


interface
UartDeviceDriver
`  
`


implements

`[UartDevice](https://developer.android.com/reference/com/google/android/things/pio/UartDevice)`


`

|-----------------------------------------------------------|
| com.google.android.things.userdriver.pio.UartDeviceDriver |

<br />

*** ** * ** ***

UART user driver. A UartDeviceDriver must implement all UART functions, but additionally this class provides an open() function that will be called whenever a process obtains ownership of the UART, to match the existing close() function.

## Summary

|                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           ### Inherited constants                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/com/google/android/things/userdriver/pio/UartDeviceDriver#)From interface` `[com.google.android.things.pio.UartDevice](https://developer.android.com/reference/com/google/android/things/pio/UartDevice)` ` |-------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `int` | [FLUSH_IN](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#FLUSH_IN) [Queue](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#flush(int))constant used to flush UART data that has been received but not read.                                                                          | | `int` | [FLUSH_IN_OUT](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#FLUSH_IN_OUT) [Queue](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#flush(int))constant used to flush UART data that has both been received and written.                                                              | | `int` | [FLUSH_OUT](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#FLUSH_OUT) [Queue](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#flush(int))constant used to flush UART data that has been written but not transmitted.                                                                  | | `int` | [HW_FLOW_CONTROL_AUTO_RTSCTS](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#HW_FLOW_CONTROL_AUTO_RTSCTS) [Mode](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#setHardwareFlowControl(int))constant used to configure a UART device to use automatic RTS/CTS hardware flow control. | | `int` | [HW_FLOW_CONTROL_NONE](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#HW_FLOW_CONTROL_NONE) [Mode](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#setHardwareFlowControl(int))constant used to configure a UART device to use no hardware flow control.                              | | `int` | [MODEM_CONTROL_CD](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#MODEM_CONTROL_CD) [Bit](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#setModemControl(int))constant used to configure a UART device data carrier detect.                                                          | | `int` | [MODEM_CONTROL_CTS](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#MODEM_CONTROL_CTS) [Bit](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#setModemControl(int))constant used to configure a UART device clear to send.                                                              | | `int` | [MODEM_CONTROL_DSR](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#MODEM_CONTROL_DSR) [Bit](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#setModemControl(int))constant used to configure a UART device data set ready.                                                             | | `int` | [MODEM_CONTROL_DTR](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#MODEM_CONTROL_DTR) [Bit](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#setModemControl(int))constant used to configure a UART device data terminal ready.                                                        | | `int` | [MODEM_CONTROL_LE](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#MODEM_CONTROL_LE) [Bit](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#setModemControl(int))constant used to configure a UART device data set ready/Line enable.                                                   | | `int` | [MODEM_CONTROL_RI](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#MODEM_CONTROL_RI) [Bit](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#setModemControl(int))constant used to configure a UART device ring.                                                                         | | `int` | [MODEM_CONTROL_RTS](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#MODEM_CONTROL_RTS) [Bit](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#setModemControl(int))constant used to configure a UART device request to send.                                                            | | `int` | [MODEM_CONTROL_SR](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#MODEM_CONTROL_SR) [Bit](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#setModemControl(int))constant used to configure a UART device secondary RXD.                                                                | | `int` | [MODEM_CONTROL_ST](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#MODEM_CONTROL_ST) [Bit](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#setModemControl(int))constant used to configure a UART device secondary TXD.                                                                | | `int` | [PARITY_EVEN](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#PARITY_EVEN) [Mode](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#setParity(int))constant used to configure a UART device to use even parity.                                                                          | | `int` | [PARITY_MARK](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#PARITY_MARK) [Mode](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#setParity(int))constant used to configure a UART device to use mark parity.                                                                          | | `int` | [PARITY_NONE](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#PARITY_NONE) [Mode](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#setParity(int))constant used to configure a UART device to use no parity.                                                                            | | `int` | [PARITY_ODD](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#PARITY_ODD) [Mode](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#setParity(int))constant used to configure a UART device to use odd parity.                                                                             | | `int` | [PARITY_SPACE](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#PARITY_SPACE) [Mode](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#setParity(int))constant used to configure a UART device to use space parity.                                                                       | |

|                                                                                                                                                                                                                                                                                 ### Public methods                                                                                                                                                                                                                                                                                  ||
|-------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` abstract void`                                                              | ` `[close](https://developer.android.com/reference/com/google/android/things/userdriver/pio/UartDeviceDriver#close())`() ` Closes the UART.                                                                                                                                                                                                                                                                                                                                                          |
| ` default `[String](https://developer.android.com/reference/java/lang/String) | ` `[getName](https://developer.android.com/reference/com/google/android/things/userdriver/pio/UartDeviceDriver#getName())`() ` Returns null.                                                                                                                                                                                                                                                                                                                                                         |
| ` abstract void`                                                              | ` `[open](https://developer.android.com/reference/com/google/android/things/userdriver/pio/UartDeviceDriver#open())`() ` Opens the UART.                                                                                                                                                                                                                                                                                                                                                             |
| ` abstract void`                                                              | ` `[registerUartDeviceCallback](https://developer.android.com/reference/com/google/android/things/userdriver/pio/UartDeviceDriver#registerUartDeviceCallback(android.os.Handler, com.google.android.things.pio.UartDeviceCallback))`(`[Handler](https://developer.android.com/reference/android/os/Handler.html)` handler, `[UartDeviceCallback](https://developer.android.com/reference/com/google/android/things/pio/UartDeviceCallback)` callback) ` Registers an interrupt callback on the UART. |

| ### Inherited methods |
|-----------------------|---|
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/com/google/android/things/userdriver/pio/UartDeviceDriver#)From interface` `[com.google.android.things.pio.UartDevice](https://developer.android.com/reference/com/google/android/things/pio/UartDevice)` ` |-------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract void`                                                              | ` `[clearModemControl](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#clearModemControl(int))`(int lines) ` Clear selected modem control lines.                                                                                                                                                                                                                                                                                           | | ` abstract void`                                                              | ` `[close](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#close())`() ` Close and release the device.                                                                                                                                                                                                                                                                                                                                     | | ` abstract void`                                                              | ` `[flush](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#flush(int))`(int direction) ` Flush the specified queue for the device.                                                                                                                                                                                                                                                                                                         | | ` default `[String](https://developer.android.com/reference/java/lang/String) | ` `[getName](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#getName())`() ` Returns the I/O name.                                                                                                                                                                                                                                                                                                                                         | | ` abstract int`                                                               | ` `[read](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#read(byte[], int))`(byte[] buffer, int length) ` Read data from the device.                                                                                                                                                                                                                                                                                                      | | ` default void`                                                               | ` `[registerUartDeviceCallback](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#registerUartDeviceCallback(com.google.android.things.pio.UartDeviceCallback))`(`[UartDeviceCallback](https://developer.android.com/reference/com/google/android/things/pio/UartDeviceCallback)` callback) ` Registers a callback for interrupts.                                                                                                           | | ` abstract void`                                                              | ` `[registerUartDeviceCallback](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#registerUartDeviceCallback(android.os.Handler, com.google.android.things.pio.UartDeviceCallback))`(`[Handler](https://developer.android.com/reference/android/os/Handler.html)` handler, `[UartDeviceCallback](https://developer.android.com/reference/com/google/android/things/pio/UartDeviceCallback)` callback) ` Registers a callback for interrupts. | | ` abstract void`                                                              | ` `[sendBreak](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#sendBreak(int))`(int duration_msecs) ` Send a break to the device.                                                                                                                                                                                                                                                                                                          | | ` abstract void`                                                              | ` `[setBaudrate](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#setBaudrate(int))`(int rate) ` Set the device baud rate.                                                                                                                                                                                                                                                                                                                  | | ` abstract void`                                                              | ` `[setDataSize](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#setDataSize(int))`(int size) ` Set the device data size.                                                                                                                                                                                                                                                                                                                  | | ` abstract void`                                                              | ` `[setHardwareFlowControl](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#setHardwareFlowControl(int))`(int mode) ` Set the hardware flow control mode.                                                                                                                                                                                                                                                                                  | | ` abstract void`                                                              | ` `[setModemControl](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#setModemControl(int))`(int lines) ` Set selected modem control lines.                                                                                                                                                                                                                                                                                                 | | ` abstract void`                                                              | ` `[setParity](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#setParity(int))`(int mode) ` Set the device parity mode.                                                                                                                                                                                                                                                                                                                    | | ` abstract void`                                                              | ` `[setStopBits](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#setStopBits(int))`(int bits) ` Set the device stop bits.                                                                                                                                                                                                                                                                                                                  | | ` abstract void`                                                              | ` `[unregisterUartDeviceCallback](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#unregisterUartDeviceCallback(com.google.android.things.pio.UartDeviceCallback))`(`[UartDeviceCallback](https://developer.android.com/reference/com/google/android/things/pio/UartDeviceCallback)` callback) ` Unregisters a callback.                                                                                                                    | | ` abstract int`                                                               | ` `[write](https://developer.android.com/reference/com/google/android/things/pio/UartDevice#write(byte[], int))`(byte[] buffer, int length) ` Write data to the device.                                                                                                                                                                                                                                                                                                     | ||
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/com/google/android/things/userdriver/pio/UartDeviceDriver#)From interface` `[java.io.Closeable](https://developer.android.com/reference/java/io/Closeable)` ` |------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract void` | ` `[close](https://developer.android.com/reference/java/io/Closeable#close())`() ` Closes this stream and releases any system resources associated with it. | ||
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/com/google/android/things/userdriver/pio/UartDeviceDriver#)From interface` `[java.lang.AutoCloseable](https://developer.android.com/reference/java/lang/AutoCloseable)` ` |------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract void` | ` `[close](https://developer.android.com/reference/java/lang/AutoCloseable#close())`() ` Closes this resource, relinquishing any underlying resources. | ||

## Public methods

### close

```
void close ()
```

Closes the UART. This will be called when the UART is closed by its owner. Any resources that need to be cleaned up should be done so here. This method may throw IOException to indicate I/O failure, but will still be considered closed and may attempt to be re-opened in the future.

<br />

|                                                                 Throws                                                                  ||
|----------------------------------------------------------------------------|-------------------------------------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException) | on I/O failure; the driver will still be considered closed. |

### getName

```
String getName ()
```

Returns null. I/O user drivers can be registered under any name so this returns null by default. Child classes may override this if other behavior is desired.

<br />

|                                  Returns                                   ||
|--------------------------------------------------------------------|--------|
| [String](https://developer.android.com/reference/java/lang/String) | <br /> |

### open

```
void open ()
```

Opens the UART. This will be called when ownership of a UART is obtained. The driver should do any initialization needed to configure the UART for use here.

<br />

|                                                           Throws                                                           ||
|----------------------------------------------------------------------------|------------------------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException) | on I/O failure; the driver will remain closed. |

### registerUartDeviceCallback

```
void registerUartDeviceCallback (Handler handler, 
                UartDeviceCallback callback)
```

Registers an interrupt callback on the UART. See[registerGpioCallback(Handler, GpioCallback)](https://developer.android.com/reference/com/google/android/things/userdriver/pio/GpioDriver#registerGpioCallback(android.os.Handler, com.google.android.things.pio.GpioCallback))for information on how callbacks work for user drivers. A typical use-case for UART user driver interrupts is an external UART chip which uses a GPIO line to indicate when data is ready to read. In this case, the driver code might look something like this:  

     public class UartWithInterruptDriver implements UartDeviceDriver {
         // The data-ready signal GPIO.
         private Gpio mInterruptGpio;

         // Our GPIO interrupt handler.
         private InterruptCallback mInterruptCallback;

         // Implement other UartDeviceDriver functionality.
         // ...

         @Override
         public void registerUartDeviceCallback(Handler handler, UartDeviceCallback callback)
                 throws IOException {
             // Configure mInterruptGpio to begin listening for interrupts.
             mInterruptCallback = new InterruptCallback(callback);
             mInterruptGpio.setEdgeTriggerType(Gpio.EDGE_RISING);
             mInterruptGpio.registerGpioCallback(handler, mInterruptCallback);
         }

         @Override
         public void unregisterUartDeviceCallback(UartDeviceCallback callback) {
             mInterruptGpio.unregisterGpioCallback(mInterruptCallback);
             mInterruptGpio.setEdgeTriggerType(Gpio.EDGE_NONE);
             mInterruptCallback = null;
         }

         // Bounces the underlying GPIO callbacks into the registered UART callback.
         private class InterruptCallback implements GpioCallback {
             private final UartDeviceCallback mUartCallback;

             public InterruptCallback(UartDeviceCallback uartCallback) {
                 mUartCallback = uartCallback;
             }

             @Override
             public boolean onGpioEdge(Gpio gpio) {
                 return mUartCallback.onUartDeviceDataAvailable(null);
             }

             @Override
             public void onGpioError(Gpio gpio, int error) {
                 // The underlying interrupt GPIO failed, we won't get any more callbacks.
                 mUartCallback.onUartDeviceError(null, error);
             }
         }
     }
     
<br />

<br />

|                                                                           Parameters                                                                           ||
|------------|----------------------------------------------------------------------------------------------------------------------------------------------------|
| `handler`  | `Handler`: Handler to optionally run callbacks on. Provided as a convenience if needed, but it's not required to execute callbacks on this. <br /> |
| `callback` | `UartDeviceCallback`: UartDeviceCallback to register; only one will ever be registered at a time. <br />                                           |

|                                                        Throws                                                        ||
|----------------------------------------------------------------------------|------------------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException) | if this UART doesn't support interrupts. |

-

  ## Interfaces

  - [GpioDriver](https://developer.android.com/reference/com/google/android/things/userdriver/pio/GpioDriver)
  - [I2cBusDriver](https://developer.android.com/reference/com/google/android/things/userdriver/pio/I2cBusDriver)
  - [PwmDriver](https://developer.android.com/reference/com/google/android/things/userdriver/pio/PwmDriver)
  - [SpiBusDriver](https://developer.android.com/reference/com/google/android/things/userdriver/pio/SpiBusDriver)
  - [UartDeviceDriver](https://developer.android.com/reference/com/google/android/things/userdriver/pio/UartDeviceDriver)
-

  ## Classes

  - [PioDriverManager](https://developer.android.com/reference/com/google/android/things/userdriver/pio/PioDriverManager)