packagecom.gclue.myapplication;importandroid.os.Handler;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.util.Log;importcom.google.android.things.pio.I2cDevice;importcom.google.android.things.pio.PeripheralManagerService;importjava.io.IOException;importandroid.widget.TextView;publicclassMainActivityextendsAppCompatActivity{// I2C Device NameprivatestaticfinalStringI2C_DEVICE_NAME="I2C1";// I2C Slave AddressprivatestaticfinalintI2C_ADDRESS=0x53;privatestaticfinalStringTAG="THINGS";privateHandlermHandler=newHandler();privatestaticfinalintINTERVAL_BETWEEN_AXIS_MS=100;privateI2cDevicemDevice;/** Who_am_i register */privatebyteADXL345_DEVID_REG=0x00;/** Data Format Control */privatebyteADXL345_DATA_FORMAT_REG=0x31;/** Power-saving features control */privatebyteADXL345_POWER_CTL_REG=0x2D;/** Power-saving features control */privatebyteADXL345_3AIXS=0x32;/** SELF Test ON */privatebyteADXL345_SELF_TEST_ON=(byte)0b10000000;/** SELF Test OFF */privatebyteADXL345_SELF_TEST_OFF=0b00000000;/** SELF SPI ON */privatebyteADXL345_SPI_ON=0b01000000;/** SELF SPI OFF */privatebyteADXL345_SPI_OFF=0b00000000;/** INT_INVERT ON */privatebyteADXL345_INT_INVERT_ON=0b00100000;/** INT_INVERT OFF */privatebyteADXL345_INT_INVERT_OFF=0b00000000;/** FULL_RES ON */privatebyteADXL345_FULL_RES_ON=0b00001000;/** FULL_RES OFF */privatebyteADXL345_FULL_RES_OFF=0b00000000;/** JUSTIFY ON */privatebyteADXL345_JUSTIFY_ON=0b00000100;/** JUSTIFY OFF */privatebyteADXL345_JUSTIFY_OFF=0b00000000;/** RANGE 2G */privatebyteADXL345_RANGE_2G=0b00;/** RANGE 4G */privatebyteADXL345_RANGE_4G=0b01;/** RANGE 8G */privatebyteADXL345_RANGE_8G=0b10;/** RANGE 16G */privatebyteADXL345_RANGE_16G=0b11;/** AUTO SLEEP ON */privatebyteADXL345_AUTO_SLEEP_ON=0b00010000;/** AUTO SLEEP OFF */privatebyteADXL345_AUTO_SLEEP_OFF=0b00000000;/** AUTO MEASURE ON */privatebyteADXL345_MEASURE_ON=0b00001000;/** AUTO MEASURE OFF */privatebyteADXL345_MEASURE_OFF=0b00000000;/** SLEEP ON */privatebyteADXL345_SLEEP_ON=0b00000100;/** SLEEP OFF */privatebyteADXL345_SLEEP_OFF=0b00000000;/** WAKEUP 8Hz */privatebyteADXL345_WAKEUP_8HZ=0b00;/** WAKEUP 4Hz */privatebyteADXL345_WAKEUP_4HZ=0b01;/** WAKEUP 2Hz */privatebyteADXL345_WAKEUP_2HZ=0b10;/** WAKEUP 1Hz */privatebyteADXL345_WAKEUP_1HZ=0b11;/** TextView */TextViewmTextViewDX;TextViewmTextViewDY;TextViewmTextViewDZ;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mTextViewDX=(TextView)findViewById(R.id.textViewX);mTextViewDY=(TextView)findViewById(R.id.textViewY);mTextViewDZ=(TextView)findViewById(R.id.textViewZ);// Attempt to access the I2C devicetry{PeripheralManagerServicemanager=newPeripheralManagerService();mDevice=manager.openI2cDevice(I2C_DEVICE_NAME,I2C_ADDRESS);}catch(IOExceptione){Log.w(TAG,"Unable to access I2C device",e);}try{// Who am I.bytevalue=mDevice.readRegByte(0x00);if((value&0xff)==0xe5){Log.i(TAG,"Device is ADXL345");}// Configure.byteconf=ADXL345_SELF_TEST_OFF;conf|=ADXL345_SPI_OFF;conf|=ADXL345_INT_INVERT_OFF;conf|=ADXL345_FULL_RES_OFF;conf|=ADXL345_JUSTIFY_OFF;conf|=ADXL345_RANGE_16G;mDevice.writeRegByte(ADXL345_DATA_FORMAT_REG,conf);// PowerOn.bytepower=ADXL345_AUTO_SLEEP_OFF;power|=ADXL345_MEASURE_ON;power|=ADXL345_SLEEP_OFF;power|=ADXL345_WAKEUP_8HZ;mDevice.writeRegByte(ADXL345_POWER_CTL_REG,power);// Handler.mHandler.post(mAxisRunnable);}catch(IOExceptione){e.printStackTrace();}}privateRunnablemAxisRunnable=newRunnable(){@Overridepublicvoidrun(){if(mDevice==null){return;}try{intlength=6;byteaxis_buff[]=newbyte[length];mDevice.readRegBuffer(ADXL345_3AIXS,axis_buff,axis_buff.length);mHandler.postDelayed(mAxisRunnable,INTERVAL_BETWEEN_AXIS_MS);intx=(((int)axis_buff[1])<<8)|axis_buff[0];inty=(((int)axis_buff[3])<<8)|axis_buff[2];intz=(((int)axis_buff[5])<<8)|axis_buff[4];mTextViewDX.setText("X: "+x);mTextViewDY.setText("Y: "+y);mTextViewDZ.setText("Z: "+z);Log.i(TAG,"x="+x+" y="+y+" z="+z);}catch(IOExceptione){Log.e(TAG,"Error on I2C Device API",e);}}};@OverrideprotectedvoidonDestroy(){super.onDestroy();if(mDevice!=null){try{mDevice.close();mDevice=null;}catch(IOExceptione){Log.w(TAG,"Unable to close I2C device",e);}}}}