Skip to content

Most visited

Recently visited

navigation

I2cDevice

I2cDevice

public abstract class I2cDevice
extends Object implements Closeable

java.lang.Object
   ↳ com.google.android.things.pio.I2cDevice


Controls an I2C device. I2C is a bus protocol allowing a master (running Android headless) to control multiple slave device connected to this bus. Each device must have a unique address to disambiguate the commands sent. Some devices use the notion of register (also called commands), allowing you to select a memory address to read from/write to. The datasheet for your device will explain what each register is used for. Opening an I2C device takes ownership of it for the whole system, preventing anyone else from opening/accessing this device until you call close(). Forgetting to call close() will prevent anyone (including the same process/app) from using the device.

Summary

Public constructors

I2cDevice()

Public methods

abstract void close()

Close and release the device.

String getName()

Returns the I/O name.

abstract void read(byte[] buffer, int length)

Read data from the device.

abstract void readRegBuffer(int reg, byte[] buffer, int length)

Read multiple bytes from a given register.

abstract byte readRegByte(int reg)

Read a byte from a given register.

abstract short readRegWord(int reg)

Read a 2-byte word from a given register.

abstract void write(byte[] buffer, int length)

Write data to the device.

abstract void writeRegBuffer(int reg, byte[] buffer, int length)

Write a byte array to a given register.

abstract void writeRegByte(int reg, byte data)

Write a byte to a given register.

abstract void writeRegWord(int reg, short data)

Write a 2-byte word to a given register.

Inherited methods

From class java.lang.Object
From interface java.io.Closeable
From interface java.lang.AutoCloseable

Public constructors

I2cDevice

I2cDevice ()

Public methods

close

void close ()

Close and release the device. Must be called in order to make the device available to other users.

Throws
IOException if the I2cDevice is already closed.

getName

String getName ()

Returns the I/O name.

Returns
String

read

void read (byte[] buffer, 
                int length)

Read data from the device.

Parameters
buffer byte: Buffer to read data into.
length int: Number of bytes to read, may not be larger than the buffer size.
Throws
IOException
IllegalArgumentException
IOException

See also:

readRegBuffer

void readRegBuffer (int reg, 
                byte[] buffer, 
                int length)

Read multiple bytes from a given register.

Parameters
reg int: The register to read from (0x00-0xFF).
buffer byte: Buffer to read data into.
length int: Number of bytes to read, may not be larger than the buffer size.
Throws
IOException
IllegalArgumentException
IOException

See also:

readRegByte

byte readRegByte (int reg)

Read a byte from a given register.

Parameters
reg int: The register to read from (0x00-0xFF).
Returns
byte The value read from the device.
Throws
IOException
IllegalArgumentException
IOException

See also:

readRegWord

short readRegWord (int reg)

Read a 2-byte word from a given register.

Parameters
reg int: The register to read from (0x00-0xFF).
Returns
short The 2-byte value read from the device.
Throws
IOException
IllegalArgumentException
IOException

See also:

write

void write (byte[] buffer, 
                int length)

Write data to the device.

Parameters
buffer byte: Data to write.
length int: Number of bytes to write, may not be larger than the buffer size.
Throws
IOException
IllegalArgumentException
IOException

See also:

writeRegBuffer

void writeRegBuffer (int reg, 
                byte[] buffer, 
                int length)

Write a byte array to a given register.

Parameters
reg int: The register to write to (0x00-0xFF).
buffer byte: Data to write.
length int: Number of bytes to write, may not be larger than the buffer size.
Throws
IOException
IllegalArgumentException
IOException

See also:

writeRegByte

void writeRegByte (int reg, 
                byte data)

Write a byte to a given register.

Parameters
reg int: The register to write to (0x00-0xFF).
data byte: Value to write.
Throws
IOException
IllegalArgumentException
IOException

See also:

writeRegWord

void writeRegWord (int reg, 
                short data)

Write a 2-byte word to a given register.

Parameters
reg int: The register to write to (0x00-0xFF).
data short: Value to write.
Throws
IOException
IllegalArgumentException
IOException

See also:

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)