From 6a3a763e685e1f39b3297200bfdb32215967c4b1 Mon Sep 17 00:00:00 2001 From: JLChen <551775569@qq.com> Date: 星期五, 18 六月 2021 17:51:47 +0800 Subject: [PATCH] 2021-6-18 1.光感距离和光感亮度,自动操作代码 --- PX30SmatekAPI/PX30SmatekAPI/app/src/main/java/com/smatek/px30smatekapi/MainActivity.java | 78 ++++++++++++++++++++++++++++++++++++++ 1 files changed, 77 insertions(+), 1 deletions(-) diff --git a/PX30SmatekAPI/PX30SmatekAPI/app/src/main/java/com/smatek/px30smatekapi/MainActivity.java b/PX30SmatekAPI/PX30SmatekAPI/app/src/main/java/com/smatek/px30smatekapi/MainActivity.java index 15a582f..a191fff 100644 --- a/PX30SmatekAPI/PX30SmatekAPI/app/src/main/java/com/smatek/px30smatekapi/MainActivity.java +++ b/PX30SmatekAPI/PX30SmatekAPI/app/src/main/java/com/smatek/px30smatekapi/MainActivity.java @@ -4,6 +4,10 @@ import android.app.SmatekManager; 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.media.AudioManager; import android.os.Build; import android.os.SystemClock; @@ -26,6 +30,8 @@ import com.smatek.px30smatekapi.utils.EthernetUtils; import java.lang.reflect.Method; + +import static android.content.ContentValues.TAG; public class MainActivity extends AppCompatActivity implements View.OnClickListener, SeekBar.OnSeekBarChangeListener { @@ -53,6 +59,10 @@ private static String TAG = "MainActivity"; private MyISmatekListener myISmatekListener; + private SensorManager mSensorManager; + private MySensorEventListener sensorEventListener;//鍏夌嚎璺濈浼犳劅鍣� + + private int screenBrightness = 100; @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) @Override @@ -63,6 +73,13 @@ smatekManager = (SmatekManager) getSystemService("smatek"); myISmatekListener = new MyISmatekListener(); smatekManager.addSmatekListener(myISmatekListener); + //鍏夌嚎璺濈浼犳劅鍣� + mSensorManager = (SensorManager)this.getSystemService(this.SENSOR_SERVICE); + sensorEventListener = new MySensorEventListener(); + Sensor als = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); + mSensorManager.registerListener(sensorEventListener, als,SensorManager.SENSOR_DELAY_NORMAL); + Sensor ps = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); + mSensorManager.registerListener(sensorEventListener, ps,SensorManager.SENSOR_DELAY_NORMAL); } @@ -298,6 +315,7 @@ smatekManager.setLcdBlackLight(true); break; case R.id.btn_close_back_light: +// isScreenClose = true; smatekManager.setLcdBlackLight(false); break; case R.id.btn_statusBar_show: @@ -392,6 +410,7 @@ break; case R.id.btn_screen: + isScreenClose = true; smatekManager.openOrCloseScreen(true); break; case R.id.btn_screen_open: @@ -566,7 +585,7 @@ @Override public void onKeyEvent(int i, int i1) { - Log.e(TAG, " i :" + i + " i1 :" + i1); + Log.e(TAG, " 灏忔澘浜嬩欢Key i :" + i + " i1 :" + i1); } @Override @@ -584,4 +603,61 @@ } } + + boolean isScreenClose = false; + //妫�娴嬪厜鎰熻窛绂诲皬浜�30cm锛屽皬浜庡鏋滃睆骞曞叧闂垯閲嶆柊鎵撳紑 + private void checkWakeUpScreen(float mPROXIMITY){ + if((65535.0 - mPROXIMITY) < 30){ + Log.e(TAG, "灏忔澘 妫�娴嬪厜鎰熻窛绂诲皬浜�30"); + //鍏夋劅璺濈灏忎簬30cm + if(isScreenClose){ + isScreenClose = false; + //鎵撳紑灞忓箷鑳屽厜 + Log.e(TAG, "灏忔澘 閲嶆柊鎵撳紑灞忓箷"); + smatekManager.openOrCloseScreen(false); + } + } + + } + + long setTime = 0; + //鑷姩璋冭妭浜害 + private void autoSetScreenBrightness(float mLIGHT){ + long timecurrentTimeMillis = System.currentTimeMillis(); + if((timecurrentTimeMillis - setTime) < 200) return; + //姣�200ms鏀瑰彉涓�娆� + setTime = timecurrentTimeMillis; + + double percent = 1 - (mLIGHT / 9535.0); + + screenBrightness = (int)(percent * 255); + + if(screenBrightness > 255){ + screenBrightness = 255; + } + + if(screenBrightness < 0){ + screenBrightness = 0; + } + + Log.e(TAG, "灏忔澘 鑷姩璋冭妭浜害锛�"+screenBrightness); + smatekManager.setBrightness(screenBrightness); + } + + private final class MySensorEventListener implements SensorEventListener { + @Override + public void onSensorChanged(SensorEvent event) { + if(event.sensor.getType() == Sensor.TYPE_LIGHT) { + Log.e(TAG, "灏忔澘 鍏夋劅浜害 values = " + event.values[0]); + autoSetScreenBrightness(event.values[0]); + } else if (event.sensor.getType() == Sensor.TYPE_PROXIMITY){ + Log.e(TAG, "灏忔澘 鍏夋劅璺濈 values = " +event.values[0]); + checkWakeUpScreen(event.values[0]); + } + } + @Override + public void onAccuracyChanged(Sensor sensor, int accuracy){ + } + } } + -- Gitblit v1.8.0