微信小程序藍(lán)牙連接小票打印機(jī)實(shí)例代碼詳解
1.連接藍(lán)牙
(第一次發(fā)表博客)
第一步打開藍(lán)牙并搜索附近打印機(jī)設(shè)備//
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: '本機(jī)藍(lán)牙不可用',
})
}
},
})
},
fail: function() {
wx.showModal({
title: '提示',
content: '藍(lán)牙初始化失敗,請(qǐng)打開藍(lán)牙',
})
}
})
}
2.將搜索到的設(shè)備列表綁定點(diǎn)擊事件并連接
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.連接成功后保存連接狀態(tài)
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)
}
})
}
/**
* 生命周期函數(shù)--監(jiān)聽頁(yè)面加載
*/
onLoad: function(options) {
app.BLEInformation.platform = app.getPlatform()
}
總結(jié)
以上所述是小編給大家介紹的微信小程序藍(lán)牙連接小票打印機(jī)實(shí)例代碼詳解,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
JavaScript實(shí)現(xiàn)輸入框與清空按鈕聯(lián)動(dòng)效果
本文給大家分享基于js實(shí)現(xiàn)輸入框與清空框按鈕聯(lián)動(dòng)效果,非常實(shí)用,代碼簡(jiǎn)單易懂,感興趣的朋友一起看看吧2016-09-09
JS清除文本框內(nèi)容離開在恢復(fù)及鼠標(biāo)離開文本框時(shí)觸發(fā)js的方法
多網(wǎng)站的需要填寫的文本框在默認(rèn)狀態(tài)下都會(huì)給出一個(gè)默認(rèn)的提示語(yǔ)言,當(dāng)鼠標(biāo)點(diǎn)擊此文本框的時(shí)候能夠?qū)⒗锩娴哪J(rèn)文本清除,當(dāng)刪除輸入的文本且焦點(diǎn)離開文本框的時(shí)候再將默認(rèn)的文本寫入文本框2016-01-01
JavaScript中判斷整字類型最簡(jiǎn)潔的實(shí)現(xiàn)方法
這篇文章主要介紹了JavaScript中判斷整字類型最簡(jiǎn)潔的實(shí)現(xiàn)方法,本文給出多個(gè)判斷整數(shù)的方法,最后總結(jié)出一個(gè)最短、最簡(jiǎn)潔的實(shí)現(xiàn)方法,需要的朋友可以參考下2014-11-11
window.event.keyCode兼容IE和Firefox實(shí)現(xiàn)js代碼
window.event.keyCode兼容IE和Firefox具體實(shí)現(xiàn)代碼如下,感興趣的朋友可以參考下哈2013-05-05
javascript:;與javascript:void(0)使用介紹
有時(shí)候我們?cè)诰帉慾s過(guò)程中,需要觸發(fā)事件而不需要返回值,那么就可能需要這樣的寫法2013-06-06
js編碼之encodeURIComponent使用介紹(asp,php)
因此對(duì)于JS腳本又重新研究了一下。在對(duì)新的URL編碼的時(shí)候發(fā)現(xiàn),網(wǎng)頁(yè)編碼的格式對(duì)于JS的影響很大,在這里書寫一點(diǎn)2012-03-03
js實(shí)現(xiàn)仿Discuz文本框彈出層效果
這篇文章主要介紹了js實(shí)現(xiàn)仿Discuz文本框彈出層效果的方法,可實(shí)現(xiàn)點(diǎn)擊文本框彈出窗口選擇數(shù)據(jù)的效果,涉及鼠標(biāo)事件及頁(yè)面自定義彈出窗口的相關(guān)操作技巧,需要的朋友可以參考下2015-08-08

