Android Accelerometer无法正常运行

时间:2017-06-14 13:14:46

标签: android accelerometer sensor

我正在尝试通过端口将Android手机上的数据发送到计算机。但是几天前,我的加速度计开始工作不正常。无论我做什么,我都无法改变加速度计的频率,并且频率在4秒内被卡住了。我已经格式化我的手机然后再次安装我的应用程序并开始正​​常工作,但它是暂时的。安装完应用程序后,它再次开始工作不正常。我该怎么办?

Android代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    counter = 0;

    x = new float[windowSize*samplingRate];
    y = new float[windowSize*samplingRate];
    z = new float[windowSize*samplingRate];
    t = new long[windowSize*samplingRate];

    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mySensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    sensorManager.registerListener(this, mySensor, 50000);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    sensorManager.unregisterListener((SensorEventListener) this );
}

@Override
public void onSensorChanged(SensorEvent event) {

    x[counter] = event.values[0];
    y[counter] = event.values[1];
    z[counter] = event.values[2];
    t[counter] = System.nanoTime();

    if(counter == 199){
        TupFeat tupFeat = new TupFeat(0,"RealUser",System.nanoTime());
        tupFeat.setRaw(x,y,z,t);
        sendData(tupFeat);
        tupFeat = null;
        counter = 0;

        long time = t[199]-value;
        double seconds = (double)time / 1000000000.0;
        value = t[199];
        System.out.println("Second is :" + seconds);
    }
    else
        counter++;

}

private void sendData(TupFeat tupData){

    int SDK_INT = android.os.Build.VERSION.SDK_INT;
    if (SDK_INT > 8) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);
        try {
            Socket socket = null;
            ObjectOutputStream oos = null;
            ObjectInputStream ois = null;
            //establish socket connection to server

            System.out.println("Sending Data");
            socket = new Socket("192.168.205.59", 4848);

            oos = new ObjectOutputStream(socket.getOutputStream());
            System.out.println("Sending request to Socket Server");
            oos.writeObject(tupData);

            //read the server response message
            ois = new ObjectInputStream(socket.getInputStream());
            String message = (String) ois.readObject();
            System.out.println("Message: " + message);
            //close resources
            ois.close();
            oos.close();

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

0 个答案:

没有答案