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


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 相关文章推荐
Javascript this指针
Jul 30 Javascript
Dom在ajax技术中的作用说明
Oct 25 Javascript
JS字符串处理实例代码
Aug 05 Javascript
在每个匹配元素的外部插入新元素的方法
Dec 20 Javascript
js点击button按钮跳转到另一个新页面
Oct 10 Javascript
JS 清除字符串数组中,重复元素的实现方法
May 24 Javascript
javascript特殊文本输入框网页特效
Sep 13 Javascript
swiper插件自定义切换箭头按钮
Dec 28 Javascript
vue 过滤器filter实例详解
Mar 14 Javascript
在Vue环境下利用worker运行interval计时器的步骤
Aug 01 Javascript
node事件循环和process模块实例分析
Feb 14 Javascript
浅谈vue中get请求解决传输数据是数组格式的问题
Aug 03 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 array_map array_multisort 高效处理多维数组排序
2009/06/11 PHP
js静态作用域的功能。
2006/12/25 Javascript
Mootools 1.2教程 Tooltips
2009/09/15 Javascript
跨域表单提交状态的变相判断代码
2009/11/12 Javascript
js动态给table添加/删除tr的方法
2013/08/02 Javascript
无限树Jquery插件zTree的常用功能特性总结
2014/09/11 Javascript
php,js,css字符串截取的办法集锦
2014/09/26 Javascript
jQuery中Ajax的get、post等方法详解
2015/01/20 Javascript
JavaScript阻止回车提交表单的方法
2015/12/30 Javascript
JavaScript中文件上传API详解
2016/04/01 Javascript
Angular.js中angular-ui-router的简单实践
2017/07/18 Javascript
基于JavaScript实现抽奖系统
2018/01/16 Javascript
Angular2.0实现modal对话框的方法示例
2018/02/18 Javascript
vue实现底部菜单功能
2018/07/24 Javascript
js根据需要计算数组中重复出现某个元素的个数
2019/01/18 Javascript
JavaScript Event Loop相关原理解析
2020/06/10 Javascript
小程序点餐界面添加购物车左右摆动动画
2020/09/23 Javascript
[01:10:16]DOTA2上海特级锦标赛B组资格赛#2 Fnatic VS Spirit第一局
2016/02/27 DOTA
python比较2个xml内容的方法
2015/05/11 Python
python中管道用法入门实例
2015/06/04 Python
python中实现k-means聚类算法详解
2017/11/11 Python
Python音频操作工具PyAudio上手教程详解
2019/06/26 Python
学python需要去培训机构吗
2020/07/01 Python
Python常用库Numpy进行矩阵运算详解
2020/07/21 Python
伦敦最受欢迎的蛋糕店:Konditor & Cook
2019/11/01 全球购物
ShellScript面试题一则-ShellScript编程
2014/03/05 面试题
本科生个人求职自荐信
2013/09/26 职场文书
医院护士的求职信范文
2013/12/26 职场文书
大堂副理的岗位职责范文
2014/02/17 职场文书
留守儿童工作方案
2014/06/02 职场文书
2014年学校法制宣传日活动总结
2014/11/01 职场文书
出纳试用期自我评价
2015/03/10 职场文书
刑事案件上诉状
2015/05/23 职场文书
python实现大文本文件分割成多个小文件
2021/04/20 Python
解决Django transaction进行事务管理踩过的坑
2021/04/24 Python
关于HTML编码导致的乱码问题
2021/09/04 HTML / CSS