jjbleConnect.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <template>
  2. <view class="content">
  3. <button class="btn" type="primary" :loading="isSearching" @tap="startSearch">搜索打印机</button>
  4. <button class="btn" type="warn" @tap="stopSearch">停止搜索</button>
  5. <view v-for="(item) in list" :data-title="item.deviceId" :data-name="item.name" :data-advertisData="item.advertisServiceUUIDs"
  6. :key="item.deviceId" @tap="bindViewTap">
  7. <view class="item">
  8. <view class="deviceId block">{{item.deviceId}}</view>
  9. <view class="name block">{{item.name}}</view>
  10. </view>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. import {
  16. mapState
  17. } from 'vuex';
  18. export default {
  19. data() {
  20. return {
  21. isSearching: false, //是否正在搜索中
  22. list: [],
  23. services: [],
  24. serviceId: 0,
  25. writeCharacter: false,
  26. readCharacter: false,
  27. notifyCharacter: false
  28. };
  29. },
  30. computed: mapState(['sysinfo', 'Bluetooth']),
  31. onLoad() {
  32. // console.log(this.Bluetooth)
  33. },
  34. onUnload() {
  35. //停止搜索蓝牙设备
  36. if (this.isSearching) {
  37. uni.stopBluetoothDevicesDiscovery();
  38. }
  39. },
  40. methods: {
  41. //错误码提示
  42. errorCodeTip(code) {
  43. if (code == 0) {
  44. //正常
  45. } else if (code == 10000) {
  46. uni.showToast({
  47. title: '未初始化蓝牙适配器',
  48. icon: 'none'
  49. })
  50. } else if (code == 10001) {
  51. uni.showToast({
  52. title: '当前蓝牙适配器不可用',
  53. icon: 'none'
  54. })
  55. } else if (code == 10002) {
  56. uni.showToast({
  57. title: '没有找到指定设备',
  58. icon: 'none'
  59. })
  60. } else if (code == 10003) {
  61. uni.showToast({
  62. title: '连接失败',
  63. icon: 'none'
  64. })
  65. } else if (code == 10004) {
  66. uni.showToast({
  67. title: '没有找到指定服务',
  68. icon: 'none'
  69. })
  70. } else if (code == 10005) {
  71. uni.showToast({
  72. title: '没有找到指定特征值',
  73. icon: 'none'
  74. })
  75. } else if (code == 10006) {
  76. uni.showToast({
  77. title: '当前连接已断开',
  78. icon: 'none'
  79. })
  80. } else if (code == 10007) {
  81. uni.showToast({
  82. title: '当前特征值不支持此操作',
  83. icon: 'none'
  84. })
  85. } else if (code == 10008) {
  86. uni.showToast({
  87. title: '其余所有系统上报的异常',
  88. icon: 'none'
  89. })
  90. } else if (code == 10009) {
  91. uni.showToast({
  92. title: 'Android 系统特有,系统版本低于 4.3 不支持 BLE',
  93. icon: 'none'
  94. })
  95. }
  96. },
  97. //开始搜索蓝牙
  98. startSearch() {
  99. let that = this
  100. uni.openBluetoothAdapter({
  101. success(res) {
  102. uni.getBluetoothAdapterState({
  103. success(res2) {
  104. console.log('getBluetoothAdapterState:', res2)
  105. if (res2.available) {
  106. that.isSearching = true;
  107. if (res2.discovering) {
  108. uni.showToast({
  109. title: '正在搜索附近打印机设备',
  110. icon: "none"
  111. })
  112. return;
  113. }
  114. //获取蓝牙设备信息
  115. that.getBluetoothDevices()
  116. // that.checkPemission()
  117. } else {
  118. uni.showModal({
  119. title: '提示',
  120. content: '本机蓝牙不可用',
  121. })
  122. }
  123. }
  124. });
  125. },
  126. fail() {
  127. uni.showModal({
  128. title: '提示',
  129. content: '蓝牙初始化失败,请打开蓝牙',
  130. })
  131. }
  132. })
  133. },
  134. stopSearch() {
  135. uni.stopBluetoothDevicesDiscovery({
  136. success: (res) => {
  137. this.isSearching = false;
  138. console.log('stop:', res)
  139. },
  140. fail: (e) => {
  141. console.log('stop:', e)
  142. this.errorCodeTip(e.errCode);
  143. }
  144. })
  145. },
  146. //校验权限
  147. checkPemission() {
  148. let that = this
  149. let {
  150. BLEInformation
  151. } = that.Bluetooth;
  152. let platform = BLEInformation.platform;
  153. that.getBluetoothDevices();
  154. },
  155. //获取蓝牙设备信息
  156. getBluetoothDevices() {
  157. let that = this
  158. that.list = [];
  159. uni.startBluetoothDevicesDiscovery({
  160. success(res) {
  161. // console.log(res)
  162. //蓝牙设备监听 uni.onBluetoothDeviceFound
  163. plus.bluetooth.onBluetoothDeviceFound((result) => {
  164. console.log('onBluetoothDeviceFound:', result)
  165. let arr = that.list;
  166. let devices = [];
  167. let list = result.devices;
  168. for (let i = 0; i < list.length; ++i) {
  169. if (list[i].name && list[i].name != "未知设备") {
  170. let arrNew = arr.filter((item) => {
  171. return item.deviceId == list[i].deviceId;
  172. });
  173. // console.log('arrNew:',arrNew.length)
  174. if (arrNew.length == 0) {
  175. devices.push(list[i]);
  176. }
  177. }
  178. }
  179. that.list = arr.concat(devices);
  180. });
  181. that.time = setTimeout(() => {
  182. // uni.getBluetoothDevices
  183. plus.bluetooth.getBluetoothDevices({
  184. success(res2) {
  185. let devices = [];
  186. let list = res2.devices;
  187. for (let i = 0; i < list.length; ++i) {
  188. if (list[i].name && list[i].name != "未知设备") {
  189. devices.push(list[i]);
  190. }
  191. }
  192. that.list = devices;
  193. console.log('getBluetoothDevices:',res2);
  194. },
  195. })
  196. clearTimeout(that.time);
  197. }, 3000);
  198. }
  199. });
  200. },
  201. //绑定蓝牙
  202. bindViewTap(e) {
  203. let that = this;
  204. let {
  205. title
  206. } = e.currentTarget.dataset;
  207. let {
  208. BLEInformation
  209. } = that.Bluetooth;
  210. this.stopSearch();
  211. that.serviceId = 0;
  212. that.writeCharacter = false;
  213. that.readCharacter = false;
  214. that.notifyCharacter = false;
  215. uni.showLoading({
  216. title: '正在连接',
  217. })
  218. console.log('deviceId:', title)
  219. // uni.createBLEConnection
  220. plus.bluetooth.createBLEConnection({
  221. deviceId: title,
  222. success(res) {
  223. console.log('createBLEConnection success:', res)
  224. BLEInformation.deviceId = title;
  225. that.$store.commit('BLEInformationSet', BLEInformation);
  226. uni.hideLoading()
  227. that.getSeviceId()
  228. },
  229. fail(e) {
  230. that.errorCodeTip(e.errCode);
  231. uni.hideLoading()
  232. }
  233. })
  234. },
  235. //获取蓝牙设备所有服务(service)。
  236. getSeviceId() {
  237. let that = this;
  238. let {
  239. BLEInformation
  240. } = that.Bluetooth;
  241. console.log('BLEInformation.deviceId:',BLEInformation.deviceId)
  242. // uni.getBLEDeviceServices
  243. let t=setTimeout(()=>{
  244. plus.bluetooth.getBLEDeviceServices({
  245. deviceId: BLEInformation.deviceId,
  246. success(res) {
  247. console.log('getBLEDeviceServices success:',res)
  248. that.services = res.services;
  249. that.getCharacteristics()
  250. },
  251. fail: function(e) {
  252. that.errorCodeTip(e.code);
  253. console.log('getBLEDeviceServices fail:',e)
  254. }
  255. })
  256. clearTimeout(t);
  257. },1500)
  258. },
  259. getCharacteristics() {
  260. var that = this
  261. let {
  262. services: list,
  263. serviceId: num,
  264. writeCharacter: write,
  265. readCharacter: read,
  266. notifyCharacter: notify
  267. } = that;
  268. let {
  269. BLEInformation
  270. } = that.Bluetooth;
  271. // uni.getBLEDeviceCharacteristics
  272. plus.bluetooth.getBLEDeviceCharacteristics({
  273. deviceId: BLEInformation.deviceId,
  274. serviceId: list[num].uuid,
  275. success(res) {
  276. // console.log(res)
  277. for (var i = 0; i < res.characteristics.length; ++i) {
  278. var properties = res.characteristics[i].properties
  279. var item = res.characteristics[i].uuid
  280. if (!notify) {
  281. if (properties.notify) {
  282. BLEInformation.notifyCharaterId = item;
  283. BLEInformation.notifyServiceId = list[num].uuid;
  284. that.$store.commit('BLEInformationSet', BLEInformation);
  285. notify = true
  286. }
  287. }
  288. if (!write) {
  289. if (properties.write) {
  290. BLEInformation.writeCharaterId = item;
  291. BLEInformation.writeServiceId = list[num].uuid;
  292. that.$store.commit('BLEInformationSet', BLEInformation);
  293. write = true
  294. }
  295. }
  296. if (!read) {
  297. if (properties.read) {
  298. BLEInformation.readCharaterId = item;
  299. BLEInformation.readServiceId = list[num].uuid;
  300. that.$store.commit('BLEInformationSet', BLEInformation);
  301. read = true
  302. }
  303. }
  304. }
  305. if (!write || !notify || !read) {
  306. num++
  307. that.writeCharacter = write;
  308. that.readCharacter = read;
  309. that.notifyCharacter = notify;
  310. that.serviceId = num;
  311. if (num == list.length) {
  312. uni.showModal({
  313. title: '提示',
  314. content: '找不到该读写的特征值',
  315. })
  316. } else {
  317. that.getCharacteristics()
  318. }
  319. } else {
  320. that.openControl()
  321. }
  322. },
  323. fail: function(e) {
  324. console.log("getBLEDeviceCharacteristics fail:",e);
  325. that.errorCodeTip(e.errCode);
  326. }
  327. })
  328. },
  329. openControl: function() {
  330. uni.navigateTo({
  331. url: '/pages/erpbusiness/jjsendCommand'
  332. })
  333. },
  334. }
  335. }
  336. </script>
  337. <style lang="less">
  338. .btn {
  339. margin-top: 50rpx;
  340. height: 40px;
  341. width: 600rpx;
  342. line-height: 40px;
  343. background: #22C572;
  344. }
  345. .item {
  346. display: block;
  347. font-family: Arial, Helvetica, sans-serif;
  348. font-size: 14px;
  349. margin: 0 30px;
  350. margin-top: 10px;
  351. background-color: #FFA850;
  352. border-radius: 5px;
  353. border-bottom: 2px solid #68BAEA;
  354. }
  355. .block {
  356. display: block;
  357. color: #ffffff;
  358. padding: 5px;
  359. }
  360. </style>