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


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 相关文章推荐
学习ExtJS 访问容器对象
Oct 07 Javascript
JSQL 批量图片切换的实现代码
May 05 Javascript
屏蔽F1~F12的快捷键的js函数
May 06 Javascript
EXTjs4.0的store的findRecord的BUG演示代码
Jun 08 Javascript
最短的IE判断var ie=!-[1,]分析
May 28 Javascript
jQuery使用load()方法载入另外一个网页文件内的指定标签内容到div标签的方法
Mar 25 Javascript
JavaScript中使用Math.floor()方法对数字取整
Jun 15 Javascript
探讨JavaScript语句的执行过程
Jan 28 Javascript
JavaScript+canvas实现七色板效果实例
Feb 18 Javascript
JavaScript中文件上传API详解
Apr 01 Javascript
Bootstrap中datetimepicker使用小结
Dec 28 Javascript
Vue中使用matomo进行访问流量统计的实现
Nov 05 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 设置MySQL连接字符集的方法
2011/01/02 PHP
PHP实现的交通银行网银在线支付接口ECSHOP插件和使用例子
2014/05/10 PHP
Laravel5.1自定义500错误页面示例
2016/10/09 PHP
PHP中检索字符串的方法分析【strstr与substr_count方法】
2017/02/17 PHP
php JWT在web端中的使用方法教程
2018/09/06 PHP
php写入mysql中文乱码的实例解决方法
2019/09/17 PHP
jQuery 学习6 操纵元素显示效果的函数
2010/02/07 Javascript
jQuery选中select控件 无法设置selected的解决方法
2010/09/01 Javascript
一些实用的jQuery代码片段收集
2011/07/12 Javascript
基于jquery的拖动布局插件
2011/11/25 Javascript
使用jquery prev()方法找到同级的前一个元素
2014/07/11 Javascript
jquery uploadify 在FF下无效的解决办法
2014/09/26 Javascript
jQuery制作可自定义大小的拼图游戏
2015/03/30 Javascript
学习JavaScript设计模式之状态模式
2016/01/08 Javascript
使用vue编写一个点击数字计时小游戏
2016/08/31 Javascript
JavaScript中访问id对象 属性的方式访问属性(实例代码)
2016/10/28 Javascript
vue实现简单loading进度条
2018/06/06 Javascript
layui 选择列表,打勾,点击确定返回数据的例子
2019/09/02 Javascript
JavaScript中0、空字符串、'0'是true还是false的知识点分享
2019/09/16 Javascript
JS script脚本中async和defer区别详解
2020/06/24 Javascript
[01:07:19]DOTA2-DPC中国联赛 正赛 CDEC vs XG BO3 第一场 1月19日
2021/03/11 DOTA
python分析apache访问日志脚本分享
2015/02/26 Python
python实现读取excel写入mysql的小工具详解
2017/11/20 Python
对python中的logger模块全面讲解
2018/04/28 Python
python 判断字符串中是否含有汉字或非汉字的实例
2019/07/15 Python
python selenium登录豆瓣网过程解析
2019/08/10 Python
解决Python使用列表副本的问题
2019/12/19 Python
python 画条形图(柱状图)实例
2020/04/24 Python
美国在线购物频道:Shop LC
2019/04/21 全球购物
食品安全工作方案
2014/05/07 职场文书
体育系毕业生自荐信
2014/06/28 职场文书
应聘护士求职信
2014/07/21 职场文书
工作经历证明书范文
2014/11/02 职场文书
酒店服务员岗位职责
2015/02/09 职场文书
从QQtabBar看css命名规范BEM的详细介绍
2021/08/07 HTML / CSS
CentOS7安装GlusterFS集群以及相关配置
2022/04/12 Servers