如何让Android Wear智能手表在加速度计事件上振动

How to get an android wear smartwatch to vibrate on an accelerometer event

How do i get the smartwatch to vibrate when sensor event is detected

package com.example.amanda.breathez;

import android.accessibilityservice.AccessibilityService;
import android.app.Fragment;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Vibrator;

import android.util.Log; 
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.Collections;
import java.util.LinkedList;


public class SensorFragment extends Fragment implements SensorEventListener {

private static final float SHAKE_THRESHOLD = 1.1f;
private static final int SHAKE_WAIT_TIME_MS = 250;
private static final float ROTATION_THRESHOLD = 2.0f;
private static final int ROTATION_WAIT_TIME_MS = 100;

private View mView;
private TextView mTextTitle;
private TextView mTextValues;
private SensorManager mSensorManager;
private Sensor mSensor;
private int mSensorType;
private long mShakeTime = 0;
private long mRotationTime = 0;

private float totalTime = 0;

LinkedList<Float> accelZData = new LinkedList<Float>();
LinkedList<Long> timeList = new LinkedList<Long>();

int listStartIndex = 0;

private int ATCount = 0;
private Vibrator mVibrator;

private float maxAccelAverage;
private float prevATMax = 0;
private int first = 0;
private int last = 150;
private long lastTime = 0;
private AccessibilityService Context;


public static SensorFragment newInstance(int sensorType) {
    SensorFragment f = new SensorFragment();

    // Supply sensorType as an argument
    Bundle args = new Bundle();
    args.putInt("sensorType", sensorType);
    f.setArguments(args);

    return f;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle args = getArguments();
    if(args != null) {
        mSensorType = args.getInt("sensorType");
    }

    mSensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
    mSensor = mSensorManager.getDefaultSensor(mSensorType);




}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    mView = inflater.inflate(R.layout.sensor, container, false);

    mTextTitle = (TextView) mView.findViewById(R.id.text_title);
    //mTextTitle.setText(mSensor.getStringType());
    mTextValues = (TextView) mView.findViewById(R.id.text_values);

    return mView;
}

@Override
public void onResume() {
    super.onResume();
    mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
public void onPause() {
    super.onPause();
    mSensorManager.unregisterListener(this);
}

@Override
public void onSensorChanged(SensorEvent event) {
    // If sensor is unreliable, then just return
    if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
    {
        return;
    }
    accelZData.add(event.values[2]);
    timeList.add(event.timestamp / 1000000);
    try {
        detectAbThrust2(event);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}
private void detectAbThrust2(SensorEvent event){

    Vibrator mVibrator = (Vibrator) this.Context.getSystemService(Context.VIBRATOR_SERVICE);
    Log.i("max : ", "" + event.values[2]);
    if(event.values[2]>15 && (event.timestamp/1000000 - lastTime) > 500){
        Log.i("prediction : ","" + "AT");
        ATCount = ATCount + 1;
        mView.setBackgroundColor(Color.rgb(0, 100, 0));
        mTextValues.setText(Integer.toString(ATCount));
        mVibrator.vibrate(500);
        lastTime = event.timestamp / 1000000;
    }
    else {
        mView.setBackgroundColor(Color.BLACK);
    }
}
}

I have also added this to my android manifest:

<uses-permission android:name="android.permission.VIBRATE"/>

I am getting this error when I try to run it: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Objectandroid.accessibilityservice.AccessibilityService.getSystemService(java.lang.String)' on a null object reference

Any help is appreciated.