java

package com.example.suruixue.myapplication;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

public class MainActivity extends AppCompatActivity implements SensorEventListener,CompoundButton.OnCheckedChangeListener{
    private String TAG = "SensorTest";
    private TextView tSensor = null;
    private  SensorManager mSensorManager;
    private  Sensor mAccelerometer;
    private List<Sensor> mAccelerometer1;
    private Vibrator vibrator;
    private ToggleButton tb1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tSensor = (TextView)findViewById(R.id.sensordata);
        mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mAccelerometer1 = mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);

        Log.i(TAG, "srx-acc1-"+mAccelerometer1.get(0));

        if(mAccelerometer == null){
            Log.i(TAG,"srx--acc not exit--");
        }
         if(mAccelerometer1 == null){
             Log.i(TAG,"srx--acc1 not exit--");
        }
        Log.i(TAG,"srx--acc may exit:"+getNodeExists("/sys/bus/platform/drivers/gsensor/sensordata") );
        mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);

        /* vibrator */
        vibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
        tb1 = (ToggleButton) findViewById(R.id.vibr_1);
        tb1.setOnCheckedChangeListener(this);

        Log.i(TAG,"srx--vibraotr exits:"+getNodeExists( "/sys/class/timed_output/vibrator/enable") );
        Log.i(TAG,"srx--vibraotr srx exits:"+getNodeExists("dev/srx_vibrator1") );

        File accFile=new File("/sys/bus/platform/drivers/gsensor/sensordata");
        if(!accFile.exists()){
            Log.i(TAG,"acc not exist");
        } else {
            Log.i(TAG,"acc exist");
        }
        File vibrFile = new File("dev/srx_vibrator1");
        if(!vibrFile.exists()){
            Log.i(TAG,"vibr not exist");
        } else {
            Log.i(TAG,"vibr exist");
        }
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
      //  Log.i(TAG,"onSensorChanged--type="+event.sensor.getName());
        if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
         //   Log.i(TAG,"x="+ event.values[0]+"y="+ event.values[1]+"z="+ event.values[2]);
            tSensor.setText(String.valueOf("x="+ event.values[0]+"y="+ event.values[1]+"z="+ event.values[2]));
          //  String,valueOf("x="+ event.values[0]+"y="+ event.values[1]+"z="+ event.values[2]);
        }
    }
    private static String getNodeExists(String path) {
        String prop = null;
        try {
            BufferedReader reader = new BufferedReader(new FileReader(path));
            prop = reader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return prop;
    }
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

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

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Log.i(TAG,"onCheckedChanged--");
        if(isChecked) {
            Log.i(TAG,"isChecked--");
            if (buttonView == tb1) {

                if(vibrator.hasVibrator()){
                    Log.i(TAG,"hasvibrator,vibrate 5s--");
                    vibrator.vibrate(5000);
                } else {
                    Log.i(TAG,"no vibrator-");
                }

            }
        } else {
            //close vibrator
            vibrator.cancel();
        }

    }
}

  activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/sensordata"
            android:text="Hello World!"
            />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="单次震动" />
    <ToggleButton
        android:id="@+id/vibr_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOff="开启"
        android:textOn="关闭"/>
    </LinearLayout>


</android.support.constraint.ConstraintLayout>

  AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.suruixue.myapplication">

<uses-permission android:name="android.permission.VIBRATE" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity"
        android:configChanges="orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

  

posted on 2018-05-18 19:53  snowdrop  阅读(270)  评论(0编辑  收藏  举报