微信小程序蓝牙连接小票打印机实例代码详解

 更新时间:2019年06月03日 09:28:26   作者:浪子回头——  
这篇文章主要介绍了微信小程序蓝牙连接小票打印机实例代码,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下

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()
}

 总结

以上所述是小编给大家介绍的微信小程序蓝牙连接小票打印机实例代码详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

相关文章

  • JS实现微信

    JS实现微信"炸屎"大作战功能

    这篇文章主要介绍了JS实现微信 "炸屎"大作战,本文通过实例代码图文展示给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-07-07
  • setInterval和setTimeout停止的方法

    setInterval和setTimeout停止的方法

    要想知道它们是怎么停止的,首先我们要了解它们的运行机制和原理,下面是具体的介绍。
    2011-01-01
  • js项目中添加ts支持实现示例详解

    js项目中添加ts支持实现示例详解

    这篇文章主要为大家介绍了如何在js项目中添加ts支持实现示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • JavaScript性能优化 创建文档碎片(document.createDocumentFragment)

    JavaScript性能优化 创建文档碎片(document.createDocumentFragment)

    讲这个方法之前,我们应该先了解下插入节点时浏览器会做什么。
    2010-07-07
  • 一文带你了解async/await的使用

    一文带你了解async/await的使用

    async/await 让异步代码变成同步的方式,从而使代码更具表现力和可读性;还统一了异步编程的经验;以及提供了更好的错误堆栈跟踪。本文就来讲讲async/await的使用,需要的可以参考一下
    2022-09-09
  • JavaScript实现数组对象转换为键值对的四种方式

    JavaScript实现数组对象转换为键值对的四种方式

    本文探讨了将包含 {icon: "abc", url: "123"} 形式对象的数组转换为键值对形式的四种方法,并从实现方式的简洁性、可读性和性能角度进行了分析比较,感兴趣的朋友可以参考下
    2024-02-02
  • 关于Javascript中defer和async的区别总结

    关于Javascript中defer和async的区别总结

    相信看过javascript高级程序设计的人,在javascript高级程序设计里,应该看到了介绍了有关defer和async的区别,可是比较浅显,而且也说得不是很清楚。下面我们来通过这篇文章来详细了解下dfer和async的区别。
    2016-09-09
  • 前端跨域的几种解决方式总结(推荐)

    前端跨域的几种解决方式总结(推荐)

    这篇文章主要介绍了前端跨域的几种解决方式,详细介绍了同源策略并同时给出了跨域的五种解决方案,具体操作步骤大家可查看下文的详细讲解,感兴趣的小伙伴们可以参考一下。
    2017-08-08
  • JavaScript SHA512&SHA256加密算法详解

    JavaScript SHA512&SHA256加密算法详解

    本文给大家分享的是javascript版的SHA512&SHA256加密算法的代码,以及用法,有需要的小伙伴可以参考下。
    2015-08-08
  • JavaScript学习笔记之取数组中最大值和最小值

    JavaScript学习笔记之取数组中最大值和最小值

    在实际业务中有的时候要取出数组中的最大值或最小值。但在数组中并没有提供arr.max()和arr.min()这样的方法。那么是不是可以通过别的方式实现类似这样的方法呢?那么今天我们就来整理取出数组中最大值和最小值的一些方法,需要的朋友一起学习吧
    2016-03-03

最新评论