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


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,超强推荐share.js
Dec 23 Javascript
Prototype1.6 JS 官方下载地址
Nov 30 Javascript
Javascript实现的类似Google的Div拖动效果代码
Aug 09 Javascript
浅析Node.js查找字符串功能
Sep 03 Javascript
js小数运算出现多位小数如何解决
Oct 08 Javascript
JavaScript数据绑定实现一个简单的 MVVM 库
Apr 08 Javascript
Angular在模板驱动表单中自定义校验器的方法
Aug 09 Javascript
基于vue-ssr服务端渲染入门详解
Jan 08 Javascript
JS实现面向对象继承的5种方式分析
Jul 21 Javascript
webuploader分片上传的实现代码(前后端分离)
Sep 10 Javascript
angular学习之动态创建表单的方法
Dec 07 Javascript
Vue 组件注册全解析
Dec 17 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
php 文件上传实例代码
2012/04/19 PHP
PHP APC配置文件2套和参数详解
2014/06/11 PHP
总结对比php中的多种序列化
2016/08/28 PHP
ext读取两种结构的xml的代码
2008/11/05 Javascript
Javascript闭包用法实例分析
2015/01/23 Javascript
jQuery实现tab选项卡效果的方法
2015/07/08 Javascript
全面了解js中的script标签
2016/07/04 Javascript
AngularJS实现DOM元素的显示与隐藏功能
2016/11/22 Javascript
jQuery实现简易的输入框字数计数功能示例
2017/01/16 Javascript
JS正则验证多个邮箱完整实例【邮箱用分号隔开】
2017/04/19 Javascript
利用Angular2的Observables实现交互控制的方法
2018/12/27 Javascript
vue实现路由切换改变title功能
2019/05/28 Javascript
vue-form表单验证是否为空值的实例详解
2019/10/29 Javascript
JavaScript简易计算器制作
2020/01/17 Javascript
Python pickle类库介绍(对象序列化和反序列化)
2014/11/21 Python
python的类方法和静态方法
2014/12/13 Python
在Django中编写模版节点及注册标签的方法
2015/07/20 Python
python实现双色球随机选号
2020/01/01 Python
python tkinter GUI绘制,以及点击更新显示图片代码
2020/03/14 Python
完美解决torch.cuda.is_available()一直返回False的玄学方法
2021/02/06 Python
使用Python制作一个数据预处理小工具(多种操作一键完成)
2021/02/07 Python
Speedo澳大利亚官网:全球领先游泳品牌
2018/02/04 全球购物
Omio中国:全欧洲低价大巴、火车和航班搜索和比价
2018/08/09 全球购物
美国第二大连锁药店:Rite Aid
2019/04/03 全球购物
美国家居装饰网上商店:Lulu & Georgia
2019/09/14 全球购物
应届生自荐信范文
2014/02/21 职场文书
美术教师岗位职责
2014/03/18 职场文书
初三新学期计划书
2014/05/03 职场文书
模具专业求职信
2014/06/26 职场文书
演讲稿的格式及范文
2014/08/22 职场文书
初中班级口号霸气押韵
2015/12/24 职场文书
2019最新版股权转让及委托持股协议书范本
2019/08/07 职场文书
详解PHP服务器如何在有限的资源里最大提升并发能力
2021/05/25 PHP
防止web项目中的SQL注入
2021/12/06 MySQL
gojs实现蚂蚁线动画效果
2022/02/18 Javascript
vue实现列表拖拽排序的示例代码
2022/04/08 Vue.js