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.

bleUtil.js 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //已经找到的设备列表
  2. let foundDevices = [];
  3. //成功返回编码
  4. const SUCCESS_CODE = 0;
  5. const SUCCESS_MSG = "成功";
  6. //打开适配器失败
  7. const OPEN_ADAPTER_FAIL_CODE = 1001;
  8. // const OPEN_ADAPTER_FAIL_MSG = "打开蓝牙适配器失败"
  9. const OPEN_ADAPTER_FAIL_MSG = "手机蓝牙功能未开启,请开启手机蓝牙功能后重试。"
  10. const AUTH_OPEN_FAIL_MSG = "蓝牙功能需要用户同意隐私授权协议,请同意后重试。"
  11. //搜索设备失败
  12. const SCAN_DEVICE_CODE = 1002;
  13. // const SCAN_DEVICE_MSG = "搜索设备失败"
  14. const SCAN_DEVICE_MSG = "未检测到蓝牙标签设备,请确认标签设备蓝牙和微信定位权限已正确开启,并将手机靠近设备后重试。"
  15. //连接设备失败
  16. const CONNECT_FAIL_CODE = 1003;
  17. const CONNECT_FAIL_MSG = "连接设备失败";
  18. //停止搜索设备失败
  19. const ERR_STOPSCANBLE_CODE = 1004;
  20. const ERR_STOPSCANBLE_MSG = "停止搜索设备失败"
  21. var scanTimeout = null;
  22. var status = false
  23. //开启蓝牙适配器扫描设备
  24. function ScanDevice(deviceNameAry, needShowList, callback) {
  25. deviceNameAry = ['obu', 'etc', 'jl', 'td']
  26. status = false;
  27. foundDevices = [];
  28. let result = new Object();
  29. // wx.closeBluetoothAdapter();
  30. wx.openBluetoothAdapter({
  31. success: (res) => {
  32. console.log("openBluetoothAdapter:success", res);
  33. wx.startBluetoothDevicesDiscovery({
  34. allowDuplicatesKey: false,
  35. // services: ['FEE7'],
  36. success: (res) => {
  37. // 10s之后搜索不到即停止搜索
  38. scanTimeout = setTimeout(function() {
  39. console.log("StopScanDevice2222222222", status);
  40. CheckBle((res) => {
  41. console.log("蓝牙状态", res)
  42. if (res.errCode !== 10000 && !status) {
  43. StopScanDevice(result => {
  44. console.log(
  45. "StopScanDevice11111111111111",
  46. result);
  47. })
  48. callback({
  49. code: 10002,
  50. err_msg: SCAN_DEVICE_MSG
  51. });
  52. }
  53. })
  54. }, 10000);
  55. console.log("startBluetoothDevicesDiscovery:success", res);
  56. wx.onBluetoothDeviceFound(function(res) {
  57. console.log('蓝牙设备返回', res)
  58. let devices = res.devices;
  59. console.log("onBluetoothDeviceFound:", devices && devices[0] &&
  60. devices[0].deviceName);
  61. for (let i = devices.length - 1; i >= 0; i--) {
  62. let isHave = false;
  63. for (let j = 0; j < foundDevices.length; j++) {
  64. if (devices[i].deviceId === foundDevices[j].deviceId) {
  65. isHave = true;
  66. break
  67. }
  68. }
  69. if (!isHave) {
  70. let device = devices[i];
  71. for (let deviceName of deviceNameAry) {
  72. let boolethName = device.localName || device.name
  73. console.log('蓝牙名称', boolethName)
  74. if (boolethName && boolethName.toUpperCase()
  75. .includes(deviceName.toUpperCase())) {
  76. console.log("搜索到设备:" + device.deviceId + " " +
  77. device.name);
  78. result = {
  79. code: SUCCESS_CODE,
  80. err_msg: SUCCESS_MSG,
  81. data: {
  82. device_no: device.deviceId,
  83. device_name: device.name
  84. }
  85. };
  86. foundDevices.push(device);
  87. status = true;
  88. clearTimeout(scanTimeout);
  89. setTimeout(() => {
  90. StopScanDevice(obj => {
  91. if (needShowList) {
  92. if (foundDevices
  93. .length === 0) {
  94. callback({
  95. code: 10002,
  96. err_msg: SCAN_DEVICE_MSG
  97. })
  98. } else {
  99. callback({
  100. code: 99,
  101. err_msg: ""
  102. })
  103. }
  104. }
  105. })
  106. }, needShowList ? 10000 : 10)
  107. callback(result, foundDevices)
  108. break
  109. }
  110. }
  111. }
  112. }
  113. })
  114. },
  115. fail: (err) => {
  116. console.log("startBluetoothDevicesDiscovery:fail", err);
  117. result = {
  118. code: SCAN_DEVICE_CODE,
  119. err_msg: SCAN_DEVICE_MSG
  120. };
  121. callback(result)
  122. }
  123. })
  124. },
  125. fail: (err) => {
  126. console.log("openBluetoothAdapter:fail", err);
  127. if (err.errno === 104) {
  128. result = {
  129. code: err.errno,
  130. err_msg: AUTH_OPEN_FAIL_MSG
  131. };
  132. } else {
  133. result = {
  134. code: OPEN_ADAPTER_FAIL_CODE,
  135. err_msg: OPEN_ADAPTER_FAIL_MSG
  136. };
  137. }
  138. callback(result)
  139. }
  140. })
  141. };
  142. //停止设备搜索
  143. function StopScanDevice(callback) {
  144. wx.stopBluetoothDevicesDiscovery({
  145. success(res) {
  146. console.log("停止设备搜索stopDiscovary:success", res);
  147. let resut = {
  148. code: SUCCESS_CODE,
  149. err_msg: SUCCESS_MSG
  150. };
  151. typeof callback === 'function' && callback(resut)
  152. },
  153. fail(err) {
  154. console.log("stopDiscovary:fail", err);
  155. let resut = {
  156. code: ERR_STOPSCANBLE_CODE,
  157. err_msg: ERR_STOPSCANBLE_MSG
  158. };
  159. typeof callback === 'function' && callback(resut)
  160. },
  161. })
  162. };
  163. //关闭蓝牙适配器
  164. function CloseBle(callback) {
  165. wx.closeBluetoothAdapter({
  166. success: (res) => {
  167. console.log("CloseBle:success", res);
  168. let result = {
  169. code: SUCCESS_CODE,
  170. err_msg: SUCCESS_MSG,
  171. data: res
  172. };
  173. callback(result)
  174. },
  175. fail: (err) => {
  176. console.log("CloseBle:fail", err);
  177. let result = {
  178. code: 1000,
  179. err_msg: "关闭蓝牙设备失败",
  180. data: err
  181. };
  182. callback(result)
  183. }
  184. })
  185. };
  186. //检测蓝牙适配器
  187. function CheckBle(callback) {
  188. wx.getBluetoothAdapterState({
  189. success(res) {
  190. if (res) {
  191. console.log("蓝牙状态", res)
  192. callback(res)
  193. }
  194. },
  195. fail(res) {
  196. callback(res)
  197. }
  198. })
  199. };
  200. //蓝牙搜索接口
  201. export default {
  202. ScanDevice,
  203. CloseBle,
  204. StopScanDevice,
  205. CheckBle
  206. }