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.AsyncTask; 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.view.View; import android.widget.Button; 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 java.util.Timer; import java.util.TimerTask; 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 mTvTemperature; private TextView mTvLight; private TextView mTvPressure; private Button button; private Timer timer; private TimerTask timerTask; /// WifiManager wifi; List list; TextView show; String csum; // Sensor 管理服务 private SensorManager mSensorManager; private StringBuilder wifiinformation; //时间 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); button = (Button) findViewById(R.id.button); wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); if(! wifi.isWifiEnabled()){ if(wifi.getWifiState() != WifiManager.WIFI_STATE_ENABLING) wifi.setWifiEnabled(true); } wifiinformation = new StringBuilder(); // 方向传感器 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); timer = new Timer(); final TimerTask timerTask = new TimerTask() { @Override public void run() { new MyTask().execute(); } }; timer.schedule(timerTask,0,500); } class MyTask extends AsyncTask<String,Void,String>{ @Override protected String doInBackground(String... strings) { wifi.startScan(); List<ScanResult> results = wifi.getScanResults(); //得到WIFI列表信息 wifiinformation.setLength(0); 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); try { //Date date = new Date(); //File extDir= Environment.getExternalStorageDirectory() ; 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(); } return wifiinformation.toString(); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); show.setText(s); } } @Override protected void onDestroy() { // 退出时取消传感器监听器 mSensorManager.unregisterListener(this); timer.cancel(); 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); mTvTemperature = (TextView) findViewById(R.id.tv_temperature); 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; // 根据不同的传感器做出不同的响应 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(); } /*if (Build.VERSION.SDK_INT >= 23 && !AppUtil.isGpsOPen(MainActivity.this)) { Settings.Secure.putInt(getContentResolver(),Settings.Secure.LOCATION_MODE, 1); }*/ 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_AMBIENT_TEMPERATURE: sb = new StringBuilder(); sb.append("温度传感器的数值:"); sb.append("\n当前的温度:"); sb.append(values[0]); mTvTemperature.setText(sb.toString()); break; case Sensor.TYPE_LIGHT: sb = new StringBuilder(); sb.append("光线传感器的数值:"); sb.append("\n当前的光线强度:"); sb.append(values[0]); mTvLight.setText(sb.toString()); // /// break; case Sensor.TYPE_PRESSURE: sb = new StringBuilder(); sb.append("压力传感器的数值:"); sb.append("\n当前的压力:"); sb.append(values[0]); mTvPressure.setText(sb.toString()); break; default: break; } } /** * 传感器精度改变时的回调 * * @param sensor * @param i */ @Override public void onAccuracyChanged(Sensor sensor, int i) { } }
安卓传感器+WIFI,WIFI写在外部,时间可控
最新推荐文章于 2021-05-28 02:21:30 发布