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


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 相关文章推荐
IE不出现Flash激活框的小发现的js实现方法
Sep 07 Javascript
javascript AutoScroller 函数类
May 29 Javascript
jquery 插件实现图片延迟加载效果代码
Feb 06 Javascript
文本框根据输入内容自适应高度的代码
Oct 24 Javascript
原生javascript和jquery判断浏览器版本等信息
Jul 04 Javascript
JS 弹出层 定位至屏幕居中示例
May 21 Javascript
JS实现可自定义大小,可双击关闭的弹出层效果
Oct 16 Javascript
基于javascript实现全国省市二级联动下拉选择菜单
Jan 28 Javascript
jquery实现左右无缝轮播图
Jul 31 Javascript
javascript实现秒表计时器的制作方法
Feb 16 Javascript
JavaScript评论点赞功能的实现方法
Mar 13 Javascript
如何重置vue打印变量的显示方式
Dec 06 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
哪吒敖丙传:新人物二哥敖乙出场 小敖丙奶气十足
2020/03/08 国漫
php实现mysql数据库操作类分享
2014/02/14 PHP
PHP return语句的另一个作用
2014/07/30 PHP
PHP中调用SVN命令更新网站方法
2015/01/07 PHP
YII Framework框架教程之国际化实现方法
2016/03/14 PHP
ExtJS如何设置与获取radio控件的选取状态
2014/01/22 Javascript
纯js实现重发验证码按钮倒数功能
2015/04/21 Javascript
整理Javascript流程控制语句学习笔记
2015/11/29 Javascript
Es6 Generator函数详细解析
2018/02/24 Javascript
详解webpack的proxyTable无效的解决方案
2018/06/15 Javascript
关于vue编译版本引入的问题的解决
2018/09/17 Javascript
VUE2.0 ElementUI2.0表格el-table自适应高度的实现方法
2018/11/28 Javascript
vue集成kindeditor富文本的实现示例代码
2019/06/07 Javascript
Vue 动态添加路由及生成菜单的方法示例
2019/06/20 Javascript
解决layui页面按钮点击无反应,也不报错的问题
2019/09/29 Javascript
JS监听组合按键思路及实现过程
2020/04/17 Javascript
简介Python的collections模块中defaultdict类型的用法
2016/07/07 Python
python机器学习实战之最近邻kNN分类器
2017/12/20 Python
numpy给array增加维度np.newaxis的实例
2018/11/01 Python
python读取目录下最新的文件夹方法
2018/12/24 Python
Ubuntu下Anaconda和Pycharm配置方法详解
2019/06/14 Python
Pandas DataFrame中的tuple元素遍历的实现
2019/10/23 Python
导致python中import错误的原因是什么
2020/07/01 Python
英国袜子店:Sock Shop
2017/01/11 全球购物
德国机车企业:FC-Moto
2017/10/27 全球购物
加拿大休闲和工业服装和鞋类零售商:L’Équipeur
2018/01/12 全球购物
TOWER London官网:鞋子、靴子、运动鞋等
2019/07/14 全球购物
屈臣氏俄罗斯在线商店:Watsons俄罗斯
2020/08/03 全球购物
大学生咖啡店创业计划书
2014/01/21 职场文书
大学生创业感言
2014/01/25 职场文书
秋季运动会广播稿大全
2014/02/17 职场文书
十八届三中全会宣传方案
2014/02/21 职场文书
班组长竞聘书
2014/03/31 职场文书
运动会搞笑广播稿
2014/10/14 职场文书
《雪域豹影》读后感:父爱的伟大
2019/12/23 职场文书
新的CSS 伪类函数 :is() 和 :where()示例详解
2022/08/05 HTML / CSS