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


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与FireFox的剪切板操作代码
Sep 28 Javascript
在Javascript中 声明时用&quot;var&quot;与不用&quot;var&quot;的区别
Apr 15 Javascript
JavaScript实现按Ctrl键打开新页面
Sep 04 Javascript
由ReactJS的Hello world说开来
Jul 02 Javascript
jQuery鼠标悬浮链接弹出跟随图片实例代码
Jan 08 Javascript
即将发布的jQuery 3 有哪些新特性
Apr 14 Javascript
EasyUI布局 高度自适应
Jun 04 Javascript
AngularJS实用开发技巧(推荐)
Jul 13 Javascript
深入理解Node.js的HTTP模块
Oct 12 Javascript
AngularJS使用ui-route实现多层嵌套路由的示例
Jan 10 Javascript
JS实现点击按钮可实现编辑功能
Jul 03 Javascript
一篇文章让你搞懂JavaScript 原型和原型链
Nov 23 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编程每天必学之验证码
2016/03/03 PHP
关于 Laravel Redis 多个进程同时取队列问题详解
2017/12/25 PHP
ThinkPHP like模糊查询,like多匹配查询,between查询,in查询,一般查询书写方法
2018/09/26 PHP
Thinkphp框架+Layui实现图片/文件上传功能分析
2020/02/07 PHP
JavaScript 学习笔记(十五)
2010/01/28 Javascript
jQuery阻止同类型事件小结
2013/04/19 Javascript
jQuery 取值、赋值的基本方法整理
2014/03/31 Javascript
基于JavaScript操作DOM常用的API小结
2015/12/01 Javascript
JavaScript给input的value赋值引发的关于基本类型值和引用类型值问题
2015/12/07 Javascript
jquery表格datatables实例解析 直接加载和延迟加载
2016/08/12 Javascript
AngularJS实现Input格式化的方法
2016/11/07 Javascript
微信小程序 页面跳转及数据传递详解
2017/03/14 Javascript
Node Puppeteer图像识别实现百度指数爬虫的示例
2018/02/22 Javascript
AjaxUpLoad.js实现文件上传
2018/03/05 Javascript
vue-quill-editor富文本编辑器简单使用方法
2018/09/21 Javascript
深入理解Angularjs 脏值检测
2018/10/12 Javascript
Bootstrap的aria-label和aria-labelledby属性实例详解
2018/11/02 Javascript
微信小程序实现九宫格抽奖
2020/04/15 Javascript
Vue表单绑定的实例代码(单选按钮,选择框(单选时,多选时,用 v-for 渲染的动态选项)
2019/05/13 Javascript
javascript sort()对数组中的元素进行排序详解
2019/10/13 Javascript
VUE实现图片验证码功能
2020/11/18 Javascript
利用JavaScript为句子加标题的3种方法示例
2021/01/05 Javascript
用Python的Tornado框架结合memcached页面改善博客性能
2015/04/24 Python
用Python解决计数原理问题的方法
2016/08/04 Python
python数据结构链表之单向链表(实例讲解)
2017/07/25 Python
Python爬虫常用库的安装及其环境配置
2018/09/19 Python
更改Python的pip install 默认安装依赖路径方法详解
2018/10/27 Python
如何在 Django 模板中输出 &quot;{{&quot;
2020/01/24 Python
日语系毕业生推荐信
2013/11/11 职场文书
大学生实习思想汇报
2014/01/12 职场文书
《十六年前的回忆》教学反思
2014/02/14 职场文书
酒鬼酒广告词
2014/03/21 职场文书
2014年科协工作总结
2014/12/09 职场文书
爱情保证书
2015/01/17 职场文书
pytorch交叉熵损失函数的weight参数的使用
2021/05/24 Python
springboot为异步任务规划自定义线程池的实现
2022/06/14 Java/Android