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


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 相关文章推荐
告诉大家什么是JSON
Jun 10 Javascript
javascript 学习笔记(一)DOM基本操作
Apr 08 Javascript
40款非常棒的jQuery 插件和制作教程(系列二)
Nov 02 Javascript
jQuery链式操作如何实现以及为什么要用链式操作
Jan 17 Javascript
如何在一个页面显示多个百度地图
Apr 07 Javascript
JS实现让网页背景图片斜向移动的方法
Feb 25 Javascript
JavaScript中的Math.sin()方法使用详解
Jun 15 Javascript
模仿password输入框的实现代码
Jun 07 Javascript
jQuery表单验证插件解析(推荐)
Jul 21 Javascript
bootstrap datetimepicker实现秒钟选择下拉框
Jan 05 Javascript
Vue使用.sync 实现父子组件的双向绑定数据问题
Apr 04 Javascript
在vue中使用jsx语法的使用方法
Sep 30 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广告加载类用法实例
2014/09/23 PHP
Zend Framework教程之Zend_Config_Xml用法分析
2016/03/23 PHP
PHP中的异常处理机制深入讲解
2020/11/10 PHP
在页面上点击任一链接时触发一个事件的代码
2007/04/07 Javascript
jquery 插件 人性化的消息显示
2008/01/21 Javascript
JQuery 操作Javascript对象和数组的工具函数小结
2010/01/22 Javascript
js限制textarea每行输入字符串长度的代码
2012/10/31 Javascript
圣诞节Merry Christmas给博客添加浪漫的下雪效果基于jquery实现
2012/12/27 Javascript
javascript版的in_array函数(判断数组中是否存在特定值)
2014/05/09 Javascript
JS解析XML实例分析
2015/01/30 Javascript
javascript实时获取鼠标坐标值并显示的方法
2015/04/30 Javascript
js验证真实姓名与身份证号是否匹配
2015/10/13 Javascript
详解Wondows下Node.js使用MongoDB的环境配置
2016/03/01 Javascript
JavaScript的MVVM库Vue.js入门学习笔记
2016/05/03 Javascript
移动端翻页插件dropload.js(支持Zepto和jQuery)
2016/07/27 Javascript
jQuery简单创建节点的方法
2016/09/09 Javascript
jquery 删除节点 添加节点 找兄弟节点的简单实现
2016/12/07 Javascript
vue 2.0路由之路由嵌套示例详解
2017/05/08 Javascript
微信小程序websocket实现聊天功能
2020/03/30 Javascript
对Layer UI 模块化的用法详解
2019/09/26 Javascript
Antd下拉选择,自动匹配功能的实现
2020/10/24 Javascript
[03:06]V社市场总监Dota2项目负责人Erik专访:希望更多中国玩家加入DOTA2
2014/07/11 DOTA
[01:11:02]Secret vs Newbee 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/17 DOTA
flask框架自定义过滤器示例【markdown文件读取和展示功能】
2019/11/08 Python
python实现将视频按帧读取到自定义目录
2019/12/10 Python
python中p-value的实现方式
2019/12/16 Python
Cotton On南非:澳洲时尚平价品牌
2018/06/28 全球购物
车辆维修工自我评价怎么写
2013/09/20 职场文书
员工试用期考核自我鉴定
2014/04/13 职场文书
中学优秀班主任事迹材料
2014/05/01 职场文书
食品科学与工程专业毕业生求职信范文
2014/07/21 职场文书
骨干教师考核评语
2014/12/31 职场文书
鼋头渚导游词
2015/02/05 职场文书
活动宣传稿范文
2015/07/23 职场文书
2020年基层司法所建设情况调研报告
2019/11/30 职场文书
GPU服务器的多用户配置方法
2022/07/07 Servers