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


Posted in Javascript onJune 03, 2019

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

 总结

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

Javascript 相关文章推荐
javascript replace()正则替换实现代码
Feb 26 Javascript
JavaScript中继承的一些示例方法与属性参考
Aug 07 Javascript
js离开或刷新页面检测(且兼容FF,IE,Chrome)
Mar 05 Javascript
JavaScript学习笔记之创建对象
Mar 25 Javascript
js遍历map javaScript遍历map的简单实现
Aug 26 Javascript
jquery获取transform里的值实现方法
Dec 12 jQuery
详解微信小程序的不同函数调用的几种方法
May 08 Javascript
react实现同页面三级跳转路由布局
Sep 26 Javascript
js实现树形数据转成扁平数据的方法示例
Feb 27 Javascript
JS运算符优先级与表达式示例详解
Sep 04 Javascript
如何通过JS实现日历简单算法
Oct 14 Javascript
jQuery实现购物车全功能
Jan 11 jQuery
react-native滑动吸顶效果的实现过程
Jun 03 #Javascript
vue-cli 3 全局过滤器的实例代码详解
Jun 03 #Javascript
vue2之简易的pc端短信验证码的问题及处理方法
Jun 03 #Javascript
使用RxJS更优雅地进行定时请求详析
Jun 02 #Javascript
Vue CLI3基础学习之pages构建多页应用
Jun 02 #Javascript
Vue基础学习之项目整合及优化
Jun 02 #Javascript
JavaScript判断对象和数组的两种方法
May 31 #Javascript
You might like
php查找指定目录下指定大小文件的方法
2014/11/28 PHP
简单谈谈php延迟静态绑定
2016/01/26 PHP
PHP实现Redis单据锁以及防止并发重复写入
2018/04/10 PHP
PHP7新增函数
2021/03/09 PHP
ext for eclipse插件安装方法
2008/04/27 Javascript
JQuery 构建客户/服务分离的链接模型中Table中的排序分析
2010/01/22 Javascript
遍历jquery对象的代码分享
2011/11/02 Javascript
jQuery动态添加、删除元素的方法
2014/01/09 Javascript
javascript动态控制服务器控件实例
2014/09/05 Javascript
jQuery使用hide方法隐藏元素自身用法实例
2015/03/30 Javascript
JavaScript实现的简单幂函数实例
2015/04/17 Javascript
jQuery+Ajax实现限制查询间隔的方法
2016/06/07 Javascript
AngularJS 中的事件详解
2016/07/28 Javascript
js基础之DOM中document对象的常用属性方法详解
2016/10/28 Javascript
Vue中使用vux的配置详解
2017/05/05 Javascript
使用vue-cli4.0快速搭建一个项目的方法步骤
2019/12/04 Javascript
JS中的继承操作实例总结
2020/06/06 Javascript
如何利用vue实现波谱拟合详解
2020/11/05 Javascript
Python基于回溯法子集树模板解决选排问题示例
2017/09/07 Python
使用apidocJs快速生成在线文档的实例讲解
2018/02/07 Python
使用matplotlib画散点图的方法
2018/05/25 Python
python迭代dict的key和value的方法
2018/07/06 Python
详解python selenium 爬取网易云音乐歌单名
2019/03/28 Python
PyQtGraph在pyqt中的应用及安装过程
2019/08/04 Python
scrapy数据存储在mysql数据库的两种方式(同步和异步)
2020/02/18 Python
Python中有几个关键字
2020/06/04 Python
UGG澳洲官网:UGG Australia
2018/04/26 全球购物
爱普生美国官网:Epson美国
2018/11/05 全球购物
竞争性谈判邀请书
2014/02/06 职场文书
集团公司党的群众路线教育实践活动工作总结
2014/03/03 职场文书
会议接待欢迎词范文
2015/01/26 职场文书
校园开放日新闻稿
2015/07/17 职场文书
高二化学教学反思
2016/02/22 职场文书
看看如何用Python绘制小米新版天价logo
2021/04/20 Python
Python机器学习应用之基于线性判别模型的分类篇详解
2022/01/18 Python
CSS实现九宫格布局(自适应)的示例代码
2022/02/12 HTML / CSS