其他分享
首页 > 其他分享> > 从Smartwatch Android Wear获取数据到智能手机

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

作者:互联网

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

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

从android页面实现代码后,我无法在设备上运行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");
            }
        }
    }

解决方法:

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

https://github.com/pocmo/SensorDashboard

标签:android,wear-os,sony-smartwatch,moto-360
来源: https://codeday.me/bug/20190716/1476715.html