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


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 相关文章推荐
一段实时更新的时间代码
Jul 07 Javascript
javascript得到XML某节点的子节点个数的脚本
Oct 11 Javascript
JavaScript 异步调用框架 (Part 2 - 用例设计)
Aug 03 Javascript
基于jquery的高性能td和input切换并可修改内容实现代码
Jan 09 Javascript
jQuery 1.5.1 发布,全面支持IE9 修复大量bug
Feb 26 Javascript
动态标签 悬停效果 延迟加载示例代码
Nov 21 Javascript
牛叉的Jquery——Jquery与DOM对象的互相转换及DOM的三种操作
Oct 29 Javascript
js 上传文件预览的简单实例
Aug 16 Javascript
DOM中事件处理概览与原理的全面解析
Aug 16 Javascript
微信小程序 实例开发总结
Apr 26 Javascript
简单学习5种处理Vue.js异常的方法
Jun 17 Javascript
vue.js的状态管理vuex中store的使用详解
Nov 08 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
第1次亲密接触PHP5(2)
2006/10/09 PHP
关于时间计算的结总
2006/12/06 PHP
thinkPHP3.1验证码的简单实现方法
2016/04/22 PHP
PHP中SQL查询语句的id=%d解释(推荐)
2016/12/10 PHP
Jquery之美中不足小结
2011/02/16 Javascript
jQuery插件实现可输入和自动匹配的下拉框
2016/10/24 Javascript
微信小程序 解决请求服务器手机预览请求不到数据的方法
2017/01/04 Javascript
ES6中新增的Object.assign()方法详解
2017/09/22 Javascript
通过vue-router懒加载解决首次加载时资源过多导致的速度缓慢问题
2018/04/08 Javascript
Vue props用法详解(小结)
2018/07/03 Javascript
Node.js事件的正确使用方法
2019/04/05 Javascript
JavaScript判断对象和数组的两种方法
2019/05/31 Javascript
vue实现井字棋游戏
2020/09/29 Javascript
axios解决高并发的方法:axios.all()与axios.spread()的操作
2020/11/09 Javascript
[56:18]DOTA2上海特级锦标赛主赛事日 - 4 败者组第四轮#2 MVP.Phx VS Fnatic第二局
2016/03/05 DOTA
[56:42]VP vs RNG 2019国际邀请赛小组赛 BO2 第二场 8.15
2019/08/17 DOTA
让python同时兼容python2和python3的8个技巧分享
2014/07/11 Python
Java中重定向输出流实现用文件记录程序日志
2015/06/12 Python
python中读入二维csv格式的表格方法详解(以元组/列表形式表示)
2020/04/24 Python
Python基于smtplib模块发送邮件代码实例
2020/05/29 Python
英国排名第一的最新设计师品牌手表独立零售商:TIC Watches
2016/09/24 全球购物
美国购买肉、鸭、家禽、鹅肝和熟食网站:D’Artagnan
2018/11/13 全球购物
eDreams意大利:南欧领先的在线旅行社
2018/11/23 全球购物
Anthropologie英国:美国家喻户晓的休闲服装和家居产品品牌
2018/12/05 全球购物
南非最大的在线时尚商店:Zando
2019/07/21 全球购物
MySQL面试题目集锦
2016/04/14 面试题
护士求职推荐信范文
2013/11/23 职场文书
党员干部形式主义个人整改措施
2014/09/17 职场文书
财务部岗位职责
2015/02/03 职场文书
2015年高校辅导员工作总结
2015/04/20 职场文书
SQLServer 日期函数大全(小结)
2021/04/08 SQL Server
Python利用folium实现地图可视化
2021/05/23 Python
Java用自带的Image IO给图片添加水印
2021/06/15 Java/Android
SQL实现LeetCode(177.第N高薪水)
2021/08/04 MySQL
python中pycryto实现数据加密
2022/04/29 Python
SQL使用复合索引实现数据库查询的优化
2022/05/25 SQL Server