-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSensorTest.js
72 lines (68 loc) · 1.64 KB
/
SensorTest.js
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import React, { Component } from 'react';
import {
StyleSheet,
Slider,
View,
Text,
TouchableHighlight,
DeviceEventEmitter,
NativeModules,
} from 'react-native';
var mSensorManager = require('NativeModules').SensorManager;
var {ScreenBrightness} = NativeModules;
var SensorTest = React.createClass({
getInitialState: function() {
return {
light: null,
value: 0,
}
},
componentDidMount() {
mSensorManager.startLightSensor(100);
DeviceEventEmitter.addListener('LightSensor', function (data) {
//console.log(data.light);
this.setState({
light: data.light,
});
if(this.state.light >= 10000){
ScreenBrightness.setBrightness(1);
}
else if(this.state.light > 40){
ScreenBrightness.setBrightness(0.7);
}
else {
ScreenBrightness.setBrightness(0);
}
}.bind(this));
},
_onValueChange(value){
this.setState({value: value});
ScreenBrightness.setBrightness(this.state.value);
},
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>{this.state.light}</Text>
<Text style={styles.welcome}>
亮度
{this.state.value && +this.state.value.toFixed(2)}
</Text>
<Slider
{...this.props}
onValueChange={(value) => this._onValueChange(value)} />
</View>
);
}
});
var styles = StyleSheet.create({
container:{
flex: 1,
justifyContent: 'center',
//alignItems: 'center',
},
welcome:{
fontSize: 40,
alignSelf: 'center',
}
});
module.exports = SensorTest;