增加js文件
This commit is contained in:
parent
0805fb7134
commit
a9332c477b
|
@ -0,0 +1,172 @@
|
||||||
|
function initManager(param, callback) {
|
||||||
|
ble.initManager(param, function (ret) {
|
||||||
|
if (ret.state == "poweredOn") {
|
||||||
|
console.log("初始化成功");
|
||||||
|
callback(true)
|
||||||
|
} else {
|
||||||
|
callback(false)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function scan(param, callback) {
|
||||||
|
ble.scan(param, function (ret) {
|
||||||
|
if (ret.status) {
|
||||||
|
console.log('正在扫描');
|
||||||
|
} else {
|
||||||
|
console.log('扫描失败');
|
||||||
|
}
|
||||||
|
callback(ret.status)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPeripheral(callback) {
|
||||||
|
ble.getPeripheral(function (ret) {
|
||||||
|
// console.log(JSON.stringify(ret));
|
||||||
|
var res = {};
|
||||||
|
if (ret.peripherals.length > 0) {
|
||||||
|
ret.peripherals.forEach(p => {
|
||||||
|
if (p.name) {
|
||||||
|
console.log(p.name)
|
||||||
|
}
|
||||||
|
if (p.name && p.name.startsWith("BLUE2USART")) {
|
||||||
|
// console.log(p.name)
|
||||||
|
// console.log(p.uuid)
|
||||||
|
res = p
|
||||||
|
callback(res)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
callback(res)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function connect(param, callback) {
|
||||||
|
ble.connect(param, function (ret, err) {
|
||||||
|
if (ret.status) {
|
||||||
|
console.log("连接成功!");
|
||||||
|
} else {
|
||||||
|
console.log("连接失败!");
|
||||||
|
}
|
||||||
|
callback(ret.status)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function disconnect(param, callback) {
|
||||||
|
ble.disconnect(param, function (ret, err) {
|
||||||
|
if (ret.status) {
|
||||||
|
console.log("断开连接成功!");
|
||||||
|
} else {
|
||||||
|
console.log("断开连接失败!");
|
||||||
|
}
|
||||||
|
callback(ret)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function discoverService(param, callback) {
|
||||||
|
ble.discoverService(param, function (ret) {
|
||||||
|
var res = '';
|
||||||
|
if (ret.services.length > 0) {
|
||||||
|
console.log("发现服务成功!");
|
||||||
|
ret.services.forEach(s => {
|
||||||
|
if (s.startsWith("0000ffe")) {
|
||||||
|
callback(s)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
console.log("发现服务失败!");
|
||||||
|
callback(res)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function discoverCharacteristics(param, callback) {
|
||||||
|
ble.discoverCharacteristics(param, function (ret) {
|
||||||
|
var res = '';
|
||||||
|
if (ret.characteristics.length > 0) {
|
||||||
|
console.log("发现特征成功!");
|
||||||
|
ret.characteristics.forEach(s => {
|
||||||
|
if (s.serviceUUID === param.serviceUUID) {
|
||||||
|
callback(s.uuid)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
console.log("发现特征失败!");
|
||||||
|
callback(res)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function setNotify(param, callback) {
|
||||||
|
ble.setNotify(param, function (ret) {
|
||||||
|
// console.log(JSON.stringify(ret))
|
||||||
|
var value = ret.characteristic.value;
|
||||||
|
if (ret && value.startsWith('ff01')) {
|
||||||
|
var wei = value.substr(14, 12);
|
||||||
|
wei = Math.round(Number(hex2a(wei)))
|
||||||
|
}
|
||||||
|
callback(wei)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function writeValueForCharacteristic(param) {
|
||||||
|
ble.writeValueForCharacteristic(param, function (ret) {
|
||||||
|
// console.log(JSON.stringify(ret));
|
||||||
|
if (ret.status) {
|
||||||
|
// console.log("发送成功");
|
||||||
|
} else {
|
||||||
|
console.log("发送失败");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getMess(param,callback) {
|
||||||
|
var param1 = {
|
||||||
|
SF: "AA A5 ",
|
||||||
|
LEN: '1A 00 ',
|
||||||
|
DES: 'FF FF ',
|
||||||
|
SRC: '12 34 ',
|
||||||
|
TID: 'B0 A1 ',
|
||||||
|
CMD1: '38 02 ',
|
||||||
|
X: '00 00 ',
|
||||||
|
Y: '00 00 ',
|
||||||
|
WIDTH: '40 00 ',
|
||||||
|
HEIGHT: '20 00 ',
|
||||||
|
FORMAT: '00 00 01 00 ',
|
||||||
|
CNT: '04 00 ',
|
||||||
|
CRC: '00 00 ',
|
||||||
|
EF: '5A 55 ',
|
||||||
|
msg: ''
|
||||||
|
}
|
||||||
|
var newmsg = feedName
|
||||||
|
var msglen = newmsg.getByteLen();
|
||||||
|
var ttr = ''
|
||||||
|
if (Number(_planSum - _feedNum) >= 0) {
|
||||||
|
ttr = '少'
|
||||||
|
echartsType = '少'
|
||||||
|
} else {
|
||||||
|
ttr = '多'
|
||||||
|
echartsType = '多'
|
||||||
|
}
|
||||||
|
if (msglen < 8) {
|
||||||
|
for (var i = 0; i < 8 - msglen; i++) {
|
||||||
|
newmsg = newmsg + ' '
|
||||||
|
}
|
||||||
|
newmsg = newmsg + ttr
|
||||||
|
} else {
|
||||||
|
newmsg = feedName + ttr
|
||||||
|
}
|
||||||
|
// param1.msg = newmsg + Math.abs(Number(_planSum - _feedNum)).toFixed(0) + 'kg'
|
||||||
|
param1.msg = newmsg + Math.abs(Number(param.value)).toFixed(0) + 'kg'
|
||||||
|
// console.log(param1.msg)
|
||||||
|
zfk.getMsg(param1, function (ret, err) {
|
||||||
|
var str = ret.sendMsg;
|
||||||
|
str = str.replace(/\s+/g, "")
|
||||||
|
const num = str.length / 2;
|
||||||
|
param.value = "FF0500" + num.toString(16) + str;
|
||||||
|
writeValueForCharacteristic(param,function (ret) {
|
||||||
|
callback(ret)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in New Issue