javaandroidkotliniotandroid-things

Android things: using commands writeBuffer, write, byte[] to write many bytes in i2c registers


I'm developing an application to move dc motors, using android things.

At this stage I already know which commands I have to process, but I do not know how. (Using the i2c smartdrive sensor with a Pio Cli commands to run motors)

Below example i2c from guide google developers:

public void writeBuffer (I2cDevice device, byte [] buffer) throws IOException {
     int count = device.write (buffer, buffer.length);
     Log.d (TAG, "Wrote" + count + "bytes over I2C.");
}

My question (based on the code above) is:

1) How to write (in Kotlin) pio i2c I2C1 0x1B write-raw 0x46 128 0x05 0x00 0xD1 in buffer to pass as parameter in writeBuffer function?

e.g.: var buffer = (new byte[] (byte) 0x46, 128, 0x05, 0x00, 0xD1, value.i2cValue.toByteArray()), did not work.

My final project is based on the project below: https://github.com/Nilhcem/i2cfun-androidthings/tree/arduino_slave/app/src/main/java/com/nilhcem/androidthings/i2cfun

quote: I know that I2C1 and 0x1B are not required because they are passed parameters, either before, or in I2cDevice.


Solution

  • I solved: val buffer = byteArrayOf(0x46, 128.toByte(), 0x05, 0x00, 0xD1.toByte()) and device?.write(buffer, buffer.size)