您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)微信小程序如何實(shí)現(xiàn)藍(lán)牙連接小票打印機(jī),小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
1.連接藍(lán)牙
(第一次發(fā)表博客)
第一步打開(kāi)藍(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)打開(kāi)藍(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)聽(tīng)頁(yè)面加載 */ onLoad: function(options) { app.BLEInformation.platform = app.getPlatform() }
關(guān)于“微信小程序如何實(shí)現(xiàn)藍(lán)牙連接小票打印機(jī)”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。