exbleConnect.vue 9.2 KB

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