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


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 模板的应用示例
Nov 12 Javascript
2014 年最热门的21款JavaScript框架推荐
Dec 25 Javascript
javascript使用输出语句实现网页特效代码
Aug 06 Javascript
js小数计算小数点后显示多位小数的实现方法
May 30 Javascript
jQuery EasyUI学习教程之datagrid点击列表头排序
Jul 09 Javascript
基于AngularJS前端云组件最佳实践
Oct 20 Javascript
js实现悬浮窗效果(支持拖动)
Mar 09 Javascript
jQuery ajax请求struts action实现异步刷新
Apr 19 jQuery
详述 Sublime Text 打开 GBK 格式中文乱码的解决方法
Oct 26 Javascript
Vue.set()动态的新增与修改数据,触发视图更新的方法
Sep 15 Javascript
Vue CLI3移动端适配(px2rem或postcss-plugin-px2rem)
Apr 27 Javascript
javascript开发实现贪吃蛇游戏
Jul 31 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 STRING 陷阱原理说明
2010/07/24 PHP
PHP使用Face++接口开发微信公众平台人脸识别系统的方法
2015/04/17 PHP
PHP下载远程图片并保存到本地方法总结
2016/01/22 PHP
smarty学习笔记之常见代码段用法总结
2016/03/19 PHP
PHP使用Redis替代文件存储Session的方法
2017/02/15 PHP
PHP设计模式之适配器模式(Adapter)原理与用法详解
2019/12/12 PHP
Google韩国首页图标动画效果
2007/08/26 Javascript
用js实现手把手教你月入万刀(转贴)
2007/11/07 Javascript
jQuery实现图片信息的浮动显示实例代码
2013/08/28 Javascript
JS清空多文本框、文本域示例代码
2014/02/24 Javascript
js根据鼠标移动速度背景图片自动旋转的方法
2015/02/28 Javascript
jquery实现初次打开有动画效果的网页TAB切换代码
2015/09/06 Javascript
从零学习node.js之文件操作(三)
2017/02/21 Javascript
JS实现的全排列组合算法示例
2017/10/09 Javascript
vue 双向数据绑定的实现学习之监听器的实现方法
2018/11/30 Javascript
使用Sonarqube扫描Javascript代码的示例
2018/12/26 Javascript
微信小程序实现炫酷的弹出式菜单特效
2019/01/28 Javascript
详解基于Wepy开发小程序插件(推荐)
2019/08/01 Javascript
antd-mobile ListView长列表的数据更新遇到的坑
2020/04/08 Javascript
[01:10]DOTA2亚洲邀请赛 征战号角响彻全场
2015/01/06 DOTA
[00:06]Yes,it worked!小卡尔成功穿越时空加入战场!
2019/07/20 DOTA
深入解答关于Python的11道基本面试题
2017/04/01 Python
pandas把dataframe转成Series,改变列中值的类型方法
2018/04/10 Python
编写多线程Python服务器 最适合基础
2018/09/14 Python
对python产生随机的二维数组实例详解
2018/12/13 Python
python 处理telnet返回的More,以及get想要的那个参数方法
2019/02/14 Python
Python实现PyPDF2处理PDF文件的方法示例
2019/09/25 Python
浅析Python中字符串的intern机制
2020/10/03 Python
世界上最大的在线旅行社新加坡网站:Expedia新加坡
2016/08/25 全球购物
群胜软件Java笔试题
2012/09/29 面试题
岗位廉洁从政承诺书
2014/03/27 职场文书
领导干部群众路线剖析材料
2014/10/09 职场文书
《槐乡的孩子》教学反思
2016/02/20 职场文书
原来实习报告是这样写的呀!
2019/07/03 职场文书
springboot @ConfigurationProperties和@PropertySource的区别
2021/06/11 Java/Android
python中validators库的使用方法详解
2022/09/23 Python