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


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 相关文章推荐
Jquery练习之表单验证实现代码
Dec 14 Javascript
ExtJS下 Ext.Direct加载和提交过程排错小结
Apr 02 Javascript
基于jQuery实现多层次的手风琴效果附源码
Sep 21 Javascript
jQuery设置聚焦并使光标位置在文字最后的实现方法
Aug 02 Javascript
js中小数向上取整数,向下取整数,四舍五入取整数的实现(必看篇)
Feb 13 Javascript
vue.js加载新的内容(实例代码)
Jun 01 Javascript
Vue单文件组件基础模板小结
Aug 10 Javascript
ReactNative 之FlatList使用及踩坑封装总结
Nov 29 Javascript
Vue.js通用应用框架-Nuxt.js的上手教程
Dec 25 Javascript
React数据传递之组件内部通信的方法
Dec 31 Javascript
vue2.0+ 从插件开发到npm发布的示例代码
Apr 28 Javascript
layui复选框限制选择个数的方法
Sep 18 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
thinkphp连贯操作实例分析
2014/11/22 PHP
Centos6.5和Centos7 php环境搭建方法
2016/05/27 PHP
js类的静态属性和实例属性的理解
2009/10/01 Javascript
jQuery中使用Ajax获取JSON格式数据示例代码
2013/11/26 Javascript
JS中attr和prop属性的区别以及优先选择示例介绍
2014/06/30 Javascript
js实现点击链接后窗口缩小并居中的方法
2015/03/02 Javascript
JavaScript中使用Object.prototype.toString判断是否为数组
2015/04/01 Javascript
浏览器中url存储的JavaScript实现
2015/07/07 Javascript
js实现input密码框提示信息的方法(附html5实现方法)
2016/01/14 Javascript
精通JavaScript的this关键字
2020/05/28 Javascript
微信小程序 开发工具快捷键整理
2016/10/31 Javascript
前端构建工具之gulp的语法教程
2017/06/12 Javascript
VueJs组件之父子通讯的方式
2018/05/06 Javascript
详解Vue.js使用Swiper.js在iOS
2018/09/10 Javascript
浅谈HTTP 缓存的那些事儿
2018/10/17 Javascript
jQuery实现的点击图片居中放大缩小功能示例
2019/01/16 jQuery
JavaScript this关键字指向常用情况解析
2020/09/02 Javascript
Python的净值数据接口调用示例分享
2016/03/15 Python
Python对数据进行插值和下采样的方法
2018/07/03 Python
Python3.7中安装openCV库的方法
2018/07/11 Python
Tensorflow 合并通道及加载子模型的方法
2018/07/26 Python
Python3对称加密算法AES、DES3实例详解
2018/12/06 Python
python f-string式格式化听语音流程讲解
2019/06/18 Python
python将excel转换为csv的代码方法总结
2019/07/03 Python
你还在@微信官方?聊聊Python生成你想要的微信头像
2019/09/25 Python
python 字典item与iteritems的区别详解
2020/04/25 Python
Python如何用wx模块创建文本编辑器
2020/06/07 Python
基于python调用jenkins-cli实现快速发布
2020/08/14 Python
python在协程中增加任务实例操作
2021/02/28 Python
W3C公布最新的HTML5标准草案
2008/10/17 HTML / CSS
军训 自我鉴定
2014/02/03 职场文书
应聘教师求职信
2014/07/19 职场文书
学生打架检讨书
2014/10/20 职场文书
大学生学生会工作总结2015
2015/05/26 职场文书
《圆明园的毁灭》教学反思
2016/02/16 职场文书
Mysql数据库group by原理详解
2022/07/07 MySQL