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


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程序 入门者学习
Jul 09 Javascript
javascript 写的一个简单的timer
Jul 30 Javascript
js简单实现让文本框内容逐个字的显示出来
Oct 22 Javascript
node.js调用C++开发的模块实例
Jul 03 Javascript
探索angularjs+requirejs全面实现按需加载的套路
Feb 26 Javascript
JS Canvas定时器模拟动态加载动画
Sep 17 Javascript
详解javascript中的Error对象
Apr 25 Javascript
微信公众号平台接口开发 获取微信服务器IP地址方法解析
Aug 14 Javascript
简单谈谈javascript高级特性
Sep 04 Javascript
JS实现简单的表格增删
Jan 16 Javascript
ajax jquery实现页面某一个div的刷新效果
Mar 04 jQuery
WebRTC记录音视频流(web技术分享)
Feb 24 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
很温暖很温暖的Lester Young
2021/03/03 冲泡冲煮
IIS6+PHP5+MySQL5+Zend Optimizer+phpMyAdmin安装配置图文教程 2009年
2009/06/08 PHP
GBK的页面输出JSON格式的php函数
2010/02/16 PHP
php提示无法加载或mcrypt没有找到 PHP 扩展 mbstring解决办法
2012/03/27 PHP
PHP字符串的连接的简单实例
2013/12/30 PHP
PHP stream_context_create()函数的使用示例
2015/05/12 PHP
smarty简单应用实例
2015/11/03 PHP
thinkphp如何获取客户端IP
2015/11/03 PHP
Yii中创建自己的Widget实例
2016/01/05 PHP
PHP实现对文件锁进行加锁、解锁操作的方法
2017/07/04 PHP
php微信公众号开发之关键词回复
2018/10/20 PHP
基于laravel缓冲cache的用法详解
2019/10/23 PHP
ThinkPHP5.0框架实现切换数据库的方法分析
2019/10/30 PHP
js动态添加onload、onresize、onscroll事件(另类方法)
2012/12/26 Javascript
JS+JSP通过img标签调用实现静态页面访问次数统计的方法
2015/12/14 Javascript
jQuery使用contains过滤器实现精确匹配方法详解
2016/02/25 Javascript
浅谈nodejs中的类定义和继承的套路
2017/07/26 NodeJs
使用JS编写的随机抽取号码的小程序
2017/08/11 Javascript
AngularJS 的$timeout服务示例代码
2017/09/21 Javascript
js类的继承定义与用法分析
2019/06/21 Javascript
Python遍历文件夹和读写文件的实现方法
2017/05/10 Python
Python数据分析:手把手教你用Pandas生成可视化图表的教程
2018/12/15 Python
基于python3抓取pinpoint应用信息入库
2020/01/08 Python
Python内置类型性能分析过程实例
2020/01/29 Python
Pycharm生成可执行文件.exe的实现方法
2020/06/02 Python
详解如何用canvas画一个微笑的表情
2019/03/14 HTML / CSS
HTML5 canvas画矩形时出现边框样式不一致的解决方法
2013/10/14 HTML / CSS
TIME时代杂志台湾总代理:台时亚洲
2018/10/22 全球购物
校园环保建议书
2014/05/14 职场文书
小学运动会口号
2014/06/07 职场文书
寝室长工作失责检讨书
2014/10/06 职场文书
工作失职检讨书(精华篇)
2014/10/15 职场文书
开展党的群众路线教育实践活动情况汇报
2014/11/05 职场文书
商务考察邀请函模板
2015/02/02 职场文书
Java生成读取条形码和二维码的简单示例
2021/07/09 Java/Android
vue 把二维或多维数组转一维数组
2022/04/24 Vue.js