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.

blue_tool.js 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. const {
  2. configLoccation,
  3. configBlueGoSetting,
  4. configScopeBlue
  5. } = require("./blue_power_tool");
  6. let timeOut = null //搜索timeOut
  7. var SCAN_TIME = 59 //扫描时间
  8. const BLE_SERVICE_15_UUID = "FEE7" // 目标广播服务
  9. let deviceArray = [] //保存当前扫描到的蓝牙设备
  10. /**
  11. * 回调结构体
  12. */
  13. const DevResult = {
  14. code: 1000, //接口执行成功与否,1000标识成功,4000通用失败,2000超时,2002连接超时
  15. msg: '', //信息
  16. data: []
  17. }
  18. /**
  19. * 对外公开
  20. * 开始搜索
  21. */
  22. export async function starSearch(cb, show = true, msg = "开始搜索设备", isShowModel = true) {
  23. // 定位权限
  24. configLoccation((code) => {
  25. // code = 0 定位权限通过
  26. if (code == 0) {
  27. beginSearch(cb, show, msg, isShowModel);
  28. } else {
  29. configBlueUtilCallBack(cb, -112, "", "");
  30. }
  31. });
  32. }
  33. /**
  34. * 关闭所有
  35. * @param {*} isCloseBlue
  36. */
  37. export function stopSearch(isCloseBlue = true) {
  38. deviceArray = [];
  39. configClearTimeOuter();
  40. stopScanDevice();
  41. if (isCloseBlue) {
  42. closeBlue();
  43. }
  44. }
  45. function beginSearch(cb, show = true, msg = "开始搜索设备", isShowModel = true, ) {
  46. if (show) {
  47. wx.showLoading({
  48. title: msg,
  49. });
  50. }
  51. var list = [];
  52. stopSearch(false);
  53. openBle((DevResult) => {
  54. console.log("------------------- 蓝牙权限 ----------------");
  55. if (DevResult.code == 0) {
  56. console.log("------------------- 开始搜索 ----------------");
  57. startBluetoothDevicesDiscovery((DevResult) => {
  58. if (DevResult.code == 0) {
  59. let device = DevResult.data;
  60. const info = {
  61. device: device,
  62. is_select: false,
  63. }
  64. if (list.some(item => {
  65. return item.device.name === device.name
  66. })) {
  67. list.splice(list.findIndex(item => item.name === device.name), 1, info);
  68. } else {
  69. list.push(info);
  70. }
  71. var ishave_select = false;
  72. for (let index = 0; index < list.length; index++) {
  73. const element = list[index];
  74. if (element.is_select) {
  75. ishave_select = true;
  76. break;
  77. }
  78. }
  79. if (!ishave_select) {
  80. list[0].is_select = true;
  81. }
  82. }
  83. configBlueUtilCallBack(cb, DevResult.code, list, DevResult.msg);
  84. });
  85. } else {
  86. if (show) {
  87. wx.hideLoading();
  88. }
  89. if (isShowModel) {
  90. wx.showModal({
  91. showCancel: false,
  92. content: DevResult.msg,
  93. });
  94. }
  95. configBlueUtilCallBack(cb, -111, DevResult.data, DevResult.msg);
  96. }
  97. });
  98. }
  99. /**
  100. * 扫描蓝牙外设
  101. */
  102. function startBluetoothDevicesDiscovery(cb) {
  103. wx.startBluetoothDevicesDiscovery({
  104. services: [BLE_SERVICE_15_UUID],
  105. allowDuplicatesKey: true, //设置是否上报同一设备
  106. success: (res) => {
  107. configStartBluetoothSuccess(cb);
  108. },
  109. fail: () => {
  110. configBlueUtilCallBack(cb, 1, "", "搜索蓝牙失败")
  111. }
  112. })
  113. }
  114. function configIsTargetDevice(deviceName) {
  115. var isHave = false;
  116. let targetDeviceS = [
  117. "JL",
  118. "GV__GD_FK",
  119. "GV_HCB_Q1",
  120. "GV__BT_my",
  121. "GENVICT",
  122. "GV_BT_my",
  123. "JY",
  124. "SD_BT_my",
  125. "GS",
  126. "GV",
  127. "GWMETC-A",
  128. "GMWETC",
  129. "GWMETC-B",
  130. "XZ",
  131. "LJ",
  132. "JT",
  133. "WJ",
  134. "AT",
  135. "WQ",
  136. "CG",
  137. "TD",
  138. "ZZ",
  139. "ETC",
  140. ];
  141. for (let index = 0; index < targetDeviceS.length; index++) {
  142. const element = targetDeviceS[index];
  143. if (element.indexOf(deviceName)) {
  144. isHave = true;
  145. break;
  146. }
  147. }
  148. return isHave;
  149. }
  150. //扫描到外设时调用
  151. function onBluetoothDeviceFound(cb) {
  152. wx.onBluetoothDeviceFound((res) => {
  153. res.devices.forEach(device => {
  154. if (!device.name && !device.localName) {
  155. return
  156. }
  157. let thisdevice = device;
  158. let devieceName = device.name;
  159. console.log(devieceName);
  160. if (configIsTargetDevice(devieceName)) {
  161. const idx = inArray(deviceArray, 'deviceId', device.deviceId);
  162. if (idx == -1) {
  163. deviceArray.push(thisdevice);
  164. configBlueUtilCallBack(cb, 0, thisdevice, "");
  165. }
  166. }
  167. })
  168. });
  169. }
  170. function inArray(arr, key, val) {
  171. for (let i = 0; i < arr.length; i++) {
  172. if (arr[i][key] === val) {
  173. return i;
  174. }
  175. }
  176. return -1;
  177. }
  178. /**
  179. * 停止搜索蓝牙OBU
  180. */
  181. function stopScanDevice(cb) {
  182. wx.offBluetoothDeviceFound();
  183. wx.stopBluetoothDevicesDiscovery({
  184. success: function (res) {
  185. DevResult.code = 0
  186. DevResult.msg = '停止搜索成功'
  187. typeof cb === 'function' && cb(DevResult)
  188. },
  189. fail: (res) => {
  190. DevResult.code = 1
  191. DevResult.msg = '停止搜索失败'
  192. typeof cb === 'function' && cb(DevResult)
  193. }
  194. })
  195. }
  196. /**
  197. * 配置搜索设备权限成功
  198. */
  199. function configStartBluetoothSuccess(cb) {
  200. configTimeOuter(cb);
  201. onBluetoothDeviceFound(cb);
  202. }
  203. /**
  204. * 配置定时器,规定时间内搜索不到设备
  205. * 提示用户
  206. */
  207. function configTimeOuter(cb) {
  208. SCAN_TIME = 59;
  209. timeOut = setInterval(function () {
  210. SCAN_TIME = SCAN_TIME - 1;
  211. console.log(SCAN_TIME);
  212. if (SCAN_TIME <= 0 && deviceArray.length == 0) {
  213. configClearTimeOuter();
  214. stopScanDevice();
  215. configBlueUtilCallBack(cb, 1, "", "搜索蓝牙失败");
  216. } else if (deviceArray.length != 0 && SCAN_TIME <= 0) {
  217. console.log("搜索到的设备");
  218. console.log(deviceArray);
  219. configClearTimeOuter();
  220. }
  221. }, 1000);
  222. }
  223. /**
  224. * 销毁定时器
  225. */
  226. function configClearTimeOuter() {
  227. console.log("清空倒计时");
  228. clearTimeout(timeOut);
  229. }
  230. /**
  231. * 打开蓝牙权限
  232. */
  233. function openBle(cb) {
  234. configScopeBlue().then(result => {
  235. wx.openBluetoothAdapter({
  236. success: () => {
  237. // configBlueUtilCallBack(cb, 0);
  238. wx.getBluetoothAdapterState({
  239. success: res => {
  240. if (res.available == true && res.discovering == false) {
  241. configBlueUtilCallBack(cb, 0);
  242. }
  243. if (res.available == false) {
  244. configBlueUtilCallBack(cb, 1, "", "蓝牙功能不可用,请尝试升级微信APP")
  245. }
  246. },
  247. fail: error => {
  248. console.log(error);
  249. let errCode = error.errCode;
  250. configBlueUtilCallBack(cb, 1, error, configBlueErrorCode(errCode));
  251. }
  252. })
  253. },
  254. fail: (err) => {
  255. console.log('openBluetoothAdapter:ERROR')
  256. console.log(err);
  257. if (err.errMsg.includes('auth deny')) {
  258. configBlueGoSetting((code, msg) => {
  259. if (code == 0) {
  260. configBlueUtilCallBack(cb, 0);
  261. } else {
  262. configBlueUtilCallBack(cb, 1, "", msg);
  263. }
  264. })
  265. } else {
  266. configBlueUtilCallBack(cb, 1, "", configBlueErrorCode(err.errCode));
  267. }
  268. // configBlueUtilCallBack(cb, 1, err, configBlueErrorCode(err.errCode));
  269. // configBlueUtilCallBack(cb, 1, "", "您已拒绝授权开启蓝牙权限,无法进行激活操作。");
  270. }
  271. });
  272. }).catch(err => {
  273. console.log('configScopeBlue:ERROR')
  274. console.log(err);
  275. configBlueUtilCallBack(cb, 1, err, "您已拒绝授权开启蓝牙权限,无法进行激活操作。");
  276. });
  277. }
  278. function configBlueErrorCode(errCode) {
  279. var msg = "请先打开手机蓝牙"
  280. if (errCode == 10000) {
  281. msg = "未初始化蓝牙适配器"
  282. } else if (errCode == 10001) {
  283. msg = "请先打开手机蓝牙";
  284. } else if (errCode == 10002) {
  285. msg = "未搜索到设备";
  286. } else if (errCode == 10003) {
  287. msg = "设备连接失败";
  288. } else if (errCode == 10004) {
  289. msg = "没有找到指定服务";
  290. } else if (errCode == 10005) {
  291. msg = "没有找到指定特征";
  292. } else if (errCode == 10006) {
  293. msg = "当前连接已断开";
  294. } else if (errCode == 10007) {
  295. msg = "当前特征不支持此操作";
  296. } else if (errCode == 10008) {
  297. msg = "其余所有系统上报的异常";
  298. } else if (errCode == 10009) {
  299. msg = "Android 系统特有,系统版本低于 4.3 不支持 BLE";
  300. } else if (errCode == 10012) {
  301. msg = "连接超时";
  302. }
  303. return msg;
  304. }
  305. // 关闭蓝牙模块。调用该方法将断开所有已建立的连接并释放系统资源
  306. function closeBlue() {
  307. wx.closeBluetoothAdapter();
  308. }
  309. function configBlueUtilCallBack(cb, code, data, msg) {
  310. DevResult.code = code;
  311. if (msg) {
  312. DevResult.msg = msg;
  313. } else {
  314. DevResult.msg = "";
  315. }
  316. if (data) {
  317. DevResult.data = data;
  318. } else {
  319. DevResult.data = "";
  320. }
  321. console.log(DevResult);
  322. typeof cb === 'function' && cb(DevResult)
  323. }
  324. module.exports = {
  325. starSearch,
  326. stopSearch,
  327. }