微信小程序蓝牙连接小票打印机实例代码详解微信小程序蓝牙连接小票打印机实例代码详解
主要介绍了微信小程序蓝牙连接小票打印机实例代码,代码简单易懂,非常不错,具有一定的参考借鉴价值,需
要的朋友可以参考下
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)