123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- const {
- configLoccation,
- configBlueGoSetting,
- configScopeBlue
- } = require("./blue_power_tool");
- let timeOut = null //搜索timeOut
- var SCAN_TIME = 59 //扫描时间
- const BLE_SERVICE_15_UUID = "FEE7" // 目标广播服务
- let deviceArray = [] //保存当前扫描到的蓝牙设备
- /**
- * 回调结构体
- */
- const DevResult = {
- code: 1000, //接口执行成功与否,1000标识成功,4000通用失败,2000超时,2002连接超时
- msg: '', //信息
- data: []
- }
-
-
-
-
- /**
- * 对外公开
- * 开始搜索
- */
- export async function starSearch(cb, show = true, msg = "开始搜索设备", isShowModel = true) {
-
- // 定位权限
- configLoccation((code) => {
- // code = 0 定位权限通过
- if (code == 0) {
- beginSearch(cb, show, msg, isShowModel);
- } else {
- configBlueUtilCallBack(cb, -112, "", "");
- }
- });
- }
- /**
- * 关闭所有
- * @param {*} isCloseBlue
- */
- export function stopSearch(isCloseBlue = true) {
- deviceArray = [];
- configClearTimeOuter();
- stopScanDevice();
- if (isCloseBlue) {
- closeBlue();
- }
- }
-
-
-
- function beginSearch(cb, show = true, msg = "开始搜索设备", isShowModel = true, ) {
- if (show) {
- wx.showLoading({
- title: msg,
- });
- }
- var list = [];
-
- stopSearch(false);
- openBle((DevResult) => {
- console.log("------------------- 蓝牙权限 ----------------");
- if (DevResult.code == 0) {
- console.log("------------------- 开始搜索 ----------------");
- startBluetoothDevicesDiscovery((DevResult) => {
- if (DevResult.code == 0) {
- let device = DevResult.data;
- const info = {
- device: device,
- is_select: false,
- }
- if (list.some(item => {
- return item.device.name === device.name
- })) {
- list.splice(list.findIndex(item => item.name === device.name), 1, info);
- } else {
- list.push(info);
- }
- var ishave_select = false;
- for (let index = 0; index < list.length; index++) {
- const element = list[index];
- if (element.is_select) {
- ishave_select = true;
- break;
- }
- }
- if (!ishave_select) {
- list[0].is_select = true;
- }
- }
- configBlueUtilCallBack(cb, DevResult.code, list, DevResult.msg);
- });
- } else {
- if (show) {
- wx.hideLoading();
- }
- if (isShowModel) {
- wx.showModal({
- showCancel: false,
- content: DevResult.msg,
- });
- }
-
- configBlueUtilCallBack(cb, -111, DevResult.data, DevResult.msg);
- }
- });
- }
-
-
- /**
- * 扫描蓝牙外设
- */
- function startBluetoothDevicesDiscovery(cb) {
- wx.startBluetoothDevicesDiscovery({
- services: [BLE_SERVICE_15_UUID],
- allowDuplicatesKey: true, //设置是否上报同一设备
- success: (res) => {
- configStartBluetoothSuccess(cb);
- },
- fail: () => {
- configBlueUtilCallBack(cb, 1, "", "搜索蓝牙失败")
- }
- })
- }
-
-
-
- function configIsTargetDevice(deviceName) {
- var isHave = false;
- let targetDeviceS = [
- "JL",
- "GV__GD_FK",
- "GV_HCB_Q1",
- "GV__BT_my",
- "GENVICT",
- "GV_BT_my",
- "JY",
- "SD_BT_my",
- "GS",
- "GV",
- "GWMETC-A",
- "GMWETC",
- "GWMETC-B",
- "XZ",
- "LJ",
- "JT",
- "WJ",
- "AT",
- "WQ",
- "CG",
- "TD",
- "ZZ",
- "ETC",
- ];
- for (let index = 0; index < targetDeviceS.length; index++) {
- const element = targetDeviceS[index];
- if (element.indexOf(deviceName)) {
- isHave = true;
- break;
- }
-
- }
- return isHave;
- }
-
- //扫描到外设时调用
- function onBluetoothDeviceFound(cb) {
- wx.onBluetoothDeviceFound((res) => {
- res.devices.forEach(device => {
- if (!device.name && !device.localName) {
- return
- }
- let thisdevice = device;
- let devieceName = device.name;
- console.log(devieceName);
- if (configIsTargetDevice(devieceName)) {
- const idx = inArray(deviceArray, 'deviceId', device.deviceId);
- if (idx == -1) {
- deviceArray.push(thisdevice);
- configBlueUtilCallBack(cb, 0, thisdevice, "");
- }
- }
- })
- });
- }
-
- function inArray(arr, key, val) {
- for (let i = 0; i < arr.length; i++) {
- if (arr[i][key] === val) {
- return i;
- }
- }
- return -1;
- }
-
-
-
-
- /**
- * 停止搜索蓝牙OBU
- */
- function stopScanDevice(cb) {
- wx.offBluetoothDeviceFound();
- wx.stopBluetoothDevicesDiscovery({
- success: function (res) {
- DevResult.code = 0
- DevResult.msg = '停止搜索成功'
- typeof cb === 'function' && cb(DevResult)
- },
- fail: (res) => {
- DevResult.code = 1
- DevResult.msg = '停止搜索失败'
- typeof cb === 'function' && cb(DevResult)
- }
- })
- }
-
-
- /**
- * 配置搜索设备权限成功
- */
- function configStartBluetoothSuccess(cb) {
- configTimeOuter(cb);
- onBluetoothDeviceFound(cb);
- }
-
- /**
- * 配置定时器,规定时间内搜索不到设备
- * 提示用户
- */
- function configTimeOuter(cb) {
- SCAN_TIME = 59;
- timeOut = setInterval(function () {
- SCAN_TIME = SCAN_TIME - 1;
- console.log(SCAN_TIME);
- if (SCAN_TIME <= 0 && deviceArray.length == 0) {
- configClearTimeOuter();
- stopScanDevice();
- configBlueUtilCallBack(cb, 1, "", "搜索蓝牙失败");
- } else if (deviceArray.length != 0 && SCAN_TIME <= 0) {
- console.log("搜索到的设备");
- console.log(deviceArray);
- configClearTimeOuter();
- }
- }, 1000);
- }
-
-
- /**
- * 销毁定时器
- */
- function configClearTimeOuter() {
- console.log("清空倒计时");
- clearTimeout(timeOut);
- }
-
- /**
- * 打开蓝牙权限
- */
- function openBle(cb) {
- configScopeBlue().then(result => {
- wx.openBluetoothAdapter({
- success: () => {
- // configBlueUtilCallBack(cb, 0);
- wx.getBluetoothAdapterState({
- success: res => {
- if (res.available == true && res.discovering == false) {
- configBlueUtilCallBack(cb, 0);
- }
- if (res.available == false) {
- configBlueUtilCallBack(cb, 1, "", "蓝牙功能不可用,请尝试升级微信APP")
- }
- },
- fail: error => {
- console.log(error);
- let errCode = error.errCode;
- configBlueUtilCallBack(cb, 1, error, configBlueErrorCode(errCode));
- }
- })
- },
- fail: (err) => {
- console.log('openBluetoothAdapter:ERROR')
- console.log(err);
-
- if (err.errMsg.includes('auth deny')) {
- configBlueGoSetting((code, msg) => {
- if (code == 0) {
- configBlueUtilCallBack(cb, 0);
- } else {
- configBlueUtilCallBack(cb, 1, "", msg);
- }
- })
- } else {
- configBlueUtilCallBack(cb, 1, "", configBlueErrorCode(err.errCode));
- }
-
- // configBlueUtilCallBack(cb, 1, err, configBlueErrorCode(err.errCode));
- // configBlueUtilCallBack(cb, 1, "", "您已拒绝授权开启蓝牙权限,无法进行激活操作。");
-
- }
- });
- }).catch(err => {
- console.log('configScopeBlue:ERROR')
- console.log(err);
- configBlueUtilCallBack(cb, 1, err, "您已拒绝授权开启蓝牙权限,无法进行激活操作。");
- });
- }
-
-
-
- function configBlueErrorCode(errCode) {
- var msg = "请先打开手机蓝牙"
- if (errCode == 10000) {
- msg = "未初始化蓝牙适配器"
- } else if (errCode == 10001) {
- msg = "请先打开手机蓝牙";
- } else if (errCode == 10002) {
- msg = "未搜索到设备";
- } else if (errCode == 10003) {
- msg = "设备连接失败";
- } else if (errCode == 10004) {
- msg = "没有找到指定服务";
- } else if (errCode == 10005) {
- msg = "没有找到指定特征";
- } else if (errCode == 10006) {
- msg = "当前连接已断开";
- } else if (errCode == 10007) {
- msg = "当前特征不支持此操作";
- } else if (errCode == 10008) {
- msg = "其余所有系统上报的异常";
- } else if (errCode == 10009) {
- msg = "Android 系统特有,系统版本低于 4.3 不支持 BLE";
- } else if (errCode == 10012) {
- msg = "连接超时";
- }
- return msg;
- }
-
- // 关闭蓝牙模块。调用该方法将断开所有已建立的连接并释放系统资源
- function closeBlue() {
- wx.closeBluetoothAdapter();
- }
-
-
-
- function configBlueUtilCallBack(cb, code, data, msg) {
- DevResult.code = code;
- if (msg) {
- DevResult.msg = msg;
- } else {
- DevResult.msg = "";
- }
- if (data) {
- DevResult.data = data;
- } else {
- DevResult.data = "";
- }
- console.log(DevResult);
- typeof cb === 'function' && cb(DevResult)
- }
-
- module.exports = {
- starSearch,
- stopSearch,
- }
|