微信小程序如何實現(xiàn)藍(lán)牙連接小票打印機(jī)

這篇文章將為大家詳細(xì)講解有關(guān)微信小程序如何實現(xiàn)藍(lán)牙連接小票打印機(jī),小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

創(chuàng)新互聯(lián)建站是專業(yè)的白云網(wǎng)站建設(shè)公司,白云接單;提供網(wǎng)站制作、成都網(wǎng)站制作,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行白云網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊,希望更多企業(yè)前來合作!

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)牙初始化失敗,請打開藍(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)聽頁面加載
*/
onLoad: function(options) {
app.BLEInformation.platform = app.getPlatform()
}

關(guān)于“微信小程序如何實現(xiàn)藍(lán)牙連接小票打印機(jī)”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

分享文章:微信小程序如何實現(xiàn)藍(lán)牙連接小票打印機(jī)
URL標(biāo)題:http://muchs.cn/article48/jepchp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、自適應(yīng)網(wǎng)站、移動網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作App開發(fā)、微信公眾號

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司