const Manager = require('./manager'); class BleUtil { static _instance = new BleUtil(); static getInstance() { return BleUtil._instance; } constructor() { var that = this; that.manager = new Manager(); } ///获取比较的数据 getCompareList() { var that = this; return that.manager.getCompareList(); } getDissmissDevice() { var that = this; return that.manager.getDissmissDevice(); } getCallBackConnect() { var that = this; return that.manager.getCallBackConnect(); } ///初始化蓝牙适配器 initBluetoothAdapter() { var that = this; that.manager.initBlueAdapter(); } ///获取蓝牙设备 connect:true,去连接,false,去对比 listenBlueDevices() { var that = this; that.manager.listenBlueDevices(); } ///搜索蓝牙设备 startScan(connectWillDevice, boolean, callBackConnect) { var that = this; that.manager.startScan( connectWillDevice, boolean, callBackConnect, ); } ///停止搜索蓝牙设备 async stopScan() { var that = this; await that.manager.stopScan(); } ///开始连接设备 async startConnect(device, onChanged) { var that = this; await that.stopScan(); // await that.disconnect(device); var res = await that.manager.startConnect(device); if (!res) { onChanged(false, device); return; } const strings = require('./../../utils/strings'); var services = await that.manager.getNotifyServices(device); if (strings.isEmpty(services)) { console.log("gadsfasdfadfaf===bbb=="); onChanged(false); return; } that.manager.getCharacteristics(device, services, 0, false, false, false, async function (notifyServiceId, notifyCharaterId) { var isTrue = that.manager.monitorCharacteristicValueChange(device, notifyServiceId, notifyCharaterId); if (!isTrue) { onChanged(false); return; } console.log("gadsfasdfadfaf===ddd==" + JSON.stringify(isTrue)); ///解决连接之后会有一段时间显示离线,先添加进去就不会了 // var has = false; // var compareList = that.manager.getCompareList(); // for (var j = 0; j < compareList.length; j++) { // if (compareList[j].deviceId == device.deviceId) { // has = true; // break; // } // } // if (!has) { // compareList.unshift(device); // that.manager.setCompareList(compareList); // } onChanged(true); const hex_util = require('./../../utils/hex_util'); const bt_parse = require('./../../devices/bluetooth/bt_parse'); that.manager.onBLECharacteristicValueChange(function (value) { var receiveText = hex_util.buf2string(value); console.log("gadsfasdfadfaf===eeee==" + receiveText); console.log('接收到数据文字:' + receiveText); var buffer = new DataView(value); var result = [] for (let i = 0; i < buffer.byteLength; i++) { result.push(buffer.getUint8(i)) } bt_parse.parseTLV(result); }); }, function () { onChanged(false); return; }); } ///发送数据 async sendData(device, wifiName, pwd) { var that = this; console.log("gadsfasdfadfaf===222==" + wifiName + "===" + pwd); var text = that.sendWiFiInfo(wifiName, pwd); console.log("gadsfasdfadfaf===444==" + text); var isTrue = await that.manager.sendData(device, text); console.log("gadsfasdfadfaf===333==" + isTrue); ///发送数据成功 if (isTrue) { } } sendWiFiInfo(wifiName, pwd) { const hex_util = require('../hex_util'); let result = []; // 字母*6 + let wifiList = hex_util.string2ListInt(wifiName); // 数字*3 + let pwdList = hex_util.string2ListInt(pwd); // 16进制 result.push(0x22); result.push(parseInt(hex_util.int2Hex(wifiList.length + pwdList.length + 6), 16)); // 账号 result.push(0x33); result.push(parseInt(hex_util.int2Hex(wifiList.length), 16)); let p = result[3] + 4; let j = 0; for (let i = 4; i < p; i++) { result.splice(i, 0, wifiList[j++]); } // 密码 result.splice(p, 0, 0x44); result.splice(++p, 0, parseInt(hex_util.int2Hex(pwdList.length), 16)); p++; j = 0; for (let i = p; i < p + pwdList.length; i++) { result.splice(i, 0, pwdList[j++]); } ///[34, 22, 51, 5, 109, 117, 122, 101, 110, 68, 11, 109, 117, 122, 101, 110, 111, 102, 102, 105, 99, 101] console.log("发送wifi账号密码:", result.toString()); return result.toString(); } ///断开设备连接 async disconnect(device) { var that = this; await that.manager.disconnect(device); } } module.exports = { BleUtil }