Blame view

src/platforms/app-plus/proximity/proximity.vue 1.79 KB
289f85d9e   Adam   提交
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
  <template>
  	<view>
  		<page-head :title="title"></page-head>
  		<view class="uni-padding-wrap uni-common-mt">
  			<view class="uni-hello-text">
  				手机顶部听筒处有传感器监听距离手机屏幕的障碍物,覆盖该传感器会触发本事件变化
  			</view>
  			<view class="uni-btn-v uni-common-mt">
  				<button type="primary" @tap="getProximity">获取距离传感器信息</button>
  				<button type="primary" @tap="watchProximity">监听距离传感器变化</button>
  				<button type="primary" @tap="watchStop">停止监听</button>
  			</view>
  			<view class="uni-textarea uni-common-mt">
  				<textarea :value="value" />
  			</view>
  		</view>
  	</view>
  </template>
  <script>
  	var id = null
  	var bright = null
  	export default {
  		data() {
  			return {
  				title: 'proximity',
  				value: ''
  			}
  		},
  		methods: {
  			getProximity: function () {
  				var that = this;
  				plus.proximity.getCurrentProximity(function (d) {
  					that.value = "距离为:" + d;
  				}, function (e) {
  					that.value = "获取失败:" + e.message;
  				});
  			},
  			watchProximity: function () {
  				var that = this;
  				if (id) {
  					return;
  				}
  				bright = plus.screen.getBrightness();
  				id = plus.proximity.watchProximity(function (d) {
  					that.value = "距离变化:" + d;
  					plus.screen.setBrightness((d < 1) ? 0.01 : bright);
  				}, function (e) {
  					plus.proximity.clearWatch(id);
  					id = null;
  					that.value = "监听失败:" + e.message;
  				});
  			},
  			watchStop: function () {
  				var that = this;
  				if (id) {
  					that.value = "停止监听设备距离传感器信息";
  					plus.proximity.clearWatch(id);
  					id = null;
  				} else {
  					that.value = "没有监听设备距离传感器";
  				}
  			}
  		}
  	}
  </script>
  
  <style>
  	
  </style>