使用uts的方式调用光感器
import { MyApiOptions} from '../interface.uts';
import Context from "android.content.Context";
import { UTSAndroid } from "io.dcloud.uts";
import Sensor from 'android.hardware.Sensor';
import SensorEvent from 'android.hardware.SensorEvent';
import SensorEventListener from 'android.hardware.SensorEventListener';
import SensorManager from 'android.hardware.SensorManager';
export function getSensorLight(options:MyApiOptions) {
// 获取android系统 application上下文
const context = UTSAndroid.getAppContext();
if(context != null){
const sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager;
const lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
sensorManager.registerListener(class implements SensorEventListener{
override onSensorChanged(sensorEvent:SensorEvent) {
const value = sensorEvent.values[0];
options.complete?.(value)
}
override onAccuracyChanged(sensor:Sensor, i:Int) {
}
}, lightSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
调用
<script>
import {getSensorLight} from '@/uni_modules/sensor-light'
getSensorLight({
complete: (ret) => {
console.log(ret);
}
})
</script>
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
上次更新: 2024/06/14, 10:48:20