|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643 |
-
- const plugin = requirePlugin('ppScale-plugin');
- import {
- CommandButtonList
- } from '../../model/index'
- let app = getApp();
- Page({
- data: {
-
- selmac: null,
-
- scaleing: false,
-
-
-
-
- devLink: false,
-
-
-
- selectDev: null,
-
- deviceModel: null,
-
- contnectResult: null,
-
- sendData: null,
-
- callBack: null,
-
- sendBtnList: CommandButtonList.send,
-
- reportData: CommandButtonList.report,
-
- inputValue: '',
- deviceInfo: {
- version: '',
- battery:'',
- chargeState: '',
- poleWorkState: '',
- workState: '',
- error: '',
- workStart: '',
- workEnd: '',
- workResult: '',
- }
-
-
- },
-
-
-
- onUnload() {
-
- wx.offBluetoothAdapterStateChange();
- wx.closeBluetoothAdapte();
- },
-
-
-
- onHide() {
-
- },
- onLoad() {
- let that = this;
-
- wx.onBLEConnectionStateChange(function (res) {
-
- console.log(`设备蓝牙连接状态`, res);
-
- if (!res.connected) {
-
- that.setData({
- devLink: false,
- })
- wx.showToast({
- title: '蓝牙已断开',
- });
- that.connect();
-
-
-
- } else {
- that.setData({
- devLink: true,
- })
- setTimeout(() => {
-
- wx.getBLEDeviceServices({
- deviceId: app.globalData.selmac.deviceId,
- success: (res) => {
- console.log("获取蓝牙低功耗设备所有服务成功", res);
- let serviceId = '0000FFF0-0000-1000-8000-00805F9B34FB'
- console.log("获取到的serviceId::", serviceId);
-
- app.globalData.serviceId = serviceId;
-
-
- wx.getBLEDeviceCharacteristics({
- deviceId: app.globalData.selmac.deviceId,
- serviceId: '0000FFF0-0000-1000-8000-00805F9B34FB',
- success: (res) => {
- console.log("获取蓝牙低功耗设备某个服务中所有特征", res);
- let characteristicId = res.characteristics[0].uuid;
-
- app.globalData.characteristicId = characteristicId;
- that.notify();
- },
- fail: (e) => {
- console.log("获取蓝牙低功耗设备某个服务中所有特征", e);
- }
- })
- },
- fail: (e) => {
- console.log("获取蓝牙低功耗设备所有服务失败", e);
- }
- })
-
-
-
-
- }, 800)
- }
- })
- },
- onShow() {
- let that = this;
-
- this.setData({
- selmac: app.globalData.selmac
- })
- console.log("selmac", that.data.selmac);
-
- wx.createBLEConnection({
- deviceId: app.globalData.selmac.deviceId,
- success: (res) => {
- console.log("连接成功", res);
- that.setData({
- devLink: true
- })
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- },
- fail: (e) => {
- console.log("连接失败", e);
- }
- })
-
-
-
-
-
-
- },
- connect() {
- let that = this;
- if (this.data.selmac.deviceId) {
- console.log("可以重新初始化蓝牙");
- if(!this.data.devLink) {
-
- wx.openBluetoothAdapter({
- mode: 'peripheral',
- })
-
- wx.createBLEConnection({
- deviceId: that.data.selmac.deviceId,
- success: (res) => {
- console.log("建立连接成功", res);
- },
- fail: (e) => {
- console.log("建立连接失败", e);
- wx.showToast({
- title: '重新连接失败',
- icon: 'error'
- })
- }
- })
- }
-
- }
- },
- disconnect() {
- let that = this;
- if (this.data.selmac.deviceId) {
-
- wx.closeBLEConnection({
- deviceId: that.data.selmac.deviceId,
- success: (res) => {
- console.log("断开连接成功", res);
- },
- fail: (e) => {
- console.log("断开连接失败", e);
- }
- })
- }
- },
- onClearInput() {
- this.setData({
- callBack: '',
- sendData: '',
- });
-
- },
- startwrite() {
-
-
-
- let testdata = new Uint16Array([...'FE0007010100FF'].map(v => v.codePointAt(0)));
- console.log("testdata", testdata);
- let strToHexCharCode = this.hexToByteString('FE0007010100FF');
- let buffer = this.hexStringToArrayBuffer(strToHexCharCode);
- let hexData = this.ab2hex(buffer);
-
- console.log("十六进制转成二进制", strToHexCharCode);
- console.log("二进制字符串转buffer", buffer);
- console.log("buffer转hexData", hexData);
-
-
-
-
- },
- hexStringToArrayBuffer(hexStr) {
-
- hexStr = hexStr.replace(/\\x/g, '').replace(/^0+/, '');
-
-
- if (hexStr.length % 2 !== 0) {
- hexStr = '0' + hexStr;
- }
-
-
- const bytes = new Uint8Array(hexStr.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
-
-
- return bytes.buffer;
- },
- bindTextAreaBlur(e) {
- this.data.inputValue = e.detail.value;
- console.log(e.detail.value)
- },
-
- hexToByteString(hexString) {
- let bytes = [];
- for (let i = 0; i < hexString.length; i += 2) {
- bytes.push("\\x" + hexString.substr(i, 2));
- }
- return bytes.join('');
- },
-
-
- onInputSend() {
- console.log("this.data.inputValue", this.data.inputValue);
- if (this.data.inputValue) {
- this.sendMessage(this.data.inputValue, true);
- } else {
- wx.showToast({
- title: '指令不能为空',
- icon: 'error'
- })
- }
- },
-
- sendMessage(event, type) {
- let that = this;
- let sendData = '';
- if (type) {
- console.log("手动输出方式");
- sendData = event
- } else {
- console.log("按钮点击输出方式");
- console.log("指令名称::", event.target.dataset.command.name);
- sendData = event.target.dataset.command.defaultSendData;
- }
-
- if (!that.data.devLink) {
- wx.showToast({
- title: '蓝牙已断开',
- icon: 'error'
- })
- return
- }
-
- if (!sendData) {
- wx.showToast({
- title: '指令不存在',
- icon: 'error'
- })
- return
- }
- this.setData({
- sendData: sendData
- })
-
-
- console.log("未转换前的十六进制数据::", sendData)
- let strToHexCharCode = this.hexToByteString(sendData);
- console.log("十六进制转成二进制的数据::", strToHexCharCode);
- let buffer = this.hexStringToArrayBuffer(strToHexCharCode);
- console.log("二进制转成arrayBuffter的数据::", buffer);
- console.log("特征值::", 'E4BAA7E5-908E-E5A1-91E5-BDA2E4BBAA02');
-
-
- console.log("buffer", buffer);
- wx.writeBLECharacteristicValue({
- deviceId: app.globalData.selmac.deviceId,
- serviceId: '0000FFF0-0000-1000-8000-00805F9B34FB',
- characteristicId: 'E4BAA7E5-908E-E5A1-91E5-BDA2E4BBAA02',
- value: buffer,
- success(res) {
- console.log("write指令发送成功", res)
-
- },
- fail(err) {
- console.log("write指令发送失败", err);
- that.setData({
- callBack: err.errMsg
- });
- wx.showToast({
- title: '指令发送失败',
- icon: 'error'
- })
-
- }
- })
- },
- string2Hex(str) {
- let val = ""
- for (let i = 0; i < str.length; i++) {
- if (val == "")
- val = str.charCodeAt(i).toString(16)
- else
- val += str.charCodeAt(i).toString(16)
- }
- return val
- },
- hex2ArrayBuffer(hex_str) {
-
- let typedArray = new Uint8Array(hex_str.match(/[\da-f]{2}/gi).map(function (h) {
- return parseInt(h, 16)
- }))
- let buffer = typedArray.buffer
- return buffer
- },
- cleanHexString(hexStr) {
-
- let cleanedHex = hexStr.replace(/0x/g, '').replace(/\b0+/g, '');
- return cleanedHex;
- },
-
-
- read() {
- let that = this;
- let text = 'fe000f000176657220312e302e31ff'
- let string = that.hexCharCodeToStr(text);
- console.log("测试", string);
-
-
- },
-
- write(data, type) {
- let that = this;
- let characteristicId = type === 'read' ? 'E4BAA7E5-908E-E5A1-91E5-BDA2E4BBAA01' : 'E4BAA7E5-908E-E5A1-91E5-BDA2E4BBAA02';
- console.log("监听到设备有数据上报,回应设备");
- console.log("未转换前的十六进制数据::", data)
- let strToHexCharCode = this.hexToByteString(data);
- console.log("十六进制转成二进制的数据::", strToHexCharCode);
- let buffer = this.hexStringToArrayBuffer(strToHexCharCode);
- console.log("二进制转成arrayBuffter的数据::", buffer);
- console.log("特征值::", characteristicId);
- wx.writeBLECharacteristicValue({
- deviceId: app.globalData.selmac.deviceId,
- serviceId: '0000FFF0-0000-1000-8000-00805F9B34FB',
- characteristicId: characteristicId,
- value: buffer,
- success(res) {
- console.log("write指令发送成功", res)
-
-
- },
- fail(err) {
- console.log("write指令发送失败", err);
- that.setData({
- callBack: err.errMsg
- });
- wx.showToast({
- title: '指令发送失败',
- icon: 'error'
- })
-
- }
- })
- },
-
- extractMiddleData(encodedStr, startNum, endNum) {
-
- const start = encodedStr.substring(0, startNum);
-
- const end = encodedStr.substring(encodedStr.length - endNum);
-
- const middleStartIndex = start.length;
-
- const middleLength = encodedStr.length - middleStartIndex - end.length;
-
- const middlePart = encodedStr.substring(middleStartIndex, middleStartIndex + middleLength);
- return middlePart;
- },
-
- notify() {
- let that = this;
- wx.notifyBLECharacteristicValueChange({
- deviceId: app.globalData.selmac.deviceId,
- serviceId: app.globalData.serviceId,
- characteristicId: app.globalData.characteristicId,
- state: true,
- success(res) {
- console.log("开启消息监听", res)
-
- that.listenValueChange()
-
-
- },
- fail(err) {
- console.error("消息监听失败", err)
- }
- })
- },
-
- listenValueChange() {
- let that = this;
- let result = "";
- wx.onBLECharacteristicValueChange(res => {
- console.log("设备响应", res)
- console.log("设备上报的数据", res.value);
-
- let resHex = that.ab2hex(res.value);
- console.log("设备上报的16进制字符串", resHex);
-
- let sliceData = resHex.slice(6, 10);
-
- console.log("截取到上报数据的关键指令", sliceData);
-
- result = that.extractMiddleData(resHex, 10, 2);
- console.log("上报截取关键内容::", result);
- let isAuto = that.data.reportData.findIndex(item => {
- return item.keyWord === sliceData;
- })
- if (isAuto > -1) {
-
- let report = that.data.reportData.filter(item => {
- return item.keyWord === sliceData
- });
- console.log("上报的数据类型:", report[0].cnName);
-
- if (report[0].keyWord === '0001') {
- that.setData({
- ['deviceInfo.version']: that.hexCharCodeToStr(result)
- })
- } else if (report[0].keyWord === '0002') {
-
- that.setData({
- ['deviceInfo.battery']: result
- })
- }else if (report[0].keyWord === '0003') {
-
- that.setData({
- ['deviceInfo.chargeState']: result === '01' ? '以充电' : '未充电'
- })
- } else if (report[0].keyWord === '0004') {
-
- that.setData({
- ['deviceInfo.poleWorkState']: result
- })
- }else if (report[0].keyWord === '0005') {
-
- that.setData({
- ['deviceInfo.workState']: result
- })
- }else if (report[0].keyWord === '0006') {
-
- that.setData({
- ['deviceInfo.error']: result
- })
- }else if (report[0].keyWord === '0007') {
-
- that.setData({
- ['deviceInfo.workStart']: result
- })
- }else if (report[0].keyWord === '0008') {
-
- that.setData({
- ['deviceInfo.workEnd']: result
- })
- }else if (report[0].keyWord === '0009') {
-
- that.setData({
- ['deviceInfo.workResult']: result
- })
- }else {
- }
-
- that.write(report[0].response, 'write');
- } else {
-
-
-
- console.log("下发上报");
-
- if(sliceData === '8101'){
-
- let resultArr = that.splitStringEveryTwoChars(result, 2);
- let workMapList = resultArr.map(item => {
- return that.hexToDecimal(item);
- })
- console.log("resultArr", resultArr);
- console.log("workMapList", workMapList);
- } else if (sliceData === '8102') {
-
- console.log("设置设备工作强度映射表响应", resHex);
-
- }
-
- }
- that.setData({
- callBack: String(result)
- });
-
-
- })
- },
-
- hexCharCodeToStr(hexCharCodeStr) {
- var trimedStr = hexCharCodeStr.trim();
- var rawStr = trimedStr.substr(0, 2).toLowerCase() === "0x" ? trimedStr.substr(2) : trimedStr;
- var len = rawStr.length;
- if (len % 2 !== 0) {
- alert("存在非法字符!");
- return "";
- }
- var curCharCode;
- var resultStr = [];
- for (var i = 0; i < len; i = i + 2) {
- curCharCode = parseInt(rawStr.substr(i, 2), 16);
- resultStr.push(String.fromCharCode(curCharCode));
- }
- return decodeURIComponent(resultStr.join(""));
- },
-
- stringToCmdBuffer(msg) {
- console.log("msg", msg);
- const buffer = new ArrayBuffer(msg.length)
- const dataView = new DataView(buffer)
-
-
- for (let i = 0; i < msg.length; i++) {
- dataView.setUint8(i, msg.charAt(i).charCodeAt())
- };
-
- return buffer
-
- },
-
- strToHexCharCode(str) {
- if (str === "")
- return "";
- var hexCharCode = [];
- hexCharCode.push("0x");
- for (var i = 0; i < str.length; i++) {
- hexCharCode.push((str.charCodeAt(i)).toString(16));
- }
- return hexCharCode.join("");
- },
-
- hexToDecimal(hexString) {
- return parseInt(hexString, 16);
- },
-
- ab2hex(buffer) {
- let hexArr = Array.prototype.map.call(
- new Uint8Array(buffer),
- function (bit) {
- return ('00' + bit.toString(16)).slice(-2)
- }
- )
- return hexArr.join('');
- },
-
- splitStringEveryTwoChars(str, splitNumber) {
- let result = [];
- for (let i = 0; i < str.length; i += splitNumber) {
- result.push(str.substring(i, i + splitNumber));
- }
- return result;
- }
-
- })
|