bleConnect.vue 9.2 KB

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