微信小程序蓝牙连接小票打印机
程序员文章站
2022-05-18 23:01:46
1.连接蓝牙 (第一次发表博客) 第一步打开蓝牙并搜索附近打印机设备// startSearch: function() { var that = this wx.openBluetoothAdapter({ success: function(res) { wx.getBluetoothAdapt ......
1.连接蓝牙
(第一次发表博客)
第一步打开蓝牙并搜索附近打印机设备//
startsearch: function() {
var that = this
wx.openbluetoothadapter({
success: function(res) {
wx.getbluetoothadapterstate({
success: function(res) {
if (res.available) {
if (res.discovering) {
wx.stopbluetoothdevicesdiscovery({
success: function(res) {
console.log(res)
}
})
}
that.checkpemission()
} else {
wx.showmodal({
title: '提示',
content: '本机蓝牙不可用',
})
}
},
})
},
fail: function() {
wx.showmodal({
title: '提示',
content: '蓝牙初始化失败,请打开蓝牙',
})
}
})
}
2.将搜索到的设备列表绑定点击事件并连接
bindviewtap: function(e) {
var that = this
wx.stopbluetoothdevicesdiscovery({
success: function(res) {
console.log(res)
},
})
that.setdata({
serviceid: 0,
writecharacter: false,
readcharacter: false,
notifycharacter: false
})
var shebei = e.currenttarget.dataset.title
wx.setstoragesync('shebei', shebei)
wx.showloading({
title: '正在连接',
})
wx.createbleconnection({
deviceid: e.currenttarget.dataset.title,
success: function(res) {
console.log(res)
app.bleinformation.deviceid = e.currenttarget.dataset.title
console.log(e.currenttarget.dataset.title)
that.getseviceid()
},
fail: function(e) {
wx.showmodal({
title: '提示',
content: '连接失败',
})
console.log(e)
wx.hideloading()
},
complete: function(e) {
console.log(e)
}
})
}
3.连接成功后保存连接状态
getseviceid: function() {
var that = this
var platform = app.bleinformation.platform
console.log(app.bleinformation.deviceid)
wx.getbledeviceservices({
deviceid: app.bleinformation.deviceid,
success: function(res) {
that.setdata({
services: res.services
})
that.getcharacteristics()
},
fail: function(e) {
console.log(e)
},
complete: function(e) {
console.log(e)
}
})
}
getcharacteristics: function() {
var that = this
var list = that.data.services
var num = that.data.serviceid
var write = that.data.writecharacter
var read = that.data.readcharacter
var notify = that.data.notifycharacter
wx.getbledevicecharacteristics({
deviceid: app.bleinformation.deviceid,
serviceid: list[num].uuid,
success: function(res) {
console.log(res)
for (var i = 0; i < res.characteristics.length; ++i) {
var properties = res.characteristics[i].properties
var item = res.characteristics[i].uuid
if (!notify) {
if (properties.notify) {
app.bleinformation.notifycharaterid = item
app.bleinformation.notifyserviceid = list[num].uuid
notify = true
}
}
if (!write) {
if (properties.write) {
app.bleinformation.writecharaterid = item
app.bleinformation.writeserviceid = list[num].uuid
write = true
}
}
if (!read) {
if (properties.read) {
app.bleinformation.readcharaterid = item
app.bleinformation.readserviceid = list[num].uuid
read = true
}
}
}
if (!write || !notify || !read) {
num++
that.setdata({
writecharacter: write,
readcharacter: read,
notifycharacter: notify,
serviceid: num
})
if (num == list.length) {
wx.showmodal({
title: '提示',
content: '找不到该读写的特征值',
})
} else {
that.getcharacteristics()
}
} else {
that.opencontrol()
}
},
fail: function(e) {
console.log(e)
},
complete: function(e) {
console.log("write:" + app.bleinformation.writecharaterid)
console.log("read:" + app.bleinformation.readcharaterid)
console.log("notify:" + app.bleinformation.notifycharaterid)
}
})
}
/**
* 生命周期函数--监听页面加载
*/
onload: function(options) {
app.bleinformation.platform = app.getplatform()
}
上一篇: 如何打造网站克隆、仿站工具(C#版)
下一篇: 前端基础之JavaScript_1