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


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 相关文章推荐
IE6下出现JavaScript未结束的字符串常量错误的解决方法
Nov 21 Javascript
jQuery的学习步骤
Feb 23 Javascript
原生js实现淘宝首页点击按钮缓慢回到顶部效果
Apr 06 Javascript
JS获取Table中td值的方法
Mar 19 Javascript
使用jQuery判断Div是否在可视区域的方法 判断div是否可见
Feb 17 Javascript
JS多文件上传的实例代码
Jan 11 Javascript
使用jQuery,Angular实现登录界面验证码详解
Apr 27 jQuery
AngularJS service之select下拉菜单效果
Jul 28 Javascript
详解微信JS-SDK选择图片遇到的坑
Aug 15 Javascript
angular6 利用 ngContentOutlet 实现组件位置交换(重排)
Nov 02 Javascript
深入解析Vue源码实例挂载与编译流程实现思路详解
May 05 Javascript
浅谈vue中$event理解和框架中在包含默认值外传参
Aug 07 Javascript
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实现禁用IE和火狐的缓存问题
2012/12/03 PHP
php中二分法查找算法实例分析
2016/09/22 PHP
PHP中大括号'{}'用法实例总结
2017/02/08 PHP
JS动态添加与删除select中的Option对象(示例代码)
2013/12/20 Javascript
js与jquery实时监听输入框值的oninput与onpropertychange方法
2015/02/05 Javascript
js实现仿MSN带关闭功能的右下角弹窗代码
2015/09/04 Javascript
jQuery实现商品活动倒计时
2015/10/16 Javascript
vuejs在解析时出现闪烁的原因及防止闪烁的方法
2016/09/19 Javascript
jQuery插件echarts实现的循环生成图效果示例【附demo源码下载】
2017/03/04 Javascript
从零开始学习Node.js系列教程二:文本提交与显示方法
2017/04/13 Javascript
jQuery操作之效果详解
2017/05/19 jQuery
详解jQuery获取特殊属性的值以及设置内容
2018/11/14 jQuery
Vue实现兄弟组件间的联动效果
2020/01/21 Javascript
[41:08]2014 DOTA2国际邀请赛中国区预选赛 HGT VS NE
2014/05/22 DOTA
[39:07]LGD vs VP 2018国际邀请赛淘汰赛BO3 第二场 8.21
2018/08/22 DOTA
python使用百度翻译进行中翻英示例
2014/04/14 Python
Python异常处理总结
2014/08/15 Python
Python中使用HTMLParser解析html实例
2015/02/08 Python
Python的Tornado框架异步编程入门实例
2015/04/24 Python
发布你的Python模块详解
2016/09/15 Python
python隐藏终端执行cmd命令的方法
2019/06/24 Python
Python 矩阵转置的几种方法小结
2019/12/02 Python
使用pyqt5 tablewidget 单元格设置正则表达式
2019/12/13 Python
python+gdal+遥感图像拼接(mosaic)的实例
2020/03/10 Python
python3中sorted函数里cmp参数改变详解
2020/03/12 Python
Python读取Excel数据并生成图表过程解析
2020/06/18 Python
python和php哪个容易学
2020/06/19 Python
用Python实现童年贪吃蛇小游戏功能的实例代码
2020/12/07 Python
负责人任命书范本
2014/06/04 职场文书
学生党员批评与自我批评
2014/10/15 职场文书
2014年创先争优工作总结
2014/12/11 职场文书
教师培训学习心得体会
2016/01/21 职场文书
68句权威创业名言
2019/08/26 职场文书
解决Golang中ResponseWriter的一个坑
2021/04/27 Golang
Mysql数据库命令大全
2021/05/26 MySQL
vue中控制mock在开发环境使用,在生产环境禁用方式
2022/04/06 Vue.js