proximity.vue 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-padding-wrap uni-common-mt">
  5. <view class="uni-hello-text">
  6. 手机顶部听筒处有传感器监听距离手机屏幕的障碍物,覆盖该传感器会触发本事件变化
  7. </view>
  8. <view class="uni-btn-v uni-common-mt">
  9. <button type="primary" @tap="getProximity">获取距离传感器信息</button>
  10. <button type="primary" @tap="watchProximity">监听距离传感器变化</button>
  11. <button type="primary" @tap="watchStop">停止监听</button>
  12. </view>
  13. <view class="uni-textarea uni-common-mt">
  14. <textarea :value="value" />
  15. </view>
  16. </view>
  17. </view>
  18. </template>
  19. <script>
  20. var id = null
  21. var bright = null
  22. export default {
  23. data() {
  24. return {
  25. title: 'proximity',
  26. value: ''
  27. }
  28. },
  29. methods: {
  30. getProximity: function () {
  31. var that = this;
  32. plus.proximity.getCurrentProximity(function (d) {
  33. that.value = "距离为:" + d;
  34. }, function (e) {
  35. that.value = "获取失败:" + e.message;
  36. });
  37. },
  38. watchProximity: function () {
  39. var that = this;
  40. if (id) {
  41. return;
  42. }
  43. bright = plus.screen.getBrightness();
  44. id = plus.proximity.watchProximity(function (d) {
  45. that.value = "距离变化:" + d;
  46. plus.screen.setBrightness((d < 1) ? 0.01 : bright);
  47. }, function (e) {
  48. plus.proximity.clearWatch(id);
  49. id = null;
  50. that.value = "监听失败:" + e.message;
  51. });
  52. },
  53. watchStop: function () {
  54. var that = this;
  55. if (id) {
  56. that.value = "停止监听设备距离传感器信息";
  57. plus.proximity.clearWatch(id);
  58. id = null;
  59. } else {
  60. that.value = "没有监听设备距离传感器";
  61. }
  62. }
  63. }
  64. }
  65. </script>
  66. <style>
  67. </style>