pos请求 微信小程序_微信小程序蓝牙连接小票打印机实例代码详解
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()
}
总结
以上所述是小编给大家介绍的微信小程序蓝牙连接小票打印机实例代码详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
pos请求 微信小程序_微信小程序蓝牙连接小票打印机实例代码详解相关推荐
- vue build text html,Vue中v-text / v-HTML使用实例代码详解_放手_前端开发者
废话少说,代码如下所述: /p> 显示123 /p> 补充:vuejs {{}},v-text 和 v-html的区别 {{message}} let app = new Vue({ el ...
- 微信小程序蓝牙连接小票打印机
1.连接蓝牙 (第一次发表博客) 第一步打开蓝牙并搜索附近打印机设备// startSearch: function() { var that = this wx.openBluetoothAdapt ...
- android studio table居中代码_五个方法实例代码详解教你在CSS中实现垂直居中
点击上面"蓝字"关注我们 你是否和小编一样在编辑一个版面时,通常用到水平居中和垂直居中,而水平居中很好处理,不外乎就是设定margin:0 auto;或是text-align:ce ...
- PHP JSON格式数据交互实例代码详解_php技巧_脚本之家
http://www.jb51.net/article/26007.htm 此前我写了不少在PHP网站开发中应用XML进行数据交互的实例,这两天通过PHP解析JSON并进行交互的实例学习和了解了JSO ...
- 微信小程序学习:(二)app.js及index.js详解
微信小程序学习:(二)app.js及index.js详解 项目地址:https://github.com/leoricding/- 在用ide创建小程序项目时,系统默认为我们创建了一个hellowor ...
- 微信小程序系列(7)如何用微信小程序写一个论坛?贴心代码详解(五)删帖页【完结篇】
源代码已开源,如果对你有帮助可以点个星:https://github.com/linkaimin/xdzs 写论坛不难,重点是各页面之间的信息传递! 先放成品图,虽然有点单调....但是麻雀虽小五脏俱 ...
- 如何微信链接自定义_微信链接自定义
微信在分享第三方网站是不显示缩略图和描述的,如下图所示 微信链接自定义_微信链接自定义 要解决这个问题必须调用微信JS-SDK接口,下面讲一下如何调用接口. 第一部分 准备步骤 注册微信公众号. 通过 ...
- java list 元素去重_小猿圈Java开发之list按照元素某个字段去重详解
小猿圈Java老师给大家分享一篇关于如何实现Java8中list按照元素的某个字段去重的详细介绍,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,下面我们一起来看一下吧. ...
- 微信公众平台万能代码详解-php语言(二)
1.基础知识在上一篇地址有讲解和图片,内容大致包括微信开发者模式后台配置.微信公众开发者文档代码详解. 2.本篇将粘贴出包括所有类型在内的消息处理办法,在开发者模式下用代码完成所有编辑模式的基础内容. ...
最新文章
- 【点云重采样Resampling】Python-pcl 基于多项式平滑点云及法线估计的曲面重建
- php7 swoole 扩展,PHP7.2加入swoole扩展
- python 图形化socket编程_Python黑帽编程2.8 套接字编程
- decimal是( )数据类型_SQL 通用数据类型
- boost::shared_from_raw相关的测试程序
- 案例 员工坐小车回家 c# 1614190875
- 2009.12.9.工作日记
- C# 高效率创建字符串类(StringBuilder)
- css3中transform-style的用法
- Cisco ISO 计划命令 kron实现配置定时自动备份
- 基本矩阵运算法则之笔记
- 服务器网站内容被阻止,关于安全狗提示“您请求的页面包含一些不合理的内容,已被网站管理员设置拦截”的解决方法...
- cf#644 div2 B. Boboniu Plays Chess
- c语言程序 计算离高考天数,用c++程序计算一个孩子从出生到高考需要多少天
- Java调用Python下载网页
- 六种方法教你更好地使用小型WiFi摄像机
- youbank客服问答
- c语言编程输出指定范围的素数,C语言输出范围内所有素数
- 51单片机教程:数码管动态显示(0~99999999)74hc138驱动
- 从「王者荣耀」,看「产品」「运营」的博弈