安卓传感器+WIFI

package com.cnlgming.sensorsdemo;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.net.Uri;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.SystemClock;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static android.content.Context.MODE_WORLD_READABLE;


public class MainActivity extends AppCompatActivity implements SensorEventListener {

    private TextView mTvOrientation;
    private TextView mTvGyroscope;
    private TextView mTvMagnetic;
    private TextView mTvGravity;
    private TextView mTvLinearAcceleration;
    private TextView mTvProximity;
    private TextView mTvLight;
    private TextView mTvPressure;
    ///
    WifiManager wifi;
    List list;
    TextView show;
    String csum;
    // Sensor 管理服务
    private SensorManager mSensorManager;
    int count =0;

    //时间
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private SimpleDateFormat sdfm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    private SimpleDateFormat sdfymd = new SimpleDateFormat("yyyyMMddHHmm");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        assignViews();
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);



    }

    @Override
    protected void onResume() {
        super.onResume();
        // 注册传感器监听器

        // 方向传感器
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_GAME);
        // 陀螺仪传感器
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE),
                SensorManager.SENSOR_DELAY_GAME);

        // 磁场传感器
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
                SensorManager.SENSOR_DELAY_GAME);

        // 重力传感器
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY),
                SensorManager.SENSOR_DELAY_GAME);

        // 线性加速度传感器
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION),
                SensorManager.SENSOR_DELAY_GAME);


        // 温度传感器
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE),
                SensorManager.SENSOR_DELAY_GAME);

        // 光线传感器
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT),
                SensorManager.SENSOR_DELAY_GAME);

        // 压力传感器

        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE),
                SensorManager.SENSOR_DELAY_GAME);
        //距离传感器
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY),
                SensorManager.SENSOR_DELAY_GAME);



    }

    @Override
    protected void onDestroy() {
        // 退出时取消传感器监听器
        mSensorManager.unregisterListener(this);
        super.onDestroy();
    }

    private void assignViews() {
        mTvOrientation = (TextView) findViewById(R.id.tv_orientation);
        mTvGyroscope = (TextView) findViewById(R.id.tv_gyroscope);
        mTvMagnetic = (TextView) findViewById(R.id.tv_magnetic);
        mTvGravity = (TextView) findViewById(R.id.tv_gravity);
        mTvLinearAcceleration = (TextView) findViewById(R.id.tv_linear_acceleration);
        mTvProximity = (TextView) findViewById(R.id.tv_proximity);
        mTvLight = (TextView) findViewById(R.id.tv_light);
        mTvPressure = (TextView) findViewById(R.id.tv_pressure);
        ///
        show =(TextView)findViewById(R.id.textView1);
        ///
    }

    /**
     * 传感器数值改变时的回调
     *
     * @param sensorEvent
     */
    @Override

    public void onSensorChanged(SensorEvent sensorEvent) {
        // 获取变化的传感器对应的数值
        int type = sensorEvent.sensor.getType();
        // 获取变化的值
        float[] values = sensorEvent.values;

        StringBuilder sb = null;
        StringBuilder sb1 = null;
        // 根据不同的传感器做出不同的响应

        Date date = new Date();
        File extDir= Environment.getExternalStorageDirectory() ;


        //StringBuilder myOrientation1=null
        switch (type) {
            case Sensor.TYPE_ORIENTATION:
                sb = new StringBuilder();
                //sb.append("方向:");
                //sb.append("\n绕 X 轴旋转的角度:");
                sb.append(values[0]+" ");
                //sb.append("\n绕 Y 轴旋转的角度:");
                sb.append(values[1]+" ");
                //sb.append("\n绕 Z 轴旋转的角度:");
                sb.append(values[2]+"\n");
                mTvOrientation.setText(sb.toString());
                try {

                    String fileName="fangXiangSensor.txt";
                    File fullFilename =new File(extDir,fileName);
                    //FileOutputStream outputStream = openFileOutput(fileName, Context.MODE_APPEND + MODE_WORLD_READABLE);
                    FileOutputStream outputStream = new FileOutputStream(fullFilename,true);
                    String str = sdfm.format(date) +" "+ sb.toString();
                    //mTvOrientation.setText(str);
                    outputStream.write(str.getBytes());
                    outputStream.close();
                }catch (FileNotFoundException e) {
                    e.printStackTrace();
                    Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
                //

                //
                break;


            case Sensor.TYPE_GYROSCOPE:
                sb = new StringBuilder();
                //sb.append("陀螺仪:");
                //sb.append("\n绕 X 轴旋转的角速度:");
                sb.append(values[0]+" ");
                //sb.append("\n绕 Y 轴旋转的角速度:");
                sb.append(values[1]+" ");
                //sb.append("\n绕 Z 轴旋转的角速度:");
                sb.append(values[2]+"\n");
                mTvGyroscope.setText(sb.toString());
                try {
                    String fileName="tuoLuoYiSensor.txt";
                    File fullFilename1 =new File(extDir,fileName);
                    //FileOutputStream outputStream = openFileOutput(fileName, Context.MODE_APPEND + MODE_WORLD_READABLE);
                    FileOutputStream outputStream = new FileOutputStream(fullFilename1,true);
                    String str = sdfm.format(date)+" " + sb.toString();
                    outputStream.write(str.getBytes());
                    outputStream.close();
                }catch (FileNotFoundException e) {
                    e.printStackTrace();
                    //Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                    //Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
                break;



            case Sensor.TYPE_MAGNETIC_FIELD:
                sb = new StringBuilder();
                //sb.append("磁场:");
                //sb.append("\nX 轴上的磁场:");
                sb.append(values[0]+" ");
                //sb.append("\nY 轴上的磁场:");
                sb.append(values[1]+" ");
                //sb.append("\nZ 轴上的磁场:");
                sb.append(values[2]+"\n");
                mTvMagnetic.setText(sb.toString());
                try {
                    String fileName="diCiSensor.txt";
                    File fullFilename2 =new File(extDir,fileName);
                    if(!fullFilename2.exists()){
                        fullFilename2.createNewFile();
                    }
                    //FileOutputStream outputStream = openFileOutput(fileName, Context.MODE_APPEND + MODE_WORLD_READABLE);
                    FileOutputStream outputStream = new FileOutputStream(fullFilename2,true);
                    String str = sdfm.format(date) +" "+ sb.toString();
                    outputStream.write(str.getBytes());
                    outputStream.close();
                }catch (FileNotFoundException e) {
                    e.printStackTrace();
                    //Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                    //Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
                break;


            case Sensor.TYPE_GRAVITY:
                sb = new StringBuilder();
                //sb.append("重力:");
                //sb.append("\nX 轴方向上的重力:");
                sb.append(values[0]+" ");
                //sb.append("\nY 轴方向上的重力:");
                sb.append(values[1]+" ");
                //sb.append("\nZ 轴方向上的重力:");
                sb.append(values[2]+"\n");
                mTvGravity.setText(sb.toString());
                try {
                    String fileName="zhongLiSensor.txt";
                    File fullFilename3 =new File(extDir,fileName);
                    //FileOutputStream outputStream = openFileOutput(fileName, Context.MODE_APPEND + MODE_WORLD_READABLE);
                    FileOutputStream outputStream = new FileOutputStream(fullFilename3,true);
                    String str = sdfm.format(date)+" " + sb.toString();
                    outputStream.write(str.getBytes());
                    outputStream.close();
                }catch (FileNotFoundException e) {
                    e.printStackTrace();
                    //Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                    //Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }

                count=count+1;
                if (count>=25) {


                    wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
                    if (!wifi.isWifiEnabled()) {
                        if (wifi.getWifiState() != WifiManager.WIFI_STATE_ENABLING)
                            wifi.setWifiEnabled(true);
                    }
                /*if (Build.VERSION.SDK_INT >= 23 && !AppUtil.isGpsOPen(MainActivity.this)) {
                    Settings.Secure.putInt(getContentResolver(),Settings.Secure.LOCATION_MODE, 1);
                }*/


                    wifi.startScan();
                    List<ScanResult> results = wifi.getScanResults();
                    StringBuilder wifiinformation = new StringBuilder();
                    //得到WIFI列表信息
                    for (ScanResult result : results) {
                        wifiinformation.append(result.BSSID);
                        wifiinformation.append(" ");
                        wifiinformation.append(result.level);
                        wifiinformation.append(" ");
                        //wifiinformation.append("\n");

                    }

                    WifiInfo info = wifi.getConnectionInfo();
                    int strength = info.getRssi();
                    int speed = info.getLinkSpeed();
                    //String bssid = info.getBSSID();
                    String ssid = info.getSSID();
                    String units = WifiInfo.LINK_SPEED_UNITS;

                    String text = "We connect" + ssid + " at " + String.valueOf(speed) + " " + String.valueOf(units) + ". Strength : " + strength;
                    wifiinformation.append(" ");
                    wifiinformation.append(text);
                    //results.clear();
                    show.setText(wifiinformation.toString());
                    try {
                        //Date date = new Date();
                        //File extDir= Environment.getExternalStorageDirectory() ;
                        String fileName = "wifiInformation.txt";
                        File fullFilename = new File(extDir, fileName);
                        //FileOutputStream outputStream = openFileOutput(fileName, Context.MODE_APPEND + MODE_WORLD_READABLE);
                        FileOutputStream outputStream = new FileOutputStream(fullFilename, true);
                        String str = sdfm.format(date) + " " + wifiinformation.toString() + "\n";
                        //mTvOrientation.setText(str);
                        outputStream.write(str.getBytes());
                        outputStream.close();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                        Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                    } catch (IOException e) {
                        e.printStackTrace();
                        Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                    count=0;
                }

                break;


            case Sensor.TYPE_LINEAR_ACCELERATION:
                sb = new StringBuilder();
                //sb.append("线性加速度:");
                //sb.append("\nX 轴方向上的线性加速度:");
                sb.append(values[0]+" ");
                //sb.append("\nY 轴方向上的线性加速度:");
                sb.append(values[1]+" ");
                //sb.append("\nZ 轴方向上的线性加速度:");
                sb.append(values[2]+"\n");
                mTvLinearAcceleration.setText(sb.toString());
                try {
                    String fileName="xianJiaSuDuSensor.txt";
                    File fullFilename0 =new File(extDir,fileName);
                    //FileOutputStream outputStream = openFileOutput(fileName, Context.MODE_APPEND + MODE_WORLD_READABLE);
                    FileOutputStream outputStream = new FileOutputStream(fullFilename0,true);
                    String str = sdfm.format(date) +" "+ sb.toString();
                    outputStream.write(str.getBytes());
                    outputStream.close();
                }catch (FileNotFoundException e) {
                    e.printStackTrace();
                    //Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                    //Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
                break;


            case Sensor.TYPE_PROXIMITY:
                sb = new StringBuilder();
                //sb.append("距离:");
                //sb.append("\n当前的温度:");
                sb.append(values[0]+" ");
                mTvProximity.setText(sb.toString());
                try {
                    String fileName="juLiSensor.txt";
                    File fullFilename6 =new File(extDir,fileName);
                    //FileOutputStream outputStream = openFileOutput(fileName, Context.MODE_APPEND + MODE_WORLD_READABLE);
                    FileOutputStream outputStream = new FileOutputStream(fullFilename6,true);
                    String str = sdfm.format(date) +" "+ sb.toString()+"\n";
                    outputStream.write(str.getBytes());
                    outputStream.close();
                }catch (FileNotFoundException e) {
                    e.printStackTrace();
                    //Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                    //Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }

                break;
            case Sensor.TYPE_LIGHT:
                sb = new StringBuilder();
                //sb.append("光线:");
                //sb.append("\n当前的光线强度:");
                sb.append(values[0]+" ");
                mTvLight.setText(sb.toString());
                try {
                    String fileName="guangXianSensor.txt";
                    File fullFilename6 =new File(extDir,fileName);
                    //FileOutputStream outputStream = openFileOutput(fileName, Context.MODE_APPEND + MODE_WORLD_READABLE);
                    FileOutputStream outputStream = new FileOutputStream(fullFilename6,true);
                    String str = sdfm.format(date) +" "+ sb.toString()+"\n";
                    outputStream.write(str.getBytes());
                    outputStream.close();
                }catch (FileNotFoundException e) {
                    e.printStackTrace();
                    //Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                    //Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }



                break;




            case Sensor.TYPE_PRESSURE:
                sb1 = new StringBuilder();
                //sb1.append("气压计:");
                //sb1.append("\n当前的压力:");
                sb1.append(values[0]+" ");
                mTvPressure.setText(sb1.toString());
                try {
                    String fileName="qiYaJi.txt";
                    File fullFilename6 =new File(extDir,fileName);
                    //FileOutputStream outputStream = openFileOutput(fileName, Context.MODE_APPEND + MODE_WORLD_READABLE);
                    FileOutputStream outputStream = new FileOutputStream(fullFilename6,true);
                    String str = sdfm.format(date) +" "+ sb.toString()+"\n";
                    outputStream.write(str.getBytes());
                    outputStream.close();
                }catch (FileNotFoundException e) {
                    e.printStackTrace();
                    //Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                    //Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }


                break;
            /*case Sensor.TYPE_PROXIMITY:
                sb1 = new StringBuilder();
                sb1.append("距离:");
                //sb1.append("\n当前的压力:");
                sb1.append(values[0]);
                mTvPressure.setText(sb1.toString());
                break;*/

            default:
                break;
        }
    }

    /**
     * 传感器精度改变时的回调
     *
     * @param sensor
     * @param i
     */
    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值