1.安卓和ios差异
微信小程序项目是扫码获取蓝牙连接设备的信息,可以拿到设备的deviceId,但是安卓和IOS的deviceId格式不一样,最好是通过蓝牙名字查 询获取deviceId
2.代码
<template>
<view class="index-container">
<view class="hmtips" v-if="tipShow && !hasConnectBTFlag" @click="toHarmonyOs">
<view>
<u-icon name="info-circle" size="35rpx"></u-icon>
</view>
<view>鸿蒙系统蓝牙无法连接的权限指导说明</view>
<view style="margin-left: 50rpx;">
<u-icon name="arrow-right" index="1" size="30rpx"></u-icon>
</view>
<view>
<u-icon name="close-circle" size="60rpx" color="#6F727A" index="2" @tap.stop="closeTips"></u-icon>
</view>
</view>
<!-- 蓝牙连接状态 -->
<view class="bluethoothStatus" v-if="0">
<view>
<!-- 蓝牙连接成功 -->
<u-image v-if="hasConnectBTFlag" :show-loading="false" width="100%" height="100%"
src="https://img.cdn.wxzhida.cn/lingpao-ui/%E8%93%9D%E7%89%99%E8%BF%9E%E6%8E%A5%E6%88%90%E5%8A%9F.png">
</u-image>
<!-- 蓝牙连接失败 -->
<u-image v-else :show-loading="false" width="100%" height="100%"
src="https://img.cdn.wxzhida.cn/lingpao-ui/%E8%93%9D%E7%89%99%E8%BF%9E%E6%8E%A5%E5%A4%B1%E8%B4%A5%20%281%29.png">
</u-image>
</view>
<view :style="{ color: hasConnectBTFlag ? '#1890FF' : '#CDCDCD' }">{{hasConnectBTFlag ? '蓝牙已连接':'蓝牙未连接'}}
</view>
</view>
<!-- 蓝牙已连接 -->
<block v-if="hasConnectBTFlag">
<!-- 充电中显示 -->
<view class="charging" v-if="hasChargeFlag">
<view class="charing-animate"></view>
<view class="charge-text">充电中</view>
</view>
<!-- 待机显示 其他状态-->
<view class="errStatus" v-if="pileErrorFlag && !hasChargeFlag && !connectionFlag">
<view class="ring">故障中</view>
</view>
<view class="otherStatus"
v-if="(!pileErrorFlag && !hasChargeFlag && !connectionFlag && !waitChargeFlag) || (version == '010229' && !hasChargeFlag && !pileErrorFlag && !waitChargeFlag)">
<view class="ring">待机中</view>
</view>
<view class="otherStatuse" v-if="connectionFlag && !hasChargeFlag && version !='010229'">
<view class="ring">车枪已连接</view>
</view>
<view class="otherStatus" v-if="!hasChargeFlag && waitChargeFlag">
<view class="ring">充电等待</view>
</view>
</block>
<!-- 蓝牙未连接有设备-->
<block v-if="!hasConnectBTFlag && hasEquipFlag">
<view class="ont-connected">
<view class="rings">蓝牙未连接</view>
</view>
</block>
<!-- 蓝牙未连接无设备 -->
<block v-if="!hasConnectBTFlag && !hasEquipFlag">
<view class="no-bind">
<view class="rings">暂无绑定设备</view>
</view>
</block>
<!-- 蓝牙连接成功按钮 -->
<block v-if="hasConnectBTFlag">
<!-- 开始充电按钮 蓝牙模式显示 chargeMode =='00'蓝牙模式 equipBaseInfo.chargeMode =='01' 充电模式-->
<view class="startCharge" v-if="equipBaseInfo.chargeMode =='00' && !hasChargeFlag"
@click="$u.throttle(startCharge,2000)">
<view></view>
</view>
<!-- 即插即充模式占位 01-->
<view style="height: 150rpx;margin: 106rpx auto 0;" v-if="equipBaseInfo.chargeMode =='01'"></view>
<!-- 停止充电按钮 蓝牙模式显示 00-->
<view class="offCharge" v-if="equipBaseInfo.chargeMode =='00' && hasChargeFlag"
@click="$u.throttle(offCharge,3000)">
<view></view>
</view>
</block>
<!-- 蓝牙连接失败按钮 -->
<block v-if="!hasConnectBTFlag && hasEquipFlag">
<view class="toContect" @click="$u.throttle(restContect, 6000)">
<view></view>
</view>
<view class="contect-text">
<view style="margin-right: 10rpx;" v-if="0">
<u-icon name="arrow-upward" color="#1890FF"></u-icon>
</view>
<view style="color: #1890FF;">请确保您在设备附近,点击上方按钮重新连接</view>
</view>
</block>
<!-- 未绑定设备去绑定设备 -->
<block v-if="!hasConnectBTFlag && !hasEquipFlag">
<view class="toBind" @click="goBind">
<view></view>
</view>
<view class="contect-text">
<view style="margin-right: 10rpx;" v-if="0">
<u-icon name="arrow-upward" color="#1890FF"></u-icon>
</view>
<view style="color: #1890FF;">点击上方按钮绑定设备</view>
</view>
</block>
<!-- 操作面板 -->
<view :class="[hasConnectBTFlag?'marg100':'marg75','panel']">
<view class="btn">
<view class="switch" @click="$u.throttle(switchType,2000)">
<view :class="[equipBaseInfo.chargeMode =='00'? 'blueThtooth':'plug']"></view>
<view :class="[equipBaseInfo.chargeMode =='00'? 'cutA':'cutB','cut']">切换</view>
<view class="switch_describe">{{equipBaseInfo.chargeMode =='00' ?'蓝牙充电模式':'即插即充模式'}}</view>
</view>
<view class="bind-squre" @click="goBind">
<view :class="[equipBaseInfo.chargeMode =='00' ?'bind_item':'bind_items']"></view>
<view class="device_describe">{{hasEquipFlag == false ? '设备绑定':'设备列表'}}</view>
</view>
<view class="device" @click="goDeviceDetail">
<view :class="[equipBaseInfo.chargeMode =='00' ?'device_item':'device_items']"></view>
<view class="device_describe">设备详情</view>
</view>
</view>
<view class="tips">
<view>
<view>{{formatChargeVal(equipBaseInfo.powerCount)}}<text class="smallFont">KW</text></view>
<view class="marg">实时功率</view>
</view>
<view class="center-tips">
<view>{{formatChargeVal(equipBaseInfo.currentChargeIVal)}}<text class="smallFont">A</text></view>
<view class="marg">实时电流</view>
</view>
<view>
<view>{{formatChargeVal(equipBaseInfo.chargeV)}}<text class="smallFont">V</text></view>
<view class="marg">实时电压</view>
</view>
</view>
</view>
<!-- 开启附近设备权限提示 -->
<u-modal v-model="modalShow" width="70%" title="蓝牙权限调用失败" content="请点击权限设置按钮,在微信权限设置页打开「附近设备」权限。"
confirm-text="权限设置" show-cancel-button @confirm="openSetting">
</u-modal>
<!-- 提示华为手机 -->
<u-modal v-model="tipModalShow" width="70%" title="关闭提示" content="关闭后将不再显示该提示,您可在「设备详情-使用手册」中查看该说明。"
confirm-text="确定" show-cancel-button @confirm="closeTip">
</u-modal>
<!-- 提示 -->
<u-toast ref="uToast" />
</view>
</template>
<script>
import blueteeth from '../../libs/blueteeth.js';
import tuiSwipeAction from "@/components/tui-swipe-action/tui-swipe-action.vue"
import blueteethCmd from '../../libs/blueteeth-cmd.js';
import store from '../../libs/store.js';
import utils from '../../libs/utils.js';
import config from '../../libs/config.js';
import {
throttle
} from "../../libs/throttle.js"; //引入节流函数
export default {
data() {
return {
pileStatus: '',
version: '',
connectionFlag: false, //车枪连接
hasEquipFlag: false, //是否绑桩
hasConnectBTFlag: false, //有没有连上蓝牙Flag
hasChargeFlag: false, //是否充电中
equipBaseInfo: {
chargeMode: '01'
}, //设备信息
deviceId: '',
deviceName: '',
canUseBlueteethFlag: false, //能否使用蓝牙
serviceId: "0000FFF0-0000-1000-8000-00805F9B34FB", // 服务ID
readCharacteristicId: "0000FFF1-0000-1000-8000-00805F9B34FB",
writeCharacteristicId: "0000FFF2-0000-1000-8000-00805F9B34FB",
statusLooper: null, //轮训查询桩状态定时器
mode: 1, //模式 1蓝牙模式 2 即插即充模式
charging: 1, // 1充电中 2 其他状态
pileErrorFlag: false, //设备故障
waitChargeFlag: false, //充电等待中
wirte0032CodeMsgId: '', // 写入的消息ID
wirte0041CodeMsgId: '', // 写入的消息ID
looperFlag: true,
currenTempChargeMode: '',
restFlag: false,
mtuTimer: null,
findList: [],
modalShow: false,
tipShow: false,
tipModalShow: false,
}
},
onShow() {
if (!uni.getStorageSync('hasLogin') == true) {
uni.reLaunch({
url: '/pages/login/index'
})
} else {
const deviceInfo = wx.getDeviceInfo()
console.log(deviceInfo)
if (deviceInfo.brand == 'HUAWEI') { // 华为手机显示提示
if (uni.getStorageSync('hasTip') == true) {
this.tipShow = false;
} else {
this.tipShow = true;
}
}
this.bluetoothJudgment()
}
},
onHide() {
clearTimeout(this.statusLooper)
clearInterval(this.mtuTimer)
this.statusLooper = null;
this.mtuTimer = null;
},
onUnload() {
clearTimeout(this.statusLooper)
clearInterval(this.mtuTimer)
this.statusLooper = null;
this.mtuTimer = null;
},
methods: {
closeTips() {
console.log('点击了关闭')
this.tipModalShow = true;
},
closeTip() {
this.tipShow = false;
this.tipModalShow = false;
uni.setStorageSync('hasTip', true)
},
/*安卓14打开发现附近的设备*/
openSetting() {
uni.openAppAuthorizeSetting({
success(res) {
console.log(res)
}
})
},
/*判断用户有没有授权蓝牙*/
bluetoothJudgment() {
let _ = this;
wx.getSetting({
success(res) {
let authSetting = res.authSetting;
if (authSetting['scope.bluetooth']) { //已授权
_.pageInitFun()
} else if (authSetting['scope.bluetooth'] === false) {
wx.showModal({
title: '您未开启蓝牙授权',
content: '请在系统设置中打开蓝牙授权,以便我们为您提供更好的服务',
success: (res) => {
if (res.confirm) {
wx.openSetting()
}
}
})
} else {
wx.authorize({
scope: 'scope.bluetooth',
success: () => {
_.pageInitFun()
},
fail: () => {
wx.showModal({
title: '您未开启蓝牙授权',
content: '请在系统设置中打开蓝牙授权,以便我们为您提供更好的服务',
success: (res) => {
if (res.confirm) {
wx.openSetting()
}
}
})
}
})
}
}
})
},
/* 页面初始化*/
pageInitFun() {
console.log('页面初始化')
let that = this;
this.hasEquipFlag = false;
// this.hasConnectBTFlag = false;
this.checkCurEquipInfo();
},
/*判断是否有设备*/
checkCurEquipInfo() {
let that = this;
this.hasEquipFlag = false;
// let obj = store.getEquipBaseInfo() || {}
// if (uni.getStorageSync('hasConnectBTFlag')) { //缓存有值
// this.equipBaseInfo = store.getEquipBaseInfo()
// this.hasEquipFlag = true;
// this.checkBlueteethFun();
// } else {
that.$u.api.getDefaultPile({
accessType: 'app',
data: {
userId: uni.getStorageSync('userId'),
defaultStake: 1,
mobile: uni.getStorageSync('mobile')
}
}).then(res => {
console.log('接口查询了有没有绑桩')
console.log(res.data)
console.log('接口查询了有没有绑桩')
if (res.data != null) { //有设备
let obj = store.getEquipBaseInfo()
if (obj.deviceId && obj.deviceId != '' && obj.deviceId == res.data.deviceId) {
that.equipBaseInfo = store.getEquipBaseInfo()
console.log('使用缓存数据XXXX')
console.log(store.getEquipBaseInfo())
console.log(this.equipBaseInfo.chargeMode)
console.log('使用缓存数据XXXX')
} else {
that.equipBaseInfo = res.data
store.setEquipBaseInfo(res.data);
}
// that.equipBaseInfo = res.data[0]
// store.setEquipBaseInfo(res.data[0]);
uni.setNavigationBarTitle({
title: that.equipBaseInfo.personalityName || ''
});
that.hasEquipFlag = true;
that.deviceId = that.equipBaseInfo.deviceId
that.deviceName = that.equipBaseInfo.deviceName
console.log('this.deviceId')
console.log(that.deviceId)
console.log(that.deviceName)
console.log('this.deviceId')
if (uni.getSystemInfoSync().platform === 'android') {
uni.setStorageSync('currentDeviceId', that.deviceId)
}
that.checkBlueteethFun();
} else { //无设备
that.hasConnectBTFlag = false;
that.hasEquipFlag = false;
store.setEquipBaseInfo({})
}
}).catch(err => {
uni.reLaunch({
url: '/pages/login/index'
})
})
},
/*蓝牙初始化 new*/
checkBlueteethFun() {
let _ = this;
_.canUseBlueteethFlag = false;
wx.openBluetoothAdapter({
success(res) {
console.log('蓝牙初始化成功哟')
console.log(uni.getStorageSync('currentServiceId'))
if (uni.getStorageSync('currentServiceId') != '') {
_.getConnectedBluetoothDevices() // 监听蓝牙是否已经连接
} else {
uni.showLoading({
title: '蓝牙连接中...',
mask: true
})
if (uni.getSystemInfoSync().platform === 'android') {
console.log('安卓直接连接')
console.log(this.deviceId)
uni.setStorageSync('currentDeviceId', this.deviceId)
_.toConnectEquip()
} else { // ios开始搜索
_.find() // 开始搜索蓝牙
}
}
},
fail(err) {
console.log('vvvv')
console.log(err)
console.log('vvvv')
if (wx.getSystemInfoSync().platform === 'ios' && err.state == 3) {
utils.errMsg('请到设置开启微信蓝牙授权')
} else {
utils.errMsg('请打开手机蓝牙后重试')
}
_.canUseBlueteethFlag = false;
_.hasConnectBTFlag = false;
}
})
},
/*---------查找附近的蓝牙设备----------*/
find() {
let _ = this;
console.log('蓝牙开始查找')
console.log('蓝牙名字=' + _.deviceName)
_.findList = []
wx.startBluetoothDevicesDiscovery({
services: [],
success: function() {
console.log('开始搜索蓝牙')
wx.onBluetoothDeviceFound(function(res) {
console.log(res, '搜索蓝牙相关设备')
for (let i = 0; i < res.devices.length; i++) {
// app.globalData.bleConfig.name 这里提前就获取了设备的name
_.findList.push(res.devices[i])
if (res.devices[i].name == _.deviceName) {
var deviceId = res.devices[i].deviceId
console.log('找到设备')
console.log(res.devices[i])
_.deviceId = res.devices[i].deviceId
uni.setStorageSync('currentDeviceId', _.deviceId)
console.log('找到设备')
// ios搜索出目标设备后,将停止搜索,否则影响手机性能
wx.stopBluetoothDevicesDiscovery({
success: function() {
_.toConnectEquip()
_.onBLEConnectionStateChange() // 监听蓝牙连接状态
}
})
}
}
})
},
fail: function(err) {
console.log('没有找到设备')
console.log(err)
console.log('没有找到设备')
wx.stopBluetoothDevicesDiscovery({
success: function() {
},
complete: function() {
_.connectErrorFun() // 关闭蓝牙连接
}
})
}
})
let timers = setTimeout(() => {
wx.stopBluetoothDevicesDiscovery({
success: function() {}
})
console.log('真的没有找到设备')
console.log(_.findList.length)
console.log('deviceId=' + _.deviceId)
console.log('真的没有找到设备')
if (_.findList.length == 0) {
if (uni.getSystemInfoSync().platform === 'android') {
_.modalShow = true;
uni.openAppAuthorizeSetting({
success(res) {
console.log(res)
}
})
}
}
uni.hideLoading()
clearTimeout(timers)
timers = null;
}, 12000)
},
/* 根据主服务 UUID 获取已连接的蓝牙设备*/
getConnectedBluetoothDevices() {
let _ = this;
wx.getConnectedBluetoothDevices({
services: [uni.getStorageSync('currentServiceId')],
success(res) {
console.log('kkkkk')
console.log(res)
console.log(uni.getStorageSync('currentDeviceId'))
console.log('kkkkk')
if (res.devices.length == 0) { // 没有设备连接
uni.showLoading({
title: '蓝牙连接中...',
mask: true
})
if (uni.getSystemInfoSync().platform === 'android') {
console.log('安卓直接连接2222')
console.log(this.deviceId)
_.toConnectEquip()
} else { // ios开始搜索
_.find() // 开始搜索蓝牙
}
} else {
for (let item of res.devices) {
if (item.deviceId == uni.getStorageSync('currentDeviceId')) {
_.hasConnectBTFlag = true;
uni.setStorageSync('hasConnectBTFlag', true);
uni.hideLoading()
_.notifyBLECharacter()
_.writePileStatusCode()
}
}
}
}
})
},
/*监听蓝牙低功耗连接状态改变事件*/
onBLEConnectionStateChange() {
wx.onBLEConnectionStateChange(function(res) {
// 该方法回调中可以用于处理连接意外断开等异常情况
console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`)
})
},
/*连接蓝牙设备*/
toConnectEquip() {
let _ = this;
console.log('deviceId=' + _.deviceId)
wx.createBLEConnection({
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId: _.deviceId,
// timeout: 10000,
success(res) {
console.log('连接蓝牙成功==suc')
_.getEquipServices();
},
fail(err) {
console.log('连接蓝牙失败==fail')
console.log(err)
// if (err.errCode == 10003) {
// console.log('返回的errCode是' + err.errCode)
// console.log(err.errCode)
// _.find()
// }
_.connectErrorFun();
},
complete(err) {
console.log(err)
}
})
},
/*获取服务信息*/
getEquipServices() {
let _ = this;
uni.getBLEDeviceServices({
deviceId: this.deviceId, // 设备ID,在上一步【4】里获取
success(res) {
_.serviceId = res.services[0].uuid;
uni.setStorageSync('currentServiceId', _.serviceId)
console.log('serviceId=' + _.serviceId)
// _.equipBaseInfo.serviceId = _.serviceId;
// store.setEquipBaseInfo(_.equipBaseInfo);
if (_.serviceId) {
_.getEquipCharacteristics();
}
},
fail(err) {
_.connectErrorFun();
}
})
},
/*获取特征值*/
getEquipCharacteristics() {
let _ = this;
uni.getBLEDeviceCharacteristics({
deviceId: _.deviceId, // 设备ID,在【4】里获取到
serviceId: _.serviceId, // 服务UUID,在【6】里能获取到
success(res) {
console.log('获取服务特征值成功')
console.log(res)
console.log('获取服务特征值成功')
_.notifyEquip();
},
fail(err) {
_.connectErrorFun();
}
})
},
/*开启监听蓝牙传值*/
notifyEquip() {
let _ = this;
console.log('*************************')
if (uni.getSystemInfoSync().platform === 'android') {
uni.setBLEMTU({
deviceId: _.deviceId, // 设备ID,在【4】里获取到
mtu: 512, // 服务UUID,在【6】里能获取到
success(res) {
console.log('设置mtu成功')
wx.getBLEMTU({
deviceId: _.deviceId,
writeType: 'write',
success(res) {
console.log(res)
}
})
_.notifyBLECharacter()
},
fail(err) {
console.error(err)
_.setAndroidMTU() // 重要代码如果失败继续往下连接然后重复设置
}
})
} else {
_.notifyBLECharacter()
}
},
/*处理设置失败安卓协商低功耗最大传输单元*/
setAndroidMTU() {
let _ = this;
console.log('处理安卓协商低功耗最大传输单元失败的方法')
_.notifyBLECharacter()
_.mtuTimer = setInterval(() => {
console.log('开始循环设置MTU值')
uni.setBLEMTU({
deviceId: _.deviceId, // 设备ID,在【4】里获取到
mtu: 512, // 服务UUID,在【6】里能获取到
success(res) {
console.log('设置mtu成功')
},
fail(err) {
console.error(err)
},
complete() {
wx.getBLEMTU({
deviceId: _.deviceId,
writeType: 'write',
success(res) {
console.log(res)
clearInterval(_.mtuTimer)
_.mtuTimer = null;
}
})
}
})
}, 1500)
},
notifyBLECharacter() {
let _ = this;
console.log('进入了开始监听的函数里面')
uni.notifyBLECharacteristicValueChange({
deviceId: _.deviceId, // 设备ID,在【4】里获取到
serviceId: _.serviceId, // 服务UUID,在【6】里能获取到
characteristicId: _.readCharacteristicId, // 特征值,在【7】里能获取到
state: true,
success(res) {
console.log('notifyBLECharacteristicValueChange成功监听')
_.hasConnectBTFlag = true;
_.restFlag = false;
uni.hideLoading()
uni.setStorageSync('hasConnectBTFlag', true)
// 下发获取充电桩状态数据
_.listenValueChange();
// 接受消息的方法
_.writePileStatusCode()
_.looperGetPileStatusFun()
},
fail(err) {
_.connectErrorFun();
}
})
},
/*处理桩返回的值*/
listenValueChange() {
console.log('vHHHHHHHHHHHHH')
let _ = this;
uni.onBLECharacteristicValueChange(res => {
console.log('!!!!!!!!!!!桩返回的16进制值!!!!!!!!!!')
console.log(res.value)
console.log('!!!!!!!!!!!桩返回的16进制值!!!!!!!!!!')
// 结果里有个value值,该值为 ArrayBuffer 类型,所以在控制台无法用肉眼观察到,必须将该值转换为16进制
let resHex = _.ab2hex(res.value);
_.handleEquipNotifyBackCode(resHex);
})
},
/*处理桩上传的信息*/
handleEquipNotifyBackCode(notifyCode) {
console.log('桩回复信息=' + notifyCode)
let _ = this;
let startIndex = notifyCode.indexOf('7e');
let endIndex = notifyCode.lastIndexOf('7e');
if (startIndex >= 0 && endIndex > 0) {
const cmdCodeType = notifyCode.substring(startIndex + 2, startIndex + 6);
console.log('cmdCodeType=' + cmdCodeType)
// 状态查询回调
if ('8051' == cmdCodeType) {
_.hasConnectBTFlag = true;
uni.hideLoading();
uni.setStorageSync('hasConnectBTFlag', true)
_.hasChargeFlag = false;
_.waitChargeFlag = false;
_.pileErrorFlag = false;
_.connectionFlag = false;
const pileStatusObj = blueteethCmd.parse8051Cmd(notifyCode)
console.log('pileStatusObj解析对象')
console.log(pileStatusObj)
console.log('pileStatusObj解析对象')
if (Object.keys(pileStatusObj).length === 0) { //异常数据就取缓存里的值
_.hasChargeFlag = uni.getStorageSync('hasChargeFlags')
this.equipBaseInfo = store.getEquipBaseInfo()
console.log('空对象使用缓存数据');
return
}
if (pileStatusObj.chargeStatusStr == '05') { //正在充电中
_.hasChargeFlag = true;
uni.setStorageSync('hasChargeFlags', true)
} else if (pileStatusObj.chargeStatusStr == '04') {
_.waitChargeFlag = true;
uni.setStorageSync('hasChargeFlags', false)
} else if (pileStatusObj.chargeStatusStr == '01') { //故障
_.pileErrorFlag = true;
uni.setStorageSync('hasChargeFlags', false)
} else if (pileStatusObj.chargeStatusStr == '03') { //车枪已连接
_.connectionFlag = true;
uni.setStorageSync('hasChargeFlags', false)
} else {
uni.setStorageSync('hasChargeFlags', false)
}
console.log('ccccccc')
console.log('chargeStatusStr=' + pileStatusObj.chargeStatusStr)
console.log('ccccccc')
this.pileStatus = pileStatusObj.chargeStatusStr
console.log(pileStatusObj)
this.equipBaseInfo.chargeMode = ('1' == pileStatusObj.chargeMode ? '01' : '00')
console.log('桩设置的模式=' + this.equipBaseInfo.chargeMode)
uni.setStorageSync('chargeModes', this.equipBaseInfo.chargeMode)
this.equipBaseInfo.chargeModeName = ('01' == this.equipBaseInfo.chargeMode ? '即插即充模式' : '蓝牙模式')
uni.setStorageSync('chargeModeNames', this.equipBaseInfo.chargeModeName)
//电流电压数据异常处理
if (Number(pileStatusObj.chargeV) > 400 || Number(pileStatusObj.chargeV) < 0) {
this.equipBaseInfo.chargeV = 220
} else {
this.equipBaseInfo.chargeV = pileStatusObj.chargeV; //电压
}
if (Number(pileStatusObj.chargeI) > 40 || Number(pileStatusObj.chargeI) < 0) {
this.equipBaseInfo.chargeI = 0
} else {
this.equipBaseInfo.chargeI = pileStatusObj.chargeI || 0; //电流
}
this.equipBaseInfo.currentChargeIVal = pileStatusObj.currentChargeIVal; //实时电流
this.equipBaseInfo.powerCount = (this.equipBaseInfo.currentChargeIVal * this.equipBaseInfo
.chargeV) /
1000; //功率
this.equipBaseInfo.chargePower = pileStatusObj.chargeI; //电流
this.equipBaseInfo.chargePowerName = pileStatusObj.chargeI + 'A';
this.equipBaseInfo.version = pileStatusObj.version; //新增版本号
this.equipBaseInfo.chargeStatusStr = pileStatusObj.chargeStatusStr //充电桩状态
if (pileStatusObj.version == '010229' || pileStatusObj.version == '010230') {
if (uni.getStorageSync('pileVersion') == '') {
uni.setStorageSync('pileVersion', pileStatusObj.version)
this.version = pileStatusObj.version;
} else {
// this.version = uni.getStorageSync('pileVersion')
this.version = pileStatusObj.version;
}
}
let obj = store.getEquipBaseInfo()
obj.chargeMode = this.equipBaseInfo.chargeMode;
obj.chargeStatusStr = this.equipBaseInfo.chargeStatusStr
console.log('FFFFFFFFF')
console.log(obj)
console.log('FFFFFFFFF')
store.setEquipBaseInfo(obj)
console.log('MMMMMMMMMMM')
console.log(store.getEquipBaseInfo())
console.log('MMMMMMMMMMM')
uni.setStorageSync('MaxChargePowerName', pileStatusObj.chargeI)
} else if ('8032' == cmdCodeType) {
_.wirte0032CodeMsgId = '';
// 充电回调
const chargeRes = blueteethCmd.parse8032Cmd(notifyCode)
if (_.hasChargeFlag) {
utils.sucMsg('结束成功')
// _.hasChargeFlag = false;
_.writePileStatusCode()
uni.setStorageSync('hasChargeFlags', false)
} else {
utils.sucMsg('充电成功')
// _.hasChargeFlag = true;
_.writePileStatusCode()
uni.setStorageSync('hasChargeFlags', true)
}
} else if ('8041' == cmdCodeType) {
let msgBody = notifyCode.substring(26, notifyCode.length - 4);
let res = msgBody.substring(4, 6)
console.log('res=' + res)
uni.hideLoading();
if (res == '00') { //'00'成功
// utils.sucMsg('模式切换成功')
// _.$refs.uToast.show({
// title: '模式切换成功',
// // 如果不传此type参数,默认为default,也可以手动写上 type: 'default'
// type: 'success',
// // 如果不需要图标,请设置为false
// icon: false
// })
_.equipBaseInfo.chargeMode = _.currenTempChargeMode
store.setEquipBaseInfo(_.equipBaseInfo);
_.wirte0041CodeMsgId = '';
} else if (res == '01') { //'01'失败
_.$refs.uToast.show({
title: '模式切换失败,请重试',
// 如果不传此type参数,默认为default,也可以手动写上 type: 'default'
type: 'error',
// 如果不需要图标,请设置为false
icon: false
})
}
_.looperFlag = true;
_.writePileStatusCode()
_.looperGetPileStatusFun()
}
}
},
/*往桩里面写入数据*/
writeDataToEquip(msg) {
let _ = this;
function hexStringToArrayBuffer(str) {
if (!str) {
return new ArrayBuffer(0);
}
var buffer = new ArrayBuffer(str.length / 2);
let dataView = new DataView(buffer)
let ind = 0;
for (var i = 0, len = str.length / 2; i < len; i += 1) {
let code = parseInt(str.substr(2 * i, 2), 16)
// console.log(code)
dataView.setUint8(ind, code)
ind++
}
return buffer;
}
function hexStringToArrayBuffer1(str) {
if (!str) {
return new ArrayBuffer(0);
}
var buffer = new ArrayBuffer(str.length);
let dataView = new DataView(buffer)
let ind = 0;
for (var i = 0, len = str.length; i < len; i += 2) {
let code = parseInt(str.substr(i, 2), 16)
// console.log(code)
dataView.setUint8(ind, code)
ind++
}
return buffer;
}
const bufferNew = hexStringToArrayBuffer(msg)
console.log(bufferNew)
uni.writeBLECharacteristicValue({
deviceId: _.deviceId, // 设备ID,在【4】里获取到
serviceId: _.serviceId, // 服务UUID,在【6】里能获取到
characteristicId: _.writeCharacteristicId, // 特征值,在【7】里能获取到
value: bufferNew,
success(res) {
console.log('写入指令成功===' + msg)
},
fail(err) {
console.error(err)
utils.errMsg('指令写入失败,请重新连接');
uni.hideLoading()
// 断开连接,重新连接
_.closeDeviceConnect();
}
})
},
/*ArrayBuffer转16进度字符串示例*/
ab2hex(buffer) {
const hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function(bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join('')
},
/*轮训查询桩的信息*/
looperGetPileStatusFun() {
if (!this.looperFlag) {
return false;
}
let _ = this;
clearTimeout(this.statusLooper)
// 连接上设备再去查状态
if (_.hasConnectBTFlag) {
this.statusLooper = setTimeout(() => {
_.writePileStatusCode();
_.looperGetPileStatusFun();
}, 5000)
}
},
/*0051获取桩状态数据*/
writePileStatusCode() {
// 获取桩状态
const cmdCode = blueteethCmd.gen0051Cmd();
this.writeDataToEquip(cmdCode)
},
/*蓝牙连接失败*/
connectErrorFun() {
uni.hideLoading();
utils.errMsg('蓝牙连接失败,请靠近充电桩后重试')
this.closeDeviceConnect();
// this.continueConnectEquipFun();
},
/*关闭蓝牙连接*/
closeDeviceConnect() {
let _ = this;
this.hasConnectBTFlag = false;
uni.setStorageSync('hasConnectBTFlag', false)
uni.setStorageSync('hasChargeFlags', false)
this.hasChargeFlag = false;
// uni.hideLoading()
console.log('准备关闭蓝牙连接')
console.log(_.deviceId)
console.log('准备关闭蓝牙连接')
// 移除蓝牙监听
wx.closeBLEConnection({
deviceId: _.deviceId,
success(res) {
console.log(res)
console.log('断开与deviceId的连接')
},
fail(err) {
console.log(err)
},
complete() {
wx.closeBluetoothAdapter({
success(res) {
console.log('彻底关闭蓝牙')
},
complete() {
}
})
}
})
},
/*模式切换 蓝牙和即插即充模式切换*/
switchType() {
let that = this;
wx.vibrateShort();
if (this.hasChargeFlag) {
utils.errMsg('充电中,无法修改');
return;
}
if (!this.hasEquipFlag) {
utils.errMsg('请先绑桩');
return;
}
if (this.waitChargeFlag) {
utils.errMsg('充电等待中,无法修改');
return
}
if (!this.hasConnectBTFlag) {
utils.errMsg('请先连接蓝牙');
return;
}
if (this.pileErrorFlag) {
utils.errMsg('设备故障中');
return;
}
if (this.version != '010229') { //010229版本不校验 车枪连接
if (this.pileStatus == '03' || this.pileStatus == '04') {
utils.errMsg('请先拔枪再切换模式')
return
}
}
let tempChargeMode = ''
if (this.equipBaseInfo.chargeMode == '00') {
tempChargeMode = '01'
} else {
tempChargeMode = '00'
}
this.currenTempChargeMode = tempChargeMode;
uni.showLoading({
title: '加载中...',
mask: true
})
let newChargeModeName = ('00' == tempChargeMode ? '蓝牙模式' : '即插即充模式')
// 设置即插即充模式
const chargePower = this.equipBaseInfo.chargePower;
const jcjcCmdCode = blueteethCmd.gen0041Cmd(tempChargeMode, chargePower)
this.looperFlag = false;
clearTimeout(this.statusLooper)
this.statusLooper = null;
this.writeDataToEquip(jcjcCmdCode);
let timer = setTimeout(() => {
if (this.looperFlag == false) {
this.secondWirte(jcjcCmdCode)
// uni.hideLoading()
// utils.errMsg('模式切换失败,请重试!')
// this.writePileStatusCode()
// this.looperGetPileStatusFun()
}
clearTimeout(timer)
timer = null;
}, 3000)
},
/*第一次指令没成功补发第二次*/
secondWirte(jcjcCmdCode) {
let that = this;
this.wirte0041CodeMsgId = utils.genUUID();
this.writeDataToEquip(jcjcCmdCode);
let timer = setTimeout(() => {
console.log('wirte0041CodeMsgId=' + this.wirte0041CodeMsgId)
uni.hideLoading()
if (this.wirte0041CodeMsgId) {
that.$refs.uToast.show({
title: '模式切换失败,请重试',
// 如果不传此type参数,默认为default,也可以手动写上 type: 'default'
type: 'error',
// 如果不需要图标,请设置为false
icon: false
})
}
this.looperFlag = true;
clearTimeout(timer)
timer = null;
this.wirte0041CodeMsgId = '';
this.writePileStatusCode()
this.looperGetPileStatusFun()
}, 2000)
},
/*开始充电*/
startCharge() {
console.log('~~~~~~~~~~~~~开始充电~~~~~~~~~~~~')
console.log("versionCC=" + this.version)
console.log('~~~~~~~~~~~~~开始充电~~~~~~~~~~~')
wx.vibrateShort() //手机震动
if (this.waitChargeFlag) {
utils.errMsg('充电等待中')
return;
}
if (this.pileErrorFlag) {
utils.errMsg('设备故障')
return;
}
if (this.version != '010229') { //010229版本不校验 车枪连接
if (this.pileStatus != '03') {
utils.errMsg('请先插枪后再开始充电')
return
}
}
uni.showLoading({
title: '加载中...',
mask: true
})
// 开始充电
const cmdCode = blueteethCmd.gen0032Cmd('01');
this.wirte0032CodeMsgId = utils.genUUID();
this.writeDataToEquip(cmdCode);
setTimeout(() => {
uni.hideLoading()
if (this.wirte0032CodeMsgId) {
// utils.errMsg('写入失败,请重试!')
this.wirte0032CodeMsgId = '';
}
}, 3000)
},
/*结束充电*/
offCharge() {
wx.vibrateShort() //手机震动
uni.showLoading({
title: '加载中...',
mask: true
})
const cmdCode = blueteethCmd.gen0032Cmd('00');
this.wirte0032CodeMsgId = utils.genUUID();
this.writeDataToEquip(cmdCode);
setTimeout(() => {
uni.hideLoading()
if (this.wirte0032CodeMsgId) {
// utils.errMsg('写入失败,请重试!')
this.wirte0032CodeMsgId = '';
// uni.setStorageSync('hasChargeFlags', false)
}
}, 3000)
},
/*重新连接蓝牙 10秒节流*/
restContect() {
let that = this;
console.log('点了重新连接')
this.hasConnectBTFlag = false;
this.restFlag = true;
uni.setStorageSync('hasConnectBTFlag', false)
wx.vibrateShort({
success(res) {
console.log('震动了')
},
fail(err) {
console.log('未震动')
}
})
//先断开之前的连接
let deviceId = uni.getStorageSync('currentDeviceId')
console.log(deviceId)
// that.pageInitFun()
wx.closeBLEConnection({
deviceId: deviceId,
success(res) {
console.log('断开低功耗蓝牙连接')
},
complete() {
wx.closeBluetoothAdapter({
success(res) {
console.log('彻底关闭蓝牙')
},
complete() {
that.pageInitFun()
}
})
}
})
},
/*设备详情*/
goDeviceDetail() {
// if (!this.hasEquipFlag) {
// utils.errMsg('请先绑桩');
// return;
// }
uni.navigateTo({
url: '/pages/device-details/index?trueIsSharePoint=' + this.equipBaseInfo.trueIsSharePoint
})
},
/*绑定设备*/
goBind() {
uni.navigateTo({
url: '/pages/index/index'
})
},
/*格式化电流电压和功率的format函数*/
formatChargeVal(val) {
if (val) {
val = parseFloat(val)
return parseFloat(val.toFixed(2))
}
return 0;
},
/*跳转鸿蒙os*/
toHarmonyOs() {
uni.navigateTo({
url: '/pages/harmonyOs/index'
})
},
}
}
</script>
<style lang="less">
page {
background-color: #F8FAFF;
}
.index-container {
width: 100%;
}
.hmtips {
width: 90%;
height: 80rpx;
margin: 0 auto;
display: flex;
align-items: center;
background-color: #6F727A;
color: #FFFFFF;
border-radius: 15rpx;
font-size: 32rpx;
position: relative;
padding-left: 20rpx;
margin-top: 10rpx;
z-index: 10000;
>view {
&:nth-child(1) {
margin-right: 18rpx;
}
&:nth-child(4) {
position: absolute;
right: 0rpx;
bottom: -80rpx;
}
}
}
.bluethoothStatus {
position: absolute;
width: 200rpx;
height: 100rpx;
left: 0;
top: 80rpx;
display: flex;
justify-content: center;
flex-direction: column;
align-items: center;
>view {
&:nth-child(1) {
width: 50rpx;
height: 50rpx;
}
&:nth-child(2) {
font-weight: 800;
font-size: 16rpx;
margin-top: 10rpx;
color: #CDCDCD;
}
}
}
.charging {
width: 400rpx;
height: 400rpx;
margin: 48rpx auto 0;
position: relative;
.charge-text {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: #FFFFFF;
font-size: 48rpx;
}
.charing-animate {
width: 400rpx;
height: 400rpx;
border-radius: 50%;
position: relative;
border-radius: 42% 58% 62% 40%;
background-color: rgba(54, 207, 201, .7);
animation: rotates 5s reverse linear infinite;
// background: radial-gradient(circle, #36CFC9 0%, #67f5ee 100%);
// color: #ffffff;
// text-align: center;
// line-height: 400rpx;
// font-size: 48rpx;
// box-shadow: 0px 2px 10px 10px rgba(57, 221, 197, 0.25);
// animation: fadenum 3s ease-in-out infinite;
&::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 400rpx;
height: 400rpx;
background-color: rgba(54, 207, 201, 1);
border-radius: 32% 48% 52% 40%;
animation: rotates 5s infinite linear;
}
}
}
.otherStatus {
width: 400rpx;
height: 400rpx;
border-radius: 50%;
background: #3291F8;
margin: 48rpx auto 0;
display: flex;
justify-content: center;
align-items: center;
.ring {
width: 360rpx;
height: 360rpx;
border-radius: 50%;
background: #FFFFFF;
color: #3291F8;
line-height: 360rpx;
text-align: center;
font-size: 48rpx;
}
}
.otherStatuse {
width: 400rpx;
height: 400rpx;
border-radius: 50%;
background: #1ED56D;
margin: 48rpx auto 0;
display: flex;
justify-content: center;
align-items: center;
.ring {
width: 360rpx;
height: 360rpx;
border-radius: 50%;
background: #FFFFFF;
color: #1ED56D;
line-height: 360rpx;
text-align: center;
font-size: 48rpx;
}
}
.errStatus {
width: 400rpx;
height: 400rpx;
border-radius: 50%;
background: #ff4d40;
margin: 48rpx auto 0;
display: flex;
justify-content: center;
align-items: center;
.ring {
width: 360rpx;
height: 360rpx;
border-radius: 50%;
background: #FFFFFF;
color: #ff4d40;
line-height: 360rpx;
text-align: center;
font-size: 48rpx;
}
}
.ont-connected {
width: 400rpx;
height: 400rpx;
border-radius: 50%;
background: #CDCDCD;
margin: 48rpx auto 0;
display: flex;
justify-content: center;
align-items: center;
.rings {
width: 360rpx;
height: 360rpx;
border-radius: 50%;
background: #FFFFFF;
color: #CDCDCD;
line-height: 360rpx;
text-align: center;
font-size: 48rpx;
}
}
.no-bind {
width: 400rpx;
height: 400rpx;
border-radius: 50%;
background: #CDCDCD;
margin: 48rpx auto 0;
display: flex;
justify-content: center;
align-items: center;
.rings {
width: 360rpx;
height: 360rpx;
border-radius: 50%;
background: #FFFFFF;
color: #CDCDCD;
line-height: 360rpx;
text-align: center;
font-size: 48rpx;
}
}
.startCharge {
width: 150rpx;
height: 150rpx;
border-radius: 50%;
background: #1890FF;
margin: 106rpx auto 0;
display: flex;
justify-content: center;
align-items: center;
>view {
width: 102rpx;
height: 102rpx;
background: url('https://img.cdn.wxzhida.cn/lingpao-ui/kaiguan.png');
background-size: cover;
}
}
.offCharge {
width: 150rpx;
height: 150rpx;
border-radius: 50%;
background: #FFC069;
margin: 106rpx auto 0;
display: flex;
justify-content: center;
align-items: center;
>view {
width: 102rpx;
height: 102rpx;
background: url('https://img.cdn.wxzhida.cn/lingpao-ui/%E5%BC%80%E5%85%B3.png');
background-size: cover;
}
}
.toContect {
width: 150rpx;
height: 150rpx;
border-radius: 50%;
background: #1890FF;
margin: 56rpx auto 0;
display: flex;
justify-content: center;
align-items: center;
>view {
width: 102rpx;
height: 102rpx;
background: url('https://img.cdn.wxzhida.cn/lingpao-ui/vvvvxx.png');
background-size: cover;
}
}
.toBind {
width: 150rpx;
height: 150rpx;
border-radius: 50%;
background: #1890FF; //#004d73 #146eb4
margin: 86rpx auto 0;
display: flex;
justify-content: center;
align-items: center;
>view {
width: 102rpx;
height: 102rpx;
background: url('https://img.cdn.wxzhida.cn/lingpao-ui/vvvvxx.png');
background-size: cover;
}
}
.contect-text {
font-size: 28rpx;
color: #C1C3C3;
height: 50rpx;
display: flex;
align-items: center;
width: 100%;
justify-content: center;
margin-top: 20rpx;
}
.marg100 {
margin: 100rpx auto 0;
}
.marg75 {
margin: 75rpx auto 0;
}
.panel {
width: 90%;
border-radius: 60rpx;
overflow: hidden;
background: #ffffff;
// margin: 100rpx auto 0;
box-shadow: 0px 2px 20px 3px rgba(169, 169, 169, 0.39);
.btn {
display: flex;
height: 266rpx;
border-bottom: 4rpx solid #F8FAFF;
>view {
flex: 1;
}
.switch {
border-right: 4rpx solid #F8FAFF;
// display: flex;
// justify-content: center;
// align-items: center;
position: relative;
}
.switch_describe {
color: #C1C3C3;
position: absolute;
width: 100%;
top: 70%;
left: 50%;
text-align: center;
transform: translateX(-50%);
}
.cut {
position: absolute;
top: 0;
width: 150rpx;
height: 48rpx;
border-bottom-right-radius: 60rpx;
text-align: center;
line-height: 48rpx;
}
.cutA {
background: #BAE7FF;
color: #2B9AFF;
}
.cutB {
background: #D9F7BE;
color: #52C41A;
}
//蓝牙模式
.blueThtooth {
width: 64rpx;
height: 64rpx;
background: url('https://img.cdn.wxzhida.cn/lingpao-ui/%E8%93%9D%E7%89%99%20%281%29.png');
background-size: 100% 100%;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
//即插即充模式
.plug {
width: 64rpx;
height: 64rpx;
background: url('https://img.cdn.wxzhida.cn/lingpao-ui/%E5%9B%BE%E6%A0%87-%E6%8F%92%E5%A4%B4.png');
background-size: 100% 100%;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.bind-squre {
position: relative;
border-right: 4rpx solid #F8FAFF;
.bind_item {
width: 64rpx;
height: 64rpx;
background: url('https://img.cdn.wxzhida.cn/lingpao-ui/%E8%AE%BE%E5%A4%87%E7%BB%91%E5%AE%9A%20%283%29.png');
background-size: 100% 100%;
background-color: #FFFFF;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.bind_items {
width: 64rpx;
height: 64rpx;
background: url('https://img.cdn.wxzhida.cn/lingpao-ui/%E8%AE%BE%E5%A4%87%E7%BB%91%E5%AE%9A%20%284%29.png');
background-size: 100% 100%;
background-color: #FFFFF;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.device_describe {
color: #C1C3C3;
position: absolute;
top: 70%;
width: 100%;
text-align: center;
}
}
.device {
position: relative;
.device_item {
width: 64rpx;
height: 64rpx;
background: url('https://img.cdn.wxzhida.cn/lingpao-ui/%E8%AE%BE%E5%A4%87.png');
background-size: 100% 100%;
background-color: #1890FF;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.device_items {
width: 64rpx;
height: 64rpx;
background: url('https://img.cdn.wxzhida.cn/lingpao-ui/%E8%AE%BE%E5%A4%87.png');
background-size: 100% 100%;
background-color: #1ED56D;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.device_describe {
color: #C1C3C3;
position: absolute;
top: 70%;
width: 100%;
text-align: center;
}
}
}
.tips {
display: flex;
height: 266rpx;
box-sizing: border-box;
>view {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: flex-end;
}
.center-tips {
border-right: 4rpx solid #F8FAFF;
border-left: 4rpx solid #F8FAFF;
}
.marg {
margin-bottom: 50rpx;
margin-top: 20rpx;
color: #C1C3C3;
}
.smallFont {
font-size: 24rpx;
}
}
}
@keyframes fadenum {
/*设置内容由显示变为隐藏*/
0% {
opacity: 0.8;
transform: scale(.9);
}
50% {
opacity: 1.0;
transform: scale(1);
}
100% {
opacity: 0.8;
transform: scale(0.9);
}
}
@keyframes rotates {
50% {
border-radius: 80% 70% 58% 70%;
}
100% {
transform: rotate(-720deg);
}
}
</style>