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


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 相关文章推荐
JS去除右边逗号的简单方法
Jul 03 Javascript
JavaScript在for循环中绑定事件解决事件参数不同的情况
Jan 20 Javascript
js获取客户端网卡的IP地址、MAC地址
Mar 26 Javascript
在JavaScript中判断整型的N种方法示例介绍
Jun 18 Javascript
node.js适合游戏后台开发吗?
Sep 03 Javascript
Vue.Js中的$watch()方法总结
Mar 23 Javascript
jQuery插件FusionCharts实现的MSBar2D图效果示例【附demo源码】
Mar 24 jQuery
vue实现商城购物车功能
Nov 27 Javascript
利用node.js如何创建子进程详解
Dec 09 Javascript
vue2.0+SVG实现音乐播放圆形进度条组件
Sep 21 Javascript
IntelliJ IDEA编辑器配置vue高亮显示
Sep 26 Javascript
Vue OpenLayer 为地图绘制风场效果
Apr 24 Vue.js
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
Syphon 虹吸式咖啡壶冲煮–拨动法
2021/03/03 冲泡冲煮
常用的php对象类型判断
2008/08/27 PHP
php实现与python进行socket通信的方法示例
2017/08/30 PHP
Yii2.0建立公共方法简单示例
2019/01/29 PHP
静态图片的十一种滤镜效果--不支持Ie7及非IE浏览器。
2007/03/06 Javascript
JavaScript 学习点滴记录
2009/04/24 Javascript
js判断样式className同时增加class或删除class
2013/01/30 Javascript
JavaScript实现简单的时钟实例代码
2013/11/23 Javascript
Js判断CSS文件加载完毕的具体实现
2014/01/17 Javascript
函数式 JavaScript(一)简介
2014/07/07 Javascript
JavaScript中合并数组的N种方法
2014/09/16 Javascript
js实现遮罩层弹出框的方法
2015/01/15 Javascript
jQuery插件ajaxFileUpload实现异步上传文件效果
2015/04/14 Javascript
JavaScript实现select添加option
2015/07/03 Javascript
angular.js之路由的选择方法
2016/09/24 Javascript
JS+Canvas实现的俄罗斯方块游戏完整实例
2016/12/12 Javascript
微信小程序 首页制作简单实例
2017/04/07 Javascript
nodejs对express中next函数的一些理解
2017/09/08 NodeJs
Angular实现下拉框模糊查询功能示例
2018/01/03 Javascript
angularjs 的数据绑定实现原理
2018/07/02 Javascript
Vue三种常用传值示例(父传子、子传父、非父子)
2018/07/24 Javascript
JS立即执行的匿名函数用法分析
2019/11/04 Javascript
原理深度解析Vue的响应式更新比React快
2020/04/04 Javascript
Python通过DOM和SAX方式解析XML的应用实例分享
2015/11/16 Python
python实现从文件中读取数据并绘制成 x y 轴图形的方法
2018/10/14 Python
python使用OpenCV模块实现图像的融合示例代码
2020/04/10 Python
python读取图像矩阵文件并转换为向量实例
2020/06/18 Python
详解python中的异常和文件读写
2021/01/03 Python
网络工程师职业规划
2014/02/10 职场文书
报关报检委托书
2014/04/08 职场文书
大学生党员自我评价范文
2014/04/09 职场文书
捐助感谢信
2015/01/22 职场文书
入伍通知书
2015/04/23 职场文书
银行保安拾金不昧表扬稿
2015/05/05 职场文书
给校长的建议书范文
2015/09/14 职场文书
党员反四风学习心得体会
2016/01/22 职场文书