const util = require("../../utils/util"); const { inArray, ab2hex, toHex, gethms } = util Page({ data: { devices: [], connected: false, name: '', Mode: "", text: "", weight: "", kcal: "", uuid1: "", uuid2: "", uuid3: "", active: "", timeList: [], deviceId: null, serviceId: null }, onLoad: function() { let that = this that.data.timeList = gethms() that.data.time_m = that.data.timeList[0][1].substring(0, 2) that.data.time_s = that.data.timeList[1][0].substring(0, 2) }, // 初始化蓝牙模块 openBluetoothAdapter() { wx.openBluetoothAdapter({ success: (res) => { console.log('openBluetoothAdapter success', res) wx.showToast({ title: '蓝牙搜索中', icon: "none" }) this.startBluetoothDevicesDiscovery() }, fail: (res) => { if (res.errCode === 10001) { wx.showToast({ title: '请打开蓝牙', icon: "none" }) } } }) }, // 开始搜寻附近的蓝牙外围设备 startBluetoothDevicesDiscovery() { wx.startBluetoothDevicesDiscovery({ allowDuplicatesKey: true, interval: 200, //上报设备的间隔 services: [], success: (res) => { console.log('startBluetoothDevicesDiscovery success', res) this.onBluetoothDeviceFound() }, }) }, // 停止搜寻附近的蓝牙外围设备 stopBluetoothDevicesDiscovery() { wx.stopBluetoothDevicesDiscovery() }, // 找到新设备的事件 onBluetoothDeviceFound() { wx.onBluetoothDeviceFound((res) => { res.devices.forEach(device => { if (!device.name && !device.localName) { return } if (device.name.indexOf('YPC') != -1) { wx.stopBluetoothDevicesDiscovery() const foundDevices = this.data.devices const idx = inArray(foundDevices, 'deviceId', device.deviceId) const data = {} const tempMac = device.name.substring(7, 19) device.macAddr = tempMac.substring(0, tempMac.length - 1) .replace(/(.{2})(?!$)/g, '$1:') + tempMac.substring(tempMac.length - 1); if (idx === -1) { data[`devices[${foundDevices.length}]`] = device } else { data[`devices[${idx}]`] = device } this.setData(data) } }) }) }, // 连接低功耗蓝牙设备 createBLEConnection(e) { this._connLoading = true wx.showLoading({ title: '连接中', }) setTimeout(() => { if (this._connLoading) { this._connLoading = false wx.hideLoading() } }, 6000) const ds = e.currentTarget.dataset const index = ds.index this._device = this.data.devices[index] const deviceId = ds.deviceId const name = ds.name this.mac = ds.mac wx.createBLEConnection({ deviceId, success: (res) => { this.setData({ connected: true, name, deviceId, }) console.log("createBLEConnection:success") this.onBLEConnectionStateChange() this.getBLEDeviceServices(deviceId) }, fail: res => { this._connLoading = false wx.hideLoading() wx.showToast({ title: '连接失败', icon: 'none' }) } }) }, //监听蓝牙连接状态 onBLEConnectionStateChange() { wx.onBLEConnectionStateChange((res) => { if (!res.connected) { setTimeout(() => { wx.showToast({ title: '连接已断开', icon: 'none' }) }, 500) this.setData({ devices: [], weight: "", text: "", Mode: "", kcal: "", }) } }) }, // 获取蓝牙设备的 serviceId getBLEDeviceServices(deviceId) { wx.getBLEDeviceServices({ deviceId, success: (res) => { for (let i = 0; i < res.services.length; i++) { let service = res.services[i]; if (service.uuid.indexOf('FFE0') > -1) { this.data.serviceId = service.uuid this.data.deviceId = deviceId this.getBLEDeviceCharacteristics(deviceId, service.uuid) wx.showLoading({ title: '获取设备的UUID成功', }) return } } } }) }, // 获取蓝牙设备某个服务中所有特征值(characteristic) /** * read: true读,write: true写,notify: true广播 */ getBLEDeviceCharacteristics(deviceId, serviceId) { let that = this this._deviceId = deviceId this._serviceId = serviceId this._device.serviceId = serviceId wx.showLoading({ title: '连接成功', }) wx.getBLEDeviceCharacteristics({ deviceId, serviceId, success: (res) => { console.log("服务的特征值成功:", res) for (let i = 0; i < res.characteristics.length; i++) { let item = res.characteristics[i]; if (item.uuid.indexOf('0000FFE4') != -1) { that.data.uuid1 = item.uuid //主服务 UUID } else if (item.uuid.indexOf('0000FF12') != -1) { that.data.uuid2 = item.uuid //写入设置 } } setTimeout(function() { //下发密码指令 let j = Number(165 + 10 + 1 + 8 + 8 + 8 + 8 + 8 + 8).toString(16) let str = "A50A01080808080808" + j.substr(j.length - 2, 2) that.SendData(str) }, 300) setTimeout(function() { //设置体重指令 默认100斤 let num = parseInt(100).toString(); let m = Number(165 + 5 + 8 + Number(num)).toString(16) let send = "A50508" + Number(num).toString(16) + m.substr(m.length - 2, 2) that.SendData(send) }, 600) wx.notifyBLECharacteristicValueChange({ deviceId, serviceId, characteristicId: that.data.uuid1, state: true, }) wx.notifyBLECharacteristicValueChange({ deviceId, serviceId, characteristicId: that.data.uuid2, state: true, }) that.notifyBLECharacteristicValue() }, fail(res) { console.error('getBLEDeviceCharacteristics', res) } }) }, notifyBLECharacteristicValue() { let that = this; wx.notifyBLECharacteristicValueChange({ state: true, // 启用 notify 功能 deviceId: that.data.deviceId, serviceId: that.data.serviceId, characteristicId: that.data.uuid1, success(res) { wx.onBLECharacteristicValueChange((characteristic) => { let value = ab2hex(characteristic.value, ""); let count = parseInt(value.substring(8, 12), 16) let Ycount = parseInt(value.substring(12, 16), 16) //设置次数 let time = parseInt(value.substring(16, 20), 16) //运行时间/秒 let timeDown = parseInt(value.substring(20, 24), 16) //倒计时时间 let type = parseInt(value.substring(30, 32), 16) //当前状态 let weight = parseInt(value.substring(32, 34), 16) //重量 let kcal = parseInt(value.substring(34, 38), 16) / 10 //卡路里 let minutes = 0 let seconds = 0 let time_m = "00" let time_s = "00" let j = null let str = null if (value == '5a05090169') { //模式设置成功 setTimeout(function() { j = Number(165 + 5 + 5).toString(16) str = "A5050500" + j.substr(j.length - 2, 2) that.SendData(str) //开始 }, 900) setTimeout(function() { j = Number(165 + 5 + 3).toString(16) str = "A5050300" + j.substr(j.length - 2, 2) that.SendData(str) //连续 }, 1200) } if (value == '5a05080168') { setTimeout(function() { //自由模式 that.data.Mode = "自由模式" j = Number(165 + 8 + 9).toString(16) str = "A5080900000000" + j.substr(j.length - 2, 2) that.SendData(str) //连续 console.log("模式模式") }, 300) } console.log("value", value, type, that.data.active) if (type == 0 || type == 4) { if (that.data.active != 2 && count != 0) { //自由模式 + 计数 minutes = Math.floor((time % 3600) / 60) seconds = time % 60 } if (that.data.active == 2) { //计时 let T = Number(timeDown) - Number(time) minutes = Math.floor((T % 3600) / 60) seconds = T % 60 } time_m = minutes > 9 ? minutes : '0' + minutes time_s = seconds > 9 ? seconds : '0' + seconds that.setData({ Mode: that.data.Mode, weight: "跳绳个数:" + count, text: "时长:" + time_m + ':' + time_s, kcal: "消耗卡路里:" + Math.floor(kcal), }) } }) } }) }, // 模式切换 handleStart(e) { let that = this let m = null let send = null console.log("ind", ind) that.data.active = e.currentTarget.dataset.name let ind = e.currentTarget.dataset.name if (ind == 1) { // 1自由 m = Number(165 + 8 + 9).toString(16) send = "A5080900000000" + m.substr(m.length - 2, 2) that.data.Mode = "自由模式" } if (ind == 2) { //2定时 默认60秒 m = Number(165 + 8 + 9 + 60).toString(16) send = "A508090000" + toHex(60, 4) + m.substr(m.length - 2, 2) that.data.Mode = "定时模式" } if (ind == 3) { //3定数 默认50个 m = Number(165 + 8 + 9 + 50).toString(16) send = "A50809" + toHex(50, 4) + "0000" + m.substr(m.length - 2, 2) that.data.Mode = "定数模式" } console.log("send", send) that.SendData(send) that.stopBluetoothDevicesDiscovery() }, SendData(str) { let that = this let buf = new Uint8Array(str.match(/[\da-f]{2}/gi).map(function(h) { return parseInt(h, 16) })) wx.writeBLECharacteristicValue({ deviceId: that.data.deviceId, serviceId: that.data.serviceId, characteristicId: that.data.uuid2, value: buf.buffer, success: res => { console.log('下发指令', res.errMsg) wx.showToast({ title: '下发指令成功', duration: 2000, icon: 'none' }) }, fail: res => { wx.showToast({ title: '下发指令失败', duration: 2000, icon: 'none' }) console.log("失败", res); }, }) }, /** * 断开蓝牙模块 */ closeBluetoothAdapter() { wx.closeBluetoothAdapter() this._discoveryStarted = false wx.showToast({ title: '断开蓝牙模块', icon: 'none' }) this.setData({ devices: [], weight: "", text: "", Mode: "", kcal: "", }) }, // 断开与低功耗蓝牙设备的连接 closeBLEConnection() { wx.closeBLEConnection({ deviceId: this._deviceId }) wx.showToast({ title: '断开蓝牙连接', icon: 'none' }) this.setData({ devices: [], weight: "", text: "", Mode: "", kcal: "", }) }, });