You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JLBleManagerTool.js 9.8KB

пре 1 месец
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. var deviceId, serviceId, characticId, dev, TAG = "JLBleManagerTool.js",
  2. bleErrorMsg = require("./JLBleErrorMsg.js"),
  3. bleCode = require("./errorCode.js"),
  4. onfire = require("./onfire.js"),
  5. dataTool = require("./dataTool.js"),
  6. successCode = bleCode.bleSuccessCode(),
  7. errorCode = bleCode.bleErrorCode(),
  8. charact_serviceId = serviceId = "FEE7";
  9. export function scan_StartScanBleDevice(e, o) {
  10. console.log(serviceId), initBluetoothAdapte(function (c) {
  11. c.code == bleCode.bleSuccessCode() ? wx.startBluetoothDevicesDiscovery({
  12. services: ["FEE7"],
  13. success: function (c) {
  14. wx.onBluetoothDeviceFound(function (c) {
  15. for (var r = [], s = c.devices, t = 0; t < s.length; t++) {
  16. var l = s[t];
  17. if (null != e) {
  18. if (-1 != l.name.indexOf(e)) {
  19. var i = {
  20. device_no: l.deviceId,
  21. device_name: l.name
  22. };
  23. r.push(i)
  24. }
  25. } else if (l.name.length > 0) {
  26. i = {
  27. device_no: l.deviceId,
  28. device_name: l.name
  29. };
  30. r.push(i)
  31. }
  32. }
  33. r.length > 0 && o.call(this, {
  34. code: successCode,
  35. err_msg: "搜索设备成功",
  36. data: r,
  37. msg: ""
  38. })
  39. })
  40. },
  41. fail: function (e) {
  42. var c = "";
  43. e && (c = bleErrorMsg.bluetoothErrMsg(e.errCode)), o.call(this, {
  44. code: bleCode.bleErrorCode(),
  45. msg: c,
  46. data: null
  47. })
  48. }
  49. }) : o.call(this, c)
  50. })
  51. }
  52. export function scan_stopScanBleDevice(e) {
  53. wx.stopBluetoothDevicesDiscovery({
  54. success: function (o) {
  55. e.call(this, {
  56. code: successCode,
  57. msg: "停止扫描成功",
  58. data: null
  59. })
  60. },
  61. fail: function (o) {
  62. var c = "";
  63. o && (c = bleErrorMsg.bluetoothErrMsg(o.errCode)), e.call(this, {
  64. code: errorCode,
  65. msg: c,
  66. data: null
  67. })
  68. }
  69. })
  70. }
  71. function initBluetoothAdapte(e) {
  72. wx.openBluetoothAdapter({
  73. success: function (o) {
  74. wx.getBluetoothAdapterState({
  75. success: function (o) {
  76. 1 == o.available && 0 == o.discovering && e.call(this, {
  77. code: successCode,
  78. msg: "初始化蓝牙模块成功",
  79. data: null
  80. }), 0 == o.available && e.call(this, {
  81. code: errorCode,
  82. msg: "蓝牙适配器不可用",
  83. data: null
  84. }), 1 == o.discovering && e.call(this, {
  85. code: errorCode,
  86. msg: "正在搜索设备",
  87. data: null
  88. })
  89. },
  90. fail: function (o) {
  91. var c = "";
  92. o && (c = bleErrorMsg.bluetoothErrMsg(o.errCode)), e.call(this, {
  93. code: errorCode,
  94. msg: c,
  95. data: null
  96. })
  97. }
  98. })
  99. },
  100. fail: function (o) {
  101. var c = "";
  102. o && (c = bleErrorMsg.bluetoothErrMsg(o.errCode)), e.call(this, {
  103. code: errorCode,
  104. msg: c,
  105. data: null
  106. })
  107. }
  108. })
  109. }
  110. function closeBluetoothAdapte(e) {
  111. wx.getBluetoothAdapterState({
  112. success: function (o) {
  113. wx.closeBluetoothAdapter({
  114. success: function (o) {
  115. e.call(this, {
  116. code: successCode,
  117. msg: "关闭蓝牙模块成功",
  118. data: null
  119. })
  120. },
  121. fail: function (o) {
  122. e.call(this, {
  123. code: errorCode,
  124. msg: o.errmsg,
  125. data: null
  126. })
  127. }
  128. })
  129. },
  130. fail: function (o) {
  131. var c = "";
  132. o && (c = bleErrorMsg.bluetoothErrMsg(o.errCode)), e.call(this, {
  133. code: errorCode,
  134. msg: c,
  135. data: null
  136. })
  137. }
  138. })
  139. }
  140. export function connectDevice(e, o) {
  141. dev = e, getConnectedDevice(function (e) {
  142. e.code == bleCode.bleSuccessCode() ? wxConnectDevice(function (e) {
  143. o.call(this, e)
  144. }) : o.call(this, e)
  145. })
  146. }
  147. export function disConnectDevice(e) {
  148. wxDisConnectDevice(function (o) {
  149. e.call(this, o)
  150. })
  151. }
  152. export function onDeviceConnectStateChange(e) {
  153. wx.onBLEConnectionStateChange(function (o) {
  154. e.call(this, o)
  155. })
  156. }
  157. function wxConnectDevice(e) {
  158. console.log("dev:" + JSON.stringify(dev)), wx.createBLEConnection({
  159. deviceId: dev.device_no,
  160. success: function (o) {
  161. e.call(this, {
  162. code: bleCode.bleSuccessCode(),
  163. err_msg: "蓝牙物理连接成功",
  164. data: dev,
  165. msg: ""
  166. })
  167. },
  168. fail: function (o) {
  169. var c = "";
  170. o && (c = bleErrorMsg.bluetoothErrMsg(o.errCode)), e.call(this, {
  171. code: bleCode.bleErrorCode(),
  172. err_msg: c,
  173. data: null,
  174. msg: ""
  175. })
  176. }
  177. })
  178. }
  179. function getConnectedDevice(e) {
  180. wx.getConnectedBluetoothDevices({
  181. services: ["fee7"],
  182. success: function (o) {
  183. o.devices.length > 0 ? e.call(this, {
  184. code: bleCode.bleErrorCode(),
  185. err_msg: "蓝牙被占用",
  186. data: o.services[0],
  187. msg: ""
  188. }) : e.call(this, {
  189. code: bleCode.bleSuccessCode(),
  190. err_msg: "可连接设备",
  191. data: null,
  192. msg: ""
  193. })
  194. },
  195. fail: function (o) {
  196. var c = "";
  197. o && (c = bleErrorMsg.bluetoothErrMsg(o.errCode)), e.call(this, {
  198. code: bleCode.bleErrorCode(),
  199. err_msg: c,
  200. data: null,
  201. msg: ""
  202. })
  203. }
  204. })
  205. }
  206. function wxDisConnectDevice(e) {
  207. null != dev ? wx.closeBLEConnection({
  208. deviceId: dev.device_no,
  209. success: function (o) {
  210. wx.closeBluetoothAdapter({
  211. success: function (o) {
  212. e.call(this, {
  213. code: bleCode.bleSuccessCode(),
  214. err_msg: "设备断开连接成功",
  215. data: null,
  216. msg: ""
  217. })
  218. },
  219. fail: function (o) {
  220. e.call(this, {
  221. code: bleCode.bleErrorCode(),
  222. err_msg: "设备断开连接成功," + o.errmsg,
  223. data: null,
  224. msg: ""
  225. })
  226. }
  227. })
  228. },
  229. fail: function (o) {
  230. var c = "";
  231. o && (c = bleErrorMsg.bluetoothErrMsg(o.errCode)), e.call(this, {
  232. code: bleCode.bleErrorCode(),
  233. err_msg: c,
  234. data: null,
  235. msg: ""
  236. })
  237. }
  238. }) : e.call(this, {
  239. code: bleCode.bleSuccessCode(),
  240. err_msg: "暂无设备连接",
  241. data: null,
  242. msg: ""
  243. })
  244. }
  245. export function findDeviceServices(e, o, c) {
  246. getDeviceService(e, function (r) {
  247. getDeviceCharacteristics(e, r.data, function (e) {
  248. o.call(this, e)
  249. }, function (e) {
  250. e.code == bleCode.bleSuccessCode() ? wx.onBLECharacteristicValueChange(function (e) {
  251. c.call(this, {
  252. code: bleCode.bleSuccessCode(),
  253. err_msg: "特征值发生变化",
  254. data: {
  255. value: dataTool.bufferTohex(e.value).toLocaleUpperCase()
  256. },
  257. msg: ""
  258. })
  259. }) : c.call(this, e)
  260. })
  261. })
  262. }
  263. function getDeviceService(e, o) {
  264. wx.getBLEDeviceServices({
  265. deviceId: e.device_no,
  266. success: function (e) {
  267. for (var c = e.services, r = 0; r < c.length; r++) "0000FEE7" == e.services[r].uuid.substr(0, 8) && o.call(this, {
  268. code: bleCode.bleSuccessCode(),
  269. err_msg: "寻找服务成功",
  270. data: e.services[r].uuid,
  271. msg: ""
  272. })
  273. }
  274. })
  275. }
  276. function getDeviceCharacteristics(e, o, c, r) {
  277. wx.getBLEDeviceCharacteristics({
  278. deviceId: e.device_no,
  279. serviceId: o,
  280. success: function (s) {
  281. for (var t = s.characteristics, l = 0; l < t.length; l++) {
  282. var i = t[l];
  283. 1 == i.properties.read && wx.readBLECharacteristicValue({
  284. deviceId: e.device_no,
  285. serviceId: charact_serviceId,
  286. characteristicId: i.uuid,
  287. success: function (e) {}
  288. }), 1 == i.properties.write && c.call(this, {
  289. code: bleCode.bleSuccessCode(),
  290. err_msg: null,
  291. data: {
  292. id: {
  293. devId: e.device_no,
  294. serId: o,
  295. charactId: i.uuid
  296. }
  297. },
  298. msg: ""
  299. }), 1 != i.properties.notify && 1 != i.properties.indicate || wx.notifyBLECharacteristicValueChange({
  300. deviceId: e.device_no,
  301. serviceId: o,
  302. characteristicId: i.uuid,
  303. state: !0,
  304. success: function (e) {
  305. r.call(this, {
  306. code: bleCode.bleSuccessCode(),
  307. err_msg: "开启设备监听成功",
  308. data: e,
  309. msg: ""
  310. })
  311. },
  312. fail: function (e) {
  313. var o = "";
  314. e && (o = bleErrorMsg.bluetoothErrMsg(e.errCode)), r.call(this, {
  315. code: bleCode.bleErrorCode(),
  316. err_msg: o,
  317. data: null,
  318. msg: ""
  319. })
  320. }
  321. })
  322. }
  323. }
  324. })
  325. }
  326. export function setParams(e) {
  327. deviceId = e.devId, serviceId = e.serId, characticId = e.charactId, console.log("params id :" + JSON.stringify(e))
  328. }
  329. export function sendMessageToDevice(e, o) {
  330. console.log(TAG + " sendMessageToDevice() cmd:" + e[0]);
  331. var c = dataTool.strToBuffer(e[0]),
  332. r = 0;
  333. console.log(TAG + " 发送当前时间 :" + Date.parse(new Date)), wx.writeBLECharacteristicValue({
  334. deviceId: deviceId,
  335. serviceId: serviceId,
  336. characteristicId: characticId,
  337. value: c,
  338. success: function (c) {
  339. console.log(TAG + "wx. 发送成功 :" + Date.parse(new Date)), r < e.length - 1 ? (console.info(TAG + " wx. 已完成发送第====" + r + "===包====【" + e[r] + "】"), r++, onfire.fire("send", r)) : (onfire.clear(), console.info("wx. 所有数据发送完成 共:" + e.length + "包"), o.call(this, {
  340. code: successCode,
  341. err_msg: "数据发送成功",
  342. data: e,
  343. msg: ""
  344. }))
  345. },
  346. fail: function (e) {
  347. console.log(TAG + "wx. 发送失败 :" + Date.parse(new Date)), console.log("writeBLECharacteristicValue fail:" + JSON.stringify(e));
  348. var c = "";
  349. e && (c = bleErrorMsg.bluetoothErrMsg(e.errCode)), o.call(this, {
  350. code: bleCode.bleErrorCode(),
  351. err_msg: c,
  352. data: null,
  353. msg: ""
  354. })
  355. }
  356. }), onfire.on("send", r => {
  357. console.log(TAG + " onfire.on sendMessageToDevice() cmd:" + e[r]);
  358. var s = dataTool.strToBuffer(e[r]);
  359. wx.writeBLECharacteristicValue({
  360. deviceId: deviceId,
  361. serviceId: serviceId,
  362. characteristicId: characticId,
  363. value: s,
  364. success: function (s) {
  365. console.log(TAG + "onfire.发送成功 :" + Date.parse(new Date)), r < e.length - 1 ? (console.info(TAG + " onfire. 已完成发送第====" + r + "===包====【" + c + "】"), r++, onfire.fire("send", r)) : (onfire.clear(), console.info("onfire.所有数据发送完成 共:" + e.length + "包"), o.call(this, {
  366. code: successCode,
  367. err_msg: "数据发送成功",
  368. data: e,
  369. msg: ""
  370. }))
  371. },
  372. fail: function (e) {
  373. console.log(TAG + "wx. 发送失败 :" + Date.parse(new Date)), console.log("writeBLECharacteristicValue fail:" + JSON.stringify(e));
  374. var c = "";
  375. e && (c = bleErrorMsg.bluetoothErrMsg(e.errCode)), o.call(this, {
  376. code: bleCode.bleErrorCode(),
  377. err_msg: c,
  378. data: null,
  379. msg: ""
  380. })
  381. }
  382. })
  383. })
  384. }