/**
* index页面
* getDevice 设备信息
* getReplenish 补货
* getShipment 出货
* getInit 初始化
* */
const apps = getApp()
import {getDevice,getReplenish,getShipment,getInit} from '../utils/Bluetooth/instruct'
import BTAdapter from '../utils/Bluetooth/index'
import phpjs from '../libs/phpjs'
Page({
/**
* 页面的初始数据
*/
data: {
e: {
localName:'',
tid:'',
skey:''
},
cmdData: [],
showNum: false,
num:1,
BTAdapter: null, //蓝牙
BtDevices: {}, //要链接蓝牙信息
BtTime: 0, //蓝牙链接查找时间
/**
* 蓝牙链接状态 0:待查找蓝牙 1:已连接 2:链接中 3:链接失败 4:已找到蓝牙 5:查找蓝牙中 6已断开
* */
BleStatus: 0,
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (e) {
//tid和skey参考设备文档
this.setData({
e : e,
["e.localName"] : '',
["e.tid"] :'00000890',
["e.skey"] : '3Zbro1K30IUT8TpB',
//实例化蓝牙工具
BTAdapter : new BTAdapter(),
})
//wx.getSystemInfo().locationEnabled 是地理位置的系统开关和locationAuthorized这个不一样
if(apps.getlocationEnabled()==true){
phpjs.checkPhoneInfo(res=>{
console.log(res,'checkPhoneInfo');
if(res.flag==true){
this._initBTAdapter()
}
})
}else{
wx.showModal({
title: '提示',
content: `请打开位置信息的系统开关,\r\n否则蓝牙将无法搜索到设备`,
showCancel: false
});
}
},
/**
* 初始化蓝牙模块
*/
_initBTAdapter() {
//初始化蓝牙
this.data.BTAdapter._init((res) => {
console.log('初始化结果', res);
//判断是否有错误消息
if (!res.errCode && res.errMsg && res.errCode !== 0) {
wx.showModal({
title: '蓝牙尚未打开',
showCancel:false,
content:res.errMsg
})
} else if (res.errCode && res.errCode == 10001) {
if (res.state && res.state == 3) {
wx.showModal({
title: '微信蓝牙未授权',
showCancel:false,
content:`请开启微信蓝牙权限:具体操作:"设置" -- "微信" -- "蓝牙"`
})
} else {
wx.showModal({
title: '蓝牙尚未打开',
showCancel:false,
content:"1、请开启手机蓝牙;\r\n2,请查看周围是否有连接此设备的蓝牙;\r\n3、请先关闭手机蓝牙在打开一次;\r\n4、退出小程序重新扫码进入."
})
}
} else if (res.errCode == 0) {
//开始搜索蓝牙
this._getBluetoothDevices();
}
this._onBTStateChange(res);
})
},
/**
* 查找蓝牙
*/
_getBluetoothDevices() {
// 蓝牙链接状态 0:待查找蓝牙 1:已连接 2:链接中 3:链接失败 4:已找到蓝牙 5:查找蓝牙中 6已断开
this.setData({
BleStatus:5,
BtTime:new Date().getTime(),
["BTAdapter.localName"]:'B' + Number(this.data.e.tid)
})
console.log(this.data.BTAdapter,'BTAdapter');
this.data.BTAdapter._getBluetoothDevices((res) => {
console.log(res,'查找蓝牙');
this.setData({
["BTAdapter.localName"]:res.localName
})
if (!res.deviceId) {
wx.showModal({
title: '查找蓝牙错误',
showCancel:false
})
return false;
}
this.setData({
BtDevices:res,
BleStatus:4
})
//开始链接蓝牙
this._connectDevices({
linknum: 0
});
});
},
//链接蓝牙设备
_connectDevices(e) {
return new Promise((success, error) => {
if (this.data.BleStatus == 1) {
return success({
errCode: 0
});
}
this.setData({
BtTime:new Date().getTime(),
BleStatus:2,
//多个设备时连接时这个是必须,用来筛选当前扫码的设备
["BTAdapter.deviceId"]:this.data.BtDevices.deviceId
})
console.log('开始链接蓝牙', this.data);
this.data.BTAdapter._createBLEConnection((res) => {
console.log('链接蓝牙结果', res);
if (res.errCode == -1) {
this.setData({
BleStatus:1
})
success(res);
} else if (res.errCode == 0) {
this.setData({
BleStatus:1,
BtTime:new Date().getTime() - this.data.BtTime
})
this._readNotify();
if (this.statusCmd) {
this._writeValues(this.statusCmd, 'statusCmd');
}
/**
* 初始化测试
* */
// console.log(this.data.BTAdapter,'B00000000');
// if(this.data.BTAdapter.localName.slice(1)=='00000000'){
// this.chushihua()
// }
success(res);
} else if (res.errCode == 10012) {
if (!e.linknum) {
e.linknum = 0;
}
e.linknum += 1;
if (e.linknum > 2) {
return
}
//链接超时
this._connectDevices(e);
} else {
this.setData({
BleStatus:3
})
wx.showModal({
title: '蓝牙失败',
showCancel:false,
content:res.errMsg
})
error(res);
}
})
})
},
//监听蓝牙模块开启状态
_onBTStateChange(e) {
wx.onBluetoothAdapterStateChange((res) => {
console.log(res, '蓝牙状态');
if (res.available) {
if (e.errCode != 0) {
this._initBTAdapter();
}
}
if (!res.available) {
this.setData({
BleStatus:0
})
this._initBTAdapter();
}
})
wx.onBLEConnectionStateChange((res) => {
if (!res.connected) {
this.setData({
BleStatus:0
})
}
})
},
/**
* 发送蓝牙指令
*/
_writeValues(cmd, mark) {
console.log(cmd,'cmd');
this.setData({
["BTAdapter.command"]:mark
})
this.data.BTAdapter._writeValues(cmd, (resWrite) => {
console.log(resWrite,'发送指令');
//判断是否发送成功
if (resWrite.errCode == 0) {
this.data.cmdData.push({
cmd: cmd,
type: mark,
status: 1,
time: phpjs.date('H:i:s')
})
console.log(this.data.cmdData,'cmdData');
} else if (resWrite.errCode == 10006) {
//蓝牙已断开
this._connectDevices().then((res) => {
this._writeValues(cmd, mark)
})
} else {
wx.showModal({
title: '发送指令错误',
showCancel:false,
content:resWrite.errMsg
})
}
});
},
duDevice() {
let cmd = getDevice(this.data.e.tid, this.data.e.skey)
this.cmdData = [];
this._writeValues(cmd, 'duDevice');
},
微信小程序蓝牙连接设备
最新推荐文章于 2024-04-17 22:08:46 发布
本文介绍了如何使用微信小程序进行蓝牙设备的连接操作,并讲解了蓝牙指令的发送流程。同时,文章还涵盖了MD5加密的方法以及针对不同手机系统和微信版本的判断技巧。
摘要由CSDN通过智能技术生成