Files
Wx_BLWConfigTools_V01_Prod/pages/HostUpgrade/HostUpgrade.js
2025-12-11 09:50:45 +08:00

421 lines
10 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const app = getApp()
function inArray(arr, key, val) {
for (let i = 0; i < arr.length; i++) {
if (arr[i][key] === val) {
return i;
}
}
return -1;
}
// ArrayBuffer转16进度字符串示例
function ab2hex(buffer) {
var hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function (bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join('');
}
Component({
properties: {
paramA: Number,
paramB: String,
// 定义属性,属性值可以是任意类型
myProperty: {
type: Array,
value: {}
}
},
data: {
devices: [],
connected: false,
chs: [],
TimerNumber:0,
myArray: [],
index: null,
firmwareName:"这里显示文件名称",
devicedisplay:0,
dat_filename:"",
dat_filePath:"",
hex_filename:"",
hex_filePath:"",
},
methods: {
onLoad: function() {
this.data.paramA // 页面参数 paramA 的值
this.data.paramB // 页面参数 paramB 的值
},
bindPickerChange: function(e) {
console.log('picker发送选择改变携带值为', e.detail.value)
this.setData({
index: e.detail.value
})
},
// 初始化蓝牙
openBluetoothAdapter() {
wx.openBluetoothAdapter({
success: (res) => {
console.log('openBluetoothAdapter success', res)
this.startBluetoothDevicesDiscovery()
},
fail: (res) => {
if (res.errCode === 10001) {
wx.onBluetoothAdapterStateChange(function (res) {
console.log('onBluetoothAdapterStateChange', res)
if (res.available) {
this.startBluetoothDevicesDiscovery()
}
})
}
}
})
var _this =this;
let num =setTimeout(function(){
_this.stopBluetoothDevicesDiscovery()
},20000)
// console.log('setTimeout'+num)
this.setData({
TimerNumber: num,
})
// console.log('setTimeout'+this.data.TimerNumber)
},
startBluetoothDevicesDiscovery() {
console.log('startBluetoothDevicesDiscovery ')
if (this._discoveryStarted) {
return
}
this._discoveryStarted = true
wx.startBluetoothDevicesDiscovery({
allowDuplicatesKey: true,
success: (res) => {
console.log('startBluetoothDevicesDiscovery success', res)
this.onBluetoothDeviceFound()
},
fail(res) {
console.error('startBluetoothDevicesDiscovery', res)
}
})
},
closeBLEConnection() {
wx.closeBLEConnection({
deviceId: this.data.deviceId
})
this.setData({
connected: false,
chs: [],
canWrite: false,
devicedisplay:-1
})
},
onBluetoothDeviceFound() {
// console.log('onBluetoothDeviceFound ')
wx.onBluetoothDeviceFound((res) => {
// console.log('onBluetoothDeviceFound ',res)
res.devices.forEach(device => {
if (!device.name && !device.localName) {
return
}
const foundDevices = this.data.devices
const idx = inArray(foundDevices, 'deviceId', device.deviceId)
const data = {}
if (idx === -1) {
data[`devices[${foundDevices.length}]`] = device
} else {
data[`devices[${idx}]`] = device
}
this.setData(data)
})
})
},
getBluetoothAdapterState() {
wx.getBluetoothAdapterState({
success: (res) => {
console.log('getBluetoothAdapterState', res)
if (res.discovering) {
this.onBluetoothDeviceFound()
} else if (res.available) {
this.startBluetoothDevicesDiscovery()
}
}
})
},
startBluetoothDevicesDiscovery() {
console.log('startBluetoothDevicesDiscovery ')
if (this._discoveryStarted) {
return
}
this._discoveryStarted = true
wx.startBluetoothDevicesDiscovery({
allowDuplicatesKey: true,
success: (res) => {
console.log('startBluetoothDevicesDiscovery success', res)
this.onBluetoothDeviceFound()
},
fail(res) {
console.error('startBluetoothDevicesDiscovery', res)
}
})
},
stopBluetoothDevicesDiscovery() {
// console.log('setTimeout'+this.data.TimerNumber)
wx.stopBluetoothDevicesDiscovery()
if (this.data.TimerNumber!=0){
// console.log('timeoutID'+ this.data.TimerNumber)
clearTimeout(this.data.TimerNumber)
}
},
createBLEConnection(e) {
const ds = e.currentTarget.dataset
const deviceId = ds.deviceId
const name = ds.name
console.log('createBLEConnection:'+deviceId)
wx.createBLEConnection({
deviceId,
success: (res) => {
this.setData({
connected: true,
name,
deviceId,
devicedisplay:deviceId,
})
console.log('kkkkk:'+this.devicedisplay)
this.getBLEDeviceServices(deviceId)
},
fail: (res) => {
console.error('createBLEConnection', res.errMsg)
}
}
)
this.stopBluetoothDevicesDiscovery()
},
getBLEDeviceServices(deviceId) {
wx.getBLEDeviceServices({
deviceId,
success: (res) => {
console.log('getBLEDeviceServices', res.characteristics)
for (let i = 0; i < res.services.length; i++) {
if (res.services[i].isPrimary) {
this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid)
return
}
}
}
})
},
getBLEDeviceCharacteristics(deviceId, serviceId) {
wx.getBLEDeviceCharacteristics({
deviceId,
serviceId,
success: (res) => {
console.log('getBLEDeviceCharacteristics success', res.characteristics)
for (let i = 0; i < res.characteristics.length; i++) {
let item = res.characteristics[i]
if (item.properties.read) {
console.log('read characteristicId:'+item.uuid, res.errMsg)
wx.readBLECharacteristicValue({
deviceId,
serviceId,
characteristicId: item.uuid,
})
}
if (item.properties.write && item.properties.notify) {
this.setData({
canWrite: true
})
this._deviceId = deviceId
this._serviceId = serviceId
this._characteristicId = item.uuid
console.log('Write _characteristicId:'+item.uuid, res.errMsg)
// this.writeBLECharacteristicValue('11223344AABBCC')
}
if (item.properties.notify || item.properties.indicate) {
console.log('notify OR indicate characteristicId:'+item.uuid, res.errMsg)
wx.notifyBLECharacteristicValueChange({
deviceId,
serviceId,
characteristicId: item.uuid,
state: true,
})
}
}
},
fail(res) {
console.error('getBLEDeviceCharacteristics', res)
}
})
// 操作之前先监听,保证第一时间获取数据
wx.onBLECharacteristicValueChange((characteristic) => {
const idx = inArray(this.data.chs, 'uuid', characteristic.characteristicId)
const data = {}
if (idx === -1) {
data[`chs[${this.data.chs.length}]`] = {
uuid: characteristic.characteristicId,
value: ab2hex(characteristic.value)
}
} else {
data[`chs[${idx}]`] = {
uuid: characteristic.characteristicId,
value: ab2hex(characteristic.value)
}
}
// data[`chs[${this.data.chs.length}]`] = {
// uuid: characteristic.characteristicId,
// value: ab2hex(characteristic.value)
// }
this.setData(data)
})
},
stringToBytes : function(strtxt) {
return new TextEncoder().encode(strtxt)
},
stringToByteArray:function (str) {
console.log('str'+str)
var array = new Uint8Array(str.length);
for (var i = 0; i < str.length; i++) {
array[i] = str.charCodeAt(i);
console.log('array[i]'+array[i])
}
return array;
} ,
chooseMessageFile(e){
const ds = e.currentTarget.dataset;
const name = ds.name;
const _this =this
let array = [name]
console.log(_this.properties.paramA)
console.log(app.globalData.roomIDName.length)
console.log(_this.data.myArray.length)
console.log(_this.properties.myProperty.length)
wx.chooseMessageFile({
count: 1,
type:'file',
extension:array,
success(res){
console.log(res)
if (name === '.hex')
{
console.log('chooseMessageFile True HEX:'+res.tempFiles[0].path)
_this.setData({
hex_filename:res.tempFiles[0].name,
hex_filePath:res.tempFiles[0].path,
})
}
else if (name === '.dat') {
console.log('chooseMessageFile True DAT:'+res.tempFiles[0].path)
_this.setData({
dat_filename:res.tempFiles[0].name,
dat_filePath:res.tempFiles[0].path,
})
}
},
fail (res) {
console.log('Failed to get file', res.errMsg)
}
})
},
writeBLECharacteristicValue(sendstr) {
// 向蓝牙设备发送一个0x00的16进制数据
console.log(sendstr)
let datebuffer =this.stringToByteArray(sendstr);
// console.log('buffer Size:'+buffer.length)
let buffer = datebuffer.buffer
console.log('datebuffer'+datebuffer)
// let dataView = new Uint8Array(buffer)
// dataView.a
console.log('buffer.leng'+buffer.byteLength)
console.log('buffer.leng'+buffer)
// dataView.setUint8(1,11)
// dataView.setUint8(2,16)
// dataView.setUint8(3,13)
// dataView.setUint8(4,18)
// dataView.setUint8(5,12)
// dataView.setUint8(6,24)
// dataView.setUint8(7,54)
// dataView.setUint8(8,34)
console.log('UUID '+this._characteristicId)
wx.writeBLECharacteristicValue({
deviceId: this._deviceId,
serviceId: this._serviceId,
characteristicId: this._characteristicId,
value: buffer,
success (res) {
console.log('writeBLECharacteristicValue success', res.errMsg)
},
fail (res) {
console.log('writeBLECharacteristicValue fail', res.errMsg)
}
})
},
writeBLECharacteristicValue1(){
this.writeBLECharacteristicValue('1233456789a')
},
closeBluetoothAdapter() {
wx.closeBluetoothAdapter()
this._discoveryStarted = false
},
},
ready: function () {
console.log(app.globalData.roomIDName)
this.setData({
myArray:app.globalData.roomIDName
})
console.log( this.data.myArray)
},
// 蓝牙操作
})