我无法在 Android 中通过 I2C 总线获取数据

I can't get data by I2C bus in Android Things

我无法通过 I2C 总线获取数据 Android 事情 Raspberry Pi 3.

我连接 Android RPi 和 DS18B20(温度传感器)上的东西。 Connect to RPi

和运行 I2C地址扫描应用程序(https://github.com/dennisg/i2c-address-scanner),但找不到可用地址。

    for (int address = 0; address < 256; address++) {

        //auto-close the devices
        try (final I2cDevice device = peripheralManagerService.openI2cDevice(BoardDefaults.getI2cBus(), address)) {

            try {
                device.readRegByte(TEST_REGISTER);
                Log.i(TAG, String.format(Locale.US, "Trying: 0x%02X - SUCCESS", address));
            } catch (final IOException e) {
                Log.i(TAG, String.format(Locale.US, "Trying: 0x%02X - FAIL", address));
            }

        } catch (final IOException e) {
            //in case the openI2cDevice(name, address) fails
        }
    }

如何通过 I2C 获取数据?

链接的测试项目似乎在几个方面使用 the I2C protocol 很糟糕。

首先,它假设特定板上只有一条 I2C 总线。虽然现在确实如此,但它可能无法扩展到支持未来 Android 事物的其他 SOM。

其次,它假定要读取的寄存器地址 (0x00)。这可能适用于开发人员开始使用的任何设备,但大量 I2C 外围设备可能不会响应该地址。

您应该查看此设备的 the datasheet。粗略查看了一下,好像没有0x00对应的寄存器。此外,它有一个自定义的读取流程,这使得上面的代码片段难以使用。在您的传感器上获取这些值的微控制器可能会将它们丢弃并且 returns 没有信号。

再次阅读数据表可能会有用。传感器似乎是 "one-wire" 而不是 I2C。虽然这两个协议可能相似,但使用内置的 readByte 方法可能会在数据传输中做出一些可能与外围协议不完全匹配的假设。

作为 Nick Felker wrote in his DS18B20 has 1-Wire interface and you can't connect it to Raspberry Pi with Android Things directly. You should use industrial (e.g DS2482-100) or custom MCU-based (like in that 项目)1-Wire <-> I2C 转换器,或其他(例如 USB <-> 1-Wire、UART<-> 1-Wire)转换器。