//add SensorEventListener to receive the data from sensors.
public class MainActivity extends WearableActivity implements SensorEventListener{
private static final SimpleDateFormat AMBIENT_DATE_FORMAT =
new SimpleDateFormat("HH:mm", Locale.US);
private BoxInsetLayout mContainerView;
private TextView mTextView;
private TextView mClockView;
SensorManager mSensorManager;
// Add Sensor and Listener to Recieve data from the sensor
Sensor mGyroscopeSensor;
SensorEventListener sensorEventListener;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setAmbientEnabled();
mContainerView = (BoxInsetLayout) findViewById(R.id.container);
mTextView = (TextView) findViewById(R.id.text);
mClockView = (TextView) findViewById(R.id.clock);
mSensorManager = ((SensorManager) getSystemService(SENSOR_SERVICE));
List<Sensor> sensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL);
// Get Sensor using SensorManager and add Listener on the SensorManager
mGyroscopeSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
mSensorManager.registerListener(this, mGyroscopeSensor,SensorManager.SENSOR_DELAY_FASTEST);
}
By adding implements "SensorEventListenr", we need to implement two methods.One is "onSensorChanged". The order one is "onAccuracyChanged". We just input code in the "onSensorChanged" :
@Overridepublic void onSensorChanged(SensorEvent sensorEvent) {
if(sensorEvent.sensor.getType() == Sensor.TYPE_GYROSCOPE){
// get gyroscope data.
String str="";
for(int i=0; i< sensorEvent.values.length;i++){
str=str+"GYROSCOPE " +i+":";
str=str+(int)sensorEvent.values[i];
}
mTextView.setText(str);
}
}
@Overridepublic void onAccuracyChanged(Sensor sensor, int i) {
}
Thank you for reading my post. I hope that you find some of this information useful.







No comments:
Post a Comment