lightbulb_outline Please take our October 2018 developer survey. Start survey

I2cDevice

public interface I2cDevice
implements Closeable

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 methods

abstract void close()

Close and release the device.

default 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 interface java.io.Closeable
From interface java.lang.AutoCloseable

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

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

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

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

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

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

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

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