ibeacon.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-padding-wrap uni-common-mt">
  5. <view class="uni-btn-v">
  6. <button type="primary" :disabled="isOpen" @click="openBluetoothAdapter">打开蓝牙模块</button>
  7. <button type="primary" :disabled="!isOpen" @click="closeBluetoothAdapter">关闭蓝牙模块</button>
  8. <button type="primary" :disabled="!isOpen || isStarted" :loading="isStarted" @click="startBeaconDiscovery">开始搜索附近的iBeacon设备</button>
  9. <button type="primary" :disabled="!isStarted" @click="stopBeaconDiscovery">停止搜索附近的iBeacon设备</button>
  10. </view>
  11. </view>
  12. <scroll-view class="uni-scroll_box">
  13. <view class="uni-title" v-if="isStarted || deviceList.length > 0">已经发现 {{ deviceList.length }} 台设备:</view>
  14. <view class="uni-list-box" v-for="(item, index) in deviceList" :key="item.uuid">
  15. <view>
  16. <view class="uni-list_name">UUID: {{ item.uuid }}</view>
  17. <view class="uni-list_item">major: {{ item.major }}</view>
  18. <view class="uni-list_item">minor: {{ item.minor }}</view>
  19. <view class="uni-list_item">rssi: {{ item.rssi }} dBm</view>
  20. <view class="uni-list_item">accuracy: {{ item.accuracy }}</view>
  21. <view class="uni-list_item">heading: {{ item.heading }}</view>
  22. </view>
  23. </view>
  24. </scroll-view>
  25. </view>
  26. </template>
  27. <script>
  28. const DEVICE_UUID_WEICHAT = 'FDA50693-A4E2-4FB1-AFCF-C6EB07647825';
  29. export default {
  30. data() {
  31. return {
  32. title: 'iBeacon',
  33. isOpen: false,
  34. isStarted: false,
  35. deviceList: [],
  36. isPageOpened: false
  37. };
  38. },
  39. onLoad() {
  40. this.onBeaconUpdate();
  41. },
  42. onShow() {
  43. this.isPageOpened = true;
  44. },
  45. methods: {
  46. maskclose() {
  47. this.maskShow = false;
  48. },
  49. openBluetoothAdapter() {
  50. uni.openBluetoothAdapter({
  51. success: (res) => {
  52. console.log('初始化蓝牙成功:' + res.errMsg);
  53. console.log(res);
  54. this.isOpen = true;
  55. this.deviceList = [];
  56. },
  57. fail: (err) => {
  58. console.log('初始化蓝牙失败,错误码:' + (err.errCode || err.errMsg));
  59. if (err.errCode !== 0) {
  60. initTypes(err.errCode, err.errMsg);
  61. }
  62. }
  63. });
  64. },
  65. closeBluetoothAdapter(OBJECT) {
  66. this.stopBeaconDiscovery();
  67. wx.closeBluetoothAdapter({
  68. success: (res) => {
  69. this.isOpen = false;
  70. console.log('断开蓝牙模块成功');
  71. }
  72. });
  73. },
  74. onBeaconUpdate() {
  75. uni.onBeaconUpdate(res => {
  76. if (!this.isPageOpened || !this.isOpen || !this.isStarted) {
  77. return;
  78. }
  79. console.log(res);
  80. // if (res.code !== 0) {
  81. // return;
  82. // }
  83. if (res.beacons && res.beacons.length > 0) {
  84. this.getBeacons();
  85. } else if (!res.connected) {
  86. this.deviceList = [];
  87. }
  88. });
  89. },
  90. startBeaconDiscovery() {
  91. uni.startBeaconDiscovery({
  92. uuids: this.getUUIDList(),
  93. success: (res) => {
  94. this.isStarted = true;
  95. console.log(res);
  96. },
  97. fail: (err) => {
  98. console.error(err);
  99. }
  100. });
  101. },
  102. stopBeaconDiscovery(types) {
  103. if(this.isStarted) {
  104. uni.stopBeaconDiscovery({
  105. success: (res) => {
  106. this.isStarted = false;
  107. },
  108. fail: (err) => {
  109. console.error(err);
  110. }
  111. });
  112. }
  113. },
  114. getBeacons() {
  115. uni.getBeacons({
  116. success: (res) => {
  117. if (res.beacons && res.beacons.length > 0) {
  118. console.log(res.beacons);
  119. this.deviceList = res.beacons;
  120. }
  121. },
  122. fail: (err) => {
  123. console.log('获取设备服务失败,错误码:' + err.errCode);
  124. if (errCode.errCode !== 0) {
  125. initTypes(errCode.errCode);
  126. }
  127. }
  128. });
  129. },
  130. getUUIDList() {
  131. // #ifdef APP-PLUS
  132. const sysInfo = uni.getSystemInfoSync();
  133. if (!sysInfo) {
  134. return [];
  135. }
  136. let isIOS = sysInfo.platform ? (sysInfo.platform.toLowerCase() === "ios") : false;
  137. if (isIOS) {
  138. return [DEVICE_UUID_WEICHAT];
  139. }
  140. return [];
  141. // #endif
  142. // #ifndef APP-PLUS
  143. return [DEVICE_UUID_WEICHAT];
  144. // #endif
  145. }
  146. },
  147. onUnload() {
  148. this.isPageOpened = false;
  149. }
  150. };
  151. /**
  152. * 判断初始化蓝牙状态
  153. */
  154. function initTypes(code, errMsg) {
  155. switch (code) {
  156. case 10000:
  157. toast('未初始化蓝牙适配器');
  158. break;
  159. case 10001:
  160. toast('未检测到蓝牙,请打开蓝牙重试!');
  161. break;
  162. case 10002:
  163. toast('没有找到指定设备');
  164. break;
  165. case 10003:
  166. toast('连接失败');
  167. break;
  168. case 10004:
  169. toast('没有找到指定服务');
  170. break;
  171. case 10005:
  172. toast('没有找到指定特征值');
  173. break;
  174. case 10006:
  175. toast('当前连接已断开');
  176. break;
  177. case 10007:
  178. toast('当前特征值不支持此操作');
  179. break;
  180. case 10008:
  181. toast('其余所有系统上报的异常');
  182. break;
  183. case 10009:
  184. toast('Android 系统特有,系统版本低于 4.3 不支持 BLE');
  185. break;
  186. default:
  187. toast(errMsg);
  188. }
  189. }
  190. /**
  191. * 弹出框封装
  192. */
  193. function toast(content, showCancel = false) {
  194. uni.showModal({
  195. title: '提示',
  196. content,
  197. showCancel
  198. });
  199. }
  200. </script>
  201. <style>
  202. .uni-title {
  203. /* width: 100%; */
  204. /* height: 80rpx; */
  205. text-align: center;
  206. }
  207. .uni-mask {
  208. position: fixed;
  209. top: 0;
  210. left: 0;
  211. bottom: 0;
  212. display: flex;
  213. align-items: center;
  214. width: 100%;
  215. background: rgba(0, 0, 0, 0.6);
  216. padding: 0 30rpx;
  217. box-sizing: border-box;
  218. }
  219. .uni-scroll_box {
  220. height: 70%;
  221. background: #fff;
  222. border-radius: 20rpx;
  223. }
  224. .uni-list-box {
  225. margin: 0 20rpx;
  226. padding: 15rpx 0;
  227. border-bottom: 1px #f5f5f5 solid;
  228. box-sizing: border-box;
  229. }
  230. .uni-list:last-child {
  231. border: none;
  232. }
  233. .uni-list_name {
  234. font-size: 30rpx;
  235. color: #333;
  236. }
  237. .uni-list_item {
  238. font-size: 24rpx;
  239. color: #555;
  240. line-height: 1.5;
  241. }
  242. .uni-success_box {
  243. position: absolute;
  244. left: 0;
  245. bottom: 0;
  246. min-height: 100rpx;
  247. width: 100%;
  248. background: #fff;
  249. box-sizing: border-box;
  250. border-top: 1px #eee solid;
  251. }
  252. .uni-success_sub {
  253. /* width: 100%%; */
  254. height: 100rpx;
  255. display: flex;
  256. justify-content: space-between;
  257. align-items: center;
  258. padding: 0 30rpx;
  259. }
  260. .uni-close_button {
  261. padding: 0 20rpx;
  262. height: 60rpx;
  263. line-height: 60rpx;
  264. background: #ce3c39;
  265. color: #ffffff;
  266. border-radius: 10rpx;
  267. }
  268. .uni-success_content {
  269. height: 600rpx;
  270. margin: 30rpx;
  271. margin-top: 0;
  272. border: 1px #eee solid;
  273. padding: 30rpx;
  274. }
  275. .uni-content_list {
  276. padding-bottom: 10rpx;
  277. border-bottom: 1px #f5f5f5 solid;
  278. }
  279. .uni-tips {
  280. text-align: center;
  281. font-size: 24rpx;
  282. color: #666;
  283. }
  284. </style>