bleConnect.vue 9.3 KB

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