print.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <template>
  2. <view class="">
  3. <view class="bluetooth_container">
  4. <view class="title">
  5. 已配对设备
  6. </view>
  7. <view class="matched_list" v-if="matchedList && matchedList.length">
  8. <view class="flex" v-for="(item, index) in matchedList" :key="item.deviceId" @longpress="showModal2(item.deviceId)">
  9. <image class="img" src="/static/print.png" mode=""></image>
  10. <text class="name">{{item.localName ? item.localName : item.name ? item.name : '--'}}</text>
  11. <text class="link linked" @click="showModal" v-if="linkedDeviceId === item.deviceId">已连接</text>
  12. <block v-else>
  13. <image v-if="selectedDeviceId==item.deviceId && isConnectting" class="load_img" src="/static/loadding.gif" mode=""></image>
  14. <text v-else class="link unlink" @click="connectHandler(item)">未连接</text>
  15. </block>
  16. </view>
  17. </view>
  18. <view class="empty_box" v-else>
  19. <u-empty mode="search" text="暂无配对设备,快快添加吧~"></u-empty>
  20. </view>
  21. <view class="block"></view>
  22. <view class="title search_title">
  23. <text>扫描可用设备</text>
  24. <view class="stop" v-if="isSearching" @click="stopSearch">
  25. <text>停止</text>
  26. </view>
  27. <view class="img_box" v-else @click="startSearch">
  28. <image class="img img1" src="/static/refresh.png" mode=""></image>
  29. </view>
  30. <view class="load_box" v-if="isSearching">
  31. <image class="load_img" src="/static/loadding.gif" mode=""></image>
  32. </view>
  33. </view>
  34. <view class="list">
  35. <view class="flex" v-for="(item, index) in deviceList" :key="index">
  36. <image class="img img2" src="/static/print.png" mode=""></image>
  37. <text class="name">{{item.localName ? item.localName : item.name ? item.name : '--'}}</text>
  38. <image v-if="selectedDeviceId==item.deviceId && isConnectting" class="load_img" src="/static/loadding.gif" mode=""></image>
  39. <text v-else class="link" @click="connectHandler(item,index)">连接</text>
  40. </view>
  41. </view>
  42. </view>
  43. <u-modal v-model="modalShow" show-cancel-button content="是否断开连接?" @confirm="closeConnect"></u-modal>
  44. <u-modal v-model="modalShow2" show-cancel-button content="是否忽略此设备?" @confirm="deleteDevice"></u-modal>
  45. </view>
  46. </template>
  47. <script>
  48. import B12s from '@/common/b12s.js';
  49. const b12s = new B12s();
  50. export default {
  51. data() {
  52. return {
  53. // 蓝牙相关;
  54. matchedList: [], //已配对的列表;
  55. deviceList: [], //搜索到的设备列表;
  56. initCode: 0, //蓝牙初始化结果;0:初始化中,1-成功;2-失败;
  57. selectedDeviceId: '', //当前操作的蓝牙设备id;
  58. isConnectting: false, //蓝牙正在连接中;
  59. linkedDeviceId: '', //已连接的蓝牙mac地址;
  60. isSearching: false, //是否正在搜索蓝牙设备;
  61. b12s: null, //蓝牙工具;
  62. }
  63. },
  64. async onLoad(options) {
  65. this.initBluetooth();
  66. },
  67. async onUnload() {
  68. this.stopSearch();
  69. b12s.closeBLE(this.linkedDeviceId);
  70. b12s.closeAdapter();
  71. },
  72. methods: {
  73. // 初始化蓝牙;
  74. async initBluetooth() {
  75. this.getMatchedList();
  76. await b12s.init(this.onConnectChange);
  77. this.autoConnect();
  78. },
  79. // 如果有配对历史,自动连接最后一个;
  80. autoConnect() {
  81. let length = this.matchedList.length
  82. if (length <= 0) return;
  83. let item = this.matchedList[length - 1];
  84. this.connectHandler(item);
  85. },
  86. // 开始搜索蓝牙;
  87. async startSearch() {
  88. await b12s.startSearch(this.getDeviceList);
  89. this.isSearching = true;
  90. },
  91. // 停止搜索
  92. async stopSearch() {
  93. await b12s.stopSearch();
  94. this.isSearching = false;
  95. },
  96. // 获取历史配对列表;
  97. getMatchedList() {
  98. let list = uni.getStorageSync('__bluetooth_list_');
  99. if (list) {
  100. this.matchedList = list;
  101. }
  102. console.log(this.matchedList);
  103. },
  104. // 获取可用设备列表;
  105. getDeviceList(devices) {
  106. for (let i = 0; i < devices.length; i++) {
  107. let name = devices[i].name || devices[i].localName;
  108. if (name) {
  109. let dLIndex = this.deviceList.findIndex(item => item.deviceId === devices[i].deviceId);
  110. let mLIndex = this.matchedList.findIndex(item => item.deviceId === devices[i].deviceId);
  111. if (dLIndex < 0 && mLIndex < 0) {
  112. this.deviceList.push({
  113. name: name,
  114. deviceId: devices[i].deviceId
  115. })
  116. }
  117. }
  118. }
  119. },
  120. // 点击连接;
  121. async connectHandler(item, index = -1) {
  122. this.stopSearch();
  123. await b12s.closeBLE(this.linkedDeviceId)
  124. this.selectedDeviceId = item.deviceId;
  125. this.isConnectting = true;
  126. await b12s.connectBLE(item.deviceId).catch(err => {
  127. this.isConnectting = false;
  128. });
  129. // this.linkedDeviceId = item.deviceId;
  130. this.isConnectting = false;
  131. let indexRes = this.matchedList.findIndex(itm => itm.deviceId === item.deviceId);
  132. if (indexRes < 0) {
  133. this.matchedList.push(item);
  134. index !== -1 && this.deviceList.splice(index, 1);
  135. this.saveStorage()
  136. }
  137. },
  138. // 断开连接
  139. closeConnect() {
  140. b12s.closeBLE(this.linkedDeviceId);
  141. this.modalShow = false;
  142. },
  143. // 监听蓝牙连接状态;
  144. onConnectChange(res) {
  145. if (res.connected) {
  146. // this.$u.toast('蓝牙已连接');
  147. this.linkedDeviceId = res.deviceId;
  148. } else {
  149. // this.$u.toast('蓝牙已断开');
  150. this.linkedDeviceId = '';
  151. }
  152. },
  153. // 删除已缓存设备;
  154. deleteDevice() {
  155. this.matchedList = this.matchedList.filter(item => item.deviceId !== this.selectedDeviceId);
  156. this.saveStorage()
  157. },
  158. // 缓存已配对蓝牙;
  159. saveStorage() {
  160. uni.setStorageSync('__bluetooth_list_', this.matchedList)
  161. }
  162. }
  163. }
  164. </script>
  165. <style lang="scss" scoped>
  166. .bluetooth_container {
  167. padding: 30rpx;
  168. .load_img {
  169. width: 40rpx;
  170. height: 40rpx;
  171. }
  172. .title {
  173. font-size: 34rpx;
  174. font-weight: bold;
  175. .img {
  176. margin-left: 20rpx;
  177. vertical-align: middle;
  178. width: 36rpx;
  179. height: 36rpx;
  180. }
  181. }
  182. .search_title {
  183. display: flex;
  184. justify-content: space-between;
  185. margin-top: 60rpx;
  186. .img_box {
  187. flex: 1;
  188. }
  189. .load_img {
  190. width: 40rpx;
  191. height: 40rpx;
  192. }
  193. .stop {
  194. flex: 1;
  195. margin-left: 30rpx;
  196. text {
  197. font-size: 24rpx;
  198. font-weight: normal;
  199. background-color: #fa3534;
  200. color: #ffffff;
  201. padding: 10rpx 20rpx;
  202. border-radius: 30rpx;
  203. }
  204. }
  205. }
  206. .matched_list {
  207. // padding: 30rpx 0;
  208. .flex {
  209. display: flex;
  210. margin: 30rpx 0;
  211. justify-content: space-between;
  212. align-items: center;
  213. .img {
  214. width: 40rpx;
  215. height: 40rpx;
  216. }
  217. .name {
  218. flex: 1;
  219. margin-left: 40rpx;
  220. }
  221. .link {
  222. padding: 10rpx 40rpx;
  223. background-color: #F2F2F2;
  224. border-radius: 40rpx;
  225. font-size: 28rpx;
  226. }
  227. .unlink {
  228. color: #606266;
  229. }
  230. .linked {
  231. color: #19be6b;
  232. }
  233. }
  234. }
  235. .empty_box {
  236. padding: 90rpx 0;
  237. }
  238. .list {
  239. .flex {
  240. display: flex;
  241. margin: 30rpx 0;
  242. justify-content: space-between;
  243. align-items: center;
  244. .img {
  245. width: 40rpx;
  246. height: 40rpx;
  247. }
  248. .name {
  249. flex: 1;
  250. margin-left: 40rpx;
  251. }
  252. .link {
  253. padding: 10rpx 40rpx;
  254. background-color: #F2F2F2;
  255. border-radius: 40rpx;
  256. color: #2979ff;
  257. font-size: 28rpx;
  258. }
  259. }
  260. }
  261. .btns {
  262. display: flex;
  263. justify-content: space-around;
  264. margin-top: 200rpx;
  265. .btn {
  266. margin: 0;
  267. width: 40%;
  268. }
  269. }
  270. }
  271. </style>