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

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

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

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

从 android 页面实现代码后,我无法在设备上运行该项目!

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

最佳答案

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

https://github.com/pocmo/SensorDashboard

关于android - 从 Smartwatch Android Wear 获取数据到智能手机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27388443/

相关文章:

wear-os - Android Wear ChannelApi示例?

java - Joda Time 给出了不正确的时区时间

android - 是否可以制作一个连接到我的 android 应用程序 Activity ( map )的磨损应用程序

android - 在sony smartwatch 2中生成动态列表

android - Android Wear 中的 Kiosk 模式/屏幕固定

android - 如何从 Android 中的 View 更新 View 模型

android - 拍的照片比预览大

Android Wear DataMap 条目有效期多长时间

android - 对我来说,添加 android :exported ="true" does not solve the problem

android - 带有 ionic 4 build 的大尺寸 apk