Get Data from Android Wear Smartwatch to Smartphone

I made several apps (pedometer, heart rate, audio recorder) for moto360 with android wear. everything works fine, but I don't know how to save data on watch and how to access data on smartphone. I was able to send messages to the watch, but I cannot send data from phone to phone. I can save my data on my smartphone, but I don't know how to manage it on the smartwatch. can anyone show me a tutorial or example? Thank you very much!

edit: The following code below is used to track heart rate on Moto360 and it works great. I tried to transfer data from hour to phone to use this tutorial -> https://developer.android.com/training/wearables/data-layer/data-items.html

After injecting the code on the android page, I couldn't run the project on the device!

    public class MainActivity extends Activity implements SensorEventListener {


        private static final String TAG = "MainActivity";
        private TextView mTextViewStepCount;
        private TextView mTextViewStepDetect;
        private TextView mTextViewHeart;
        PutDataMapRequest dataMap = PutDataMapRequest.create("/count");
        GoogleApiClient mGoogleApiClient;

        @Override
        protected void onCreate(Bundle savedInstanceState) {


            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
            stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
                @Override
                public void onLayoutInflated(WatchViewStub stub) {

                    mTextViewStepCount = (TextView) stub.findViewById(R.id.step_count);
                    mTextViewStepDetect = (TextView) stub.findViewById(R.id.step_detect);
                    mTextViewHeart = (TextView) stub.findViewById(R.id.heart);
                    getStepCount();

                }
            });
        }

        private void getStepCount() {
            SensorManager mSensorManager = ((SensorManager) getSystemService(SENSOR_SERVICE));
            Sensor mHeartRateSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE);
            Sensor mStepCountSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
            Sensor mStepDetectSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);

            mSensorManager.registerListener(this, mHeartRateSensor, SensorManager.SENSOR_DELAY_NORMAL);
            mSensorManager.registerListener(this, mStepCountSensor, SensorManager.SENSOR_DELAY_NORMAL);
            mSensorManager.registerListener(this, mStepDetectSensor, SensorManager.SENSOR_DELAY_NORMAL);
        }

        private String currentTimeStr() {
            Calendar c = Calendar.getInstance();
            SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
            return df.format(c.getTime());
        }

        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            Log.d(TAG, "onAccuracyChanged - accuracy: " + accuracy);
        }

        public void onSensorChanged(SensorEvent event) {
            if (event.sensor.getType() == Sensor.TYPE_HEART_RATE) {
                String msg = "" + (int) event.values[0];

                mTextViewHeart.setText(msg);
                Log.d(TAG, msg);

            } else if (event.sensor.getType() == Sensor.TYPE_STEP_COUNTER) {
                String msg = "Count: " + (int) event.values[0];
                mTextViewStepCount.setText(msg);
                Log.d(TAG, msg);
            } else if (event.sensor.getType() == Sensor.TYPE_STEP_DETECTOR) {
                String msg = "Detected at " + currentTimeStr();
                mTextViewStepDetect.setText(msg);
                Log.d(TAG, msg);
            } else {
                Log.d(TAG, "Unknown sensor type");
            }
        }
    }

      

+3


source to share


2 answers


this code helps me a lot, i hope it helps many other people :)



https://github.com/pocmo/SensorDashboard

+11


source


You need to use data. Sample image:

private static Asset createAssetFromBitmap(Bitmap bitmap) {
    final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteStream);
    return Asset.createFromBytes(byteStream.toByteArray());
}

      

and then



Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
Asset asset = createAssetFromBitmap(bitmap);
PutDataRequest request = PutDataRequest.create("/image");
request.putAsset("profileImage", asset);
Wearable.DataApi.putDataItem(mGoogleApiClient, request);

      

more http://developer.android.com/training/wearables/data-layer/assets.html

0


source







All Articles