从Smartwatch Android Wear获取数据到智能手机

时间:2014-12-09 20:41:37

标签: android wear-os sony-smartwatch moto-360

我为带有android的moto360制作了一些应用程序(计步器,心率,录音机)。一切正常,但我不知道如何保存手表上的数据以及如何访问智能手机上的数据。我已设法向手表发送消息,但我无法将手表中的数据发送到手机。我可以将数据保存在智能手机上,但我不知道如何在智能手表上管理它。有人可以给我一个教程或一个例子吗?非常感谢你!

编辑: 以下代码用于跟踪Moto360的心率,它工作正常。 我尝试将数据从手表传输到手机,因为我使用了本教程 - > https://developer.android.com/training/wearables/data-layer/data-items.html

从android页面实现Code后,我无法在设备上运行Project!

    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");
            }
        }
    }

2 个答案:

答案 0 :(得分:11)

这段代码对我很有帮助,我希望它可以帮助很多其他人:)

https://github.com/pocmo/SensorDashboard

答案 1 :(得分:0)

您需要使用数据资产。图片示例:

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

然后

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);

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