国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 熱點 > 微信 > 正文

微信小程序藍牙連接小票打印機實例代碼詳解

2024-07-22 01:18:15
字體:
來源:轉載
供稿:網友

1.連接藍牙

(第一次發表博客)

第一步打開藍牙并搜索附近打印機設備//

startSearch: function() {var that = thiswx.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 = thiswx.stopBluetoothDevicesDiscovery({success: function(res) {console.log(res)},})that.setData({serviceId: 0,writeCharacter: false,readCharacter: false,notifyCharacter: false})var shebei = e.currentTarget.dataset.titlewx.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.titleconsole.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 = thisvar platform = app.BLEInformation.platformconsole.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 = thisvar list = that.data.servicesvar num = that.data.serviceIdvar write = that.data.writeCharactervar read = that.data.readCharactervar notify = that.data.notifyCharacterwx.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].propertiesvar item = res.characteristics[i].uuidif (!notify) {if (properties.notify) {app.BLEInformation.notifyCharaterId = itemapp.BLEInformation.notifyServiceId = list[num].uuidnotify = true}}if (!write) {if (properties.write) {app.BLEInformation.writeCharaterId = itemapp.BLEInformation.writeServiceId = list[num].uuidwrite = true}}if (!read) {if (properties.read) {app.BLEInformation.readCharaterId = itemapp.BLEInformation.readServiceId = list[num].uuidread = 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()}            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 彭州市| 洛阳市| 新龙县| 通海县| 普格县| 公安县| 福贡县| 招远市| 昭通市| 江津市| 青冈县| 宝应县| 嘉善县| 大余县| 鹰潭市| 闽清县| 益阳市| 南丹县| 扎兰屯市| 湾仔区| 武邑县| 巴楚县| 驻马店市| 新龙县| 盈江县| 新宾| 陈巴尔虎旗| 额济纳旗| 海宁市| 万年县| 胶州市| 舞阳县| 延寿县| 沙河市| 宾川县| 深州市| 分宜县| 盐亭县| 米林县| 钟山县| 犍为县|