bluetooth.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <template>
  2. <view :class="$isEmpty(scanList)?'empty-wrap':''">
  3. <view class="container" v-if="scanList.length>0">
  4. <scroll-view scroll-y :style="'height:' + list_height + 'rpx'">
  5. <block v-for="(item, index) in scanList" :key="index">
  6. <view class="list-item">
  7. <view style="display:flex;flex-direction:column;width:80%">
  8. <text style="font-size:medium;word-break:break-all">设备名称: {{item.name}}</text>
  9. <text style="font-size:x-small;color:gray;word-break:break-all">设备ID: {{item.deviceId}}</text>
  10. <text style="font-size:x-small;color:gray;word-break:break-all">信号强度RSSI: {{item.RSSI}}</text>
  11. </view>
  12. <view class="openBtn" :data-deviceId="item.deviceId" :data-wxapp_ekey="item.wxapp_ekey" @tap="openDoor">开门</view>
  13. </view>
  14. </block>
  15. </scroll-view>
  16. </view>
  17. <view class="default" v-if="scanList==null || scanList.length==0">
  18. <image src="/static/common/empty.png" mode="heightFix"></image>
  19. <view>
  20. <text>没有扫描到设备</text>
  21. </view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. //西墨sdk对象
  27. var tmsdk = require("assets/js/DoormasterSDK-V1.4.js");
  28. const app = getApp();
  29. export default {
  30. data() {
  31. return {
  32. scanList: null
  33. };
  34. },
  35. components: {},
  36. props: {},
  37. /**
  38. * 生命周期函数--监听页面加载
  39. */
  40. onLoad: function (options) {
  41. //监听蓝牙适配器状态变化
  42. uni.onBluetoothAdapterStateChange(function (res) {//console.log(`----adapterState changed, now is`, res)
  43. }); //扫描附近设备
  44. this.scanDevices();
  45. },
  46. /**
  47. * 页面相关事件处理函数--监听用户下拉动作
  48. */
  49. onPullDownRefresh: function () {
  50. uni.stopPullDownRefresh(); //注意!下拉刷新,Android系统设备应执行一次停止蓝牙发现接口
  51. if (app.globalData.platform == 'android') {
  52. uni.stopBluetoothDevicesDiscovery({
  53. success: function (res) {// console.log('----stopBluetoothDevicesDiscovery', res)
  54. }
  55. });
  56. } //重新扫描蓝牙设备
  57. this.scanDevices();
  58. },
  59. onUnload: function () {
  60. uni.stopBluetoothDevicesDiscovery({
  61. success: function (res) {// console.log('---stopBluetoothDevicesDiscovery--success',res);
  62. }
  63. });
  64. uni.closeBluetoothAdapter({
  65. success: function (res) {// console.log('---closeBluetoothAdapter--success', res);
  66. }
  67. });
  68. },
  69. methods: {
  70. //扫描蓝牙设备
  71. scanDevices: function () {
  72. uni.showLoading({
  73. title: '扫描中...',
  74. mask: true
  75. });
  76. var that = this;
  77. tmsdk.scanDevices(function (res) {
  78. if (!res.isBluetoothAvailable) {
  79. uni.hideLoading();
  80. uni.showModal({
  81. content: '请打开蓝牙',
  82. showCancel: false,
  83. success: function (res) {
  84. if (res.confirm) {
  85. uni.navigateBack({
  86. delta: 1 // 返回上一级页面。
  87. }); //跳转到首页
  88. // wx.redirectTo({
  89. // url: '/pages/index/index'
  90. // })
  91. }
  92. }
  93. });
  94. return;
  95. } //筛选后的西墨设备列表
  96. var scanList = res.scanList; //未获取的设备
  97. if (scanList.length == 0) {
  98. uni.hideLoading();
  99. uni.showModal({
  100. content: '未获取到设备信息,请靠近设备再试',
  101. showCancel: false,
  102. success: function (res) {
  103. if (res.confirm) {
  104. uni.navigateBack({
  105. delta: 1 // 返回上一级页面。
  106. }); //跳转到首页
  107. // wx.redirectTo({
  108. // url: '/pages/index/index'
  109. // })
  110. }
  111. }
  112. });
  113. return;
  114. } else {
  115. uni.hideLoading(); //过滤设备-z
  116. let scanList_new = new Array();
  117. let device_list = app.globalData.device_list;
  118. for (let i = 0; i < scanList.length; i++) {
  119. for (let x = 0; x < device_list.length; x++) {
  120. if (scanList[i].name == device_list[x].dev_sn) {
  121. scanList[i]['name'] = device_list[x].dev_name; //用户私钥
  122. scanList[i]['wxapp_ekey'] = device_list[x].wxapp_ekey;
  123. scanList_new.push(scanList[i]);
  124. }
  125. }
  126. }
  127. if (scanList_new.length == 0) {
  128. uni.hideLoading();
  129. uni.showModal({
  130. content: '未获取到授权设备',
  131. showCancel: false,
  132. success: function (res) {
  133. if (res.confirm) {
  134. uni.navigateBack({
  135. delta: 1 // 返回上一级页面。
  136. }); //跳转到首页
  137. // wx.redirectTo({
  138. // url: '/pages/index/index'
  139. // })
  140. }
  141. }
  142. });
  143. return;
  144. }
  145. that.setData({
  146. scanList: scanList_new
  147. });
  148. }
  149. });
  150. },
  151. // 执行开门操作
  152. openDoor: function (event) {
  153. uni.showLoading({
  154. title: '开门中...',
  155. mask: true
  156. });
  157. var deviceId = event.currentTarget.dataset.deviceid;
  158. var wxapp_ekey = event.currentTarget.dataset.wxapp_ekey;
  159. console.info(deviceId + "=" + wxapp_ekey);
  160. tmsdk.openDoor(deviceId, wxapp_ekey, function (res) {
  161. uni.hideLoading();
  162. console.log("----res.errCode:", res.errCode); // 暂时忽略10008错误码,这是其余所有系统上报的异常
  163. if (res.errCode === 0 || res.errCode === 10008) {
  164. uni.showToast({
  165. title: '开门成功',
  166. icon: "success"
  167. });
  168. } else {
  169. uni.showToast({
  170. title: '开门失败,' + '错误码:' + res.errCode
  171. });
  172. }
  173. });
  174. }
  175. }
  176. };
  177. </script>
  178. <style lang="scss">
  179. .container {
  180. align-items: center;
  181. }
  182. .list-item {
  183. display: flex;
  184. flex-direction: row;
  185. justify-content: space-between;
  186. align-items: center;
  187. border-radius: 10rpx;
  188. padding: 30rpx;
  189. box-sizing: border-box;
  190. background: #fff;
  191. margin: 30rpx;
  192. position: relative;
  193. line-height:40rpx;
  194. }
  195. .list-item:last-child {
  196. border-style: none;
  197. }
  198. .button {
  199. position: fixed;
  200. width: 690rpx;
  201. bottom: 30rpx;
  202. }
  203. .openBtn {
  204. float: right;
  205. }
  206. .openBtn {
  207. position: absolute;
  208. right: 20rpx;
  209. top: 50%;
  210. transform: translate(0, -50%);
  211. border: 1px solid $base-btn-color;
  212. height: 40rpx;
  213. line-height: 40rpx;
  214. padding: 0 20rpx;
  215. border-radius: 6rpx;
  216. color: #fff;
  217. background: $base-btn-color;
  218. }
  219. .default {
  220. text-align: center;
  221. position: fixed;
  222. left: 50%;
  223. top: 40%;
  224. transform: translate(-50%, -50%);
  225. }
  226. .default text{
  227. color: #AAAAAA;
  228. }
  229. .default image {
  230. height: 160rpx;
  231. display: inline-block;
  232. }
  233. .empty-wrap{
  234. background-color: #FFFFFF;
  235. min-height: 100vh;
  236. }
  237. </style>