⼩程序蓝⽛连接⼩票打印机实例代码详解1.连接蓝⽛
(第⼀次发表博客)
第⼀步打开蓝⽛并搜索附近打印机设备//
startSearch: function() {
var that = this
wx.openBluetoothAdapter({
success: function(res) {
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: '正在连接',
})
deviceId: e.currentTarget.dataset.title,
success: function(res) {
console.log(res)
app.BLEInformation.deviceId = e.currentTarget.dataset.title
console.log(e.currentTarget.dataset.title)
},
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)
deviceId: app.BLEInformation.deviceId, success: function(res) {
that.setData({
services: res.services
})
},
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 = adCharacter
var notify = ifyCharacter
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 (ify) {
ifyCharaterId = item
ifyServiceId = 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 (ad) {
adCharaterId = item
adServiceId = 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 {
}
} else {
that.openControl()
}
},
fail: function(e) {
console.log(e)
},
complete: function(e) {
console.log("write:" + app.BLEInformation.writeCharaterId)
console.log("read:" + adCharaterId)
console.log("notify:" + ifyCharaterId)
}
})
}
/**
* ⽣命周期函数--监听页⾯加载
*/
onLoad: function(options) {
app.BLEInformation.platform = Platform()
}
总结
以上所述是⼩编给⼤家介绍的⼩程序蓝⽛连接⼩票打印机实例代码详解,希望对⼤家有所帮助,如果⼤家有任何疑问请给我留⾔,⼩编会及时回复⼤家的。在此也⾮常感谢⼤家对⽹站的⽀持!
如果你觉得本⽂对你有帮助,欢迎转载,烦请注明出处,谢谢!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。