@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setAmbientEnabled(); mContainerView = (BoxInsetLayout) findViewById(R.id.container); mImageView = (ImageView) findViewById(R.id.imageView); gestureDetector = new GestureDetector(getApplicationContext(), new GameGestureListener()); mImageView.setOnTouchListener( new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return gestureDetector.onTouchEvent(event); } }); mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mGyroSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE); mHeartSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE); mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(Wearable.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); // up vector cal mHeadTracker = HeadTracker.createFromContext(getApplicationContext()); headView = new float[16]; }
void sendHeadVector() { mHeadTracker.getLastHeadView(headView, 0); // Matrix.rotateM(headView, 0, 180, 0, 0, 1); //upside down SensorManager.remapCoordinateSystem( headView, SensorManager.AXIS_Z, SensorManager.AXIS_MINUS_Y, headView); // SensorManager.remapCoordinateSystem(headView, SensorManager.AXIS_MINUS_Z, // SensorManager.AXIS_Y, headView); //rotate for sensor. PutDataMapRequest req = PutDataMapRequest.create("/head"); for (int i = 0; i < 16; ++i) { req.getDataMap().putFloat(HEAD_PRE + i, headView[i]); } req.getDataMap().putLong("time", new Date().getTime()); Wearable.DataApi.putDataItem(mGoogleApiClient, req.asPutDataRequest()) .setResultCallback( new ResultCallback<DataApi.DataItemResult>() { @Override public void onResult(DataApi.DataItemResult dataItemResult) { if (dataItemResult.getStatus().isSuccess()) { Log.d("TEST", "Data item set: " + dataItemResult.getDataItem().getUri()); } else if (dataItemResult.getStatus().isCanceled()) { Log.d("TEST", "canceled"); } else if (dataItemResult.getStatus().isInterrupted()) { Log.d("TEST", "interrupted"); } } }); }
@Override protected void onPause() { super.onPause(); if (mGoogleApiClient != null) Wearable.DataApi.removeListener(mGoogleApiClient, this); if (mSensorManager != null) mSensorManager.unregisterListener(this); if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) mGoogleApiClient.disconnect(); if (mHeadTracker != null) mHeadTracker.stopTracking(); }
@Override protected void onResume() { super.onResume(); if (mSensorManager != null) { mSensorManager.registerListener(this, mGyroSensor, SensorManager.SENSOR_DELAY_NORMAL); mSensorManager.registerListener(this, mHeartSensor, SensorManager.SENSOR_DELAY_NORMAL); } if (mGoogleApiClient != null) mGoogleApiClient.connect(); if (mHeadTracker != null) mHeadTracker.startTracking(); }