uniapp 蓝牙通讯(搜索/连接蓝牙、读、写)
生活随笔
收集整理的這篇文章主要介紹了
uniapp 蓝牙通讯(搜索/连接蓝牙、读、写)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
小程序點擊藍牙設(shè)備進行通訊
1.初始化藍牙設(shè)備 | | 提醒用戶打開藍牙設(shè)備
player() {var that = this;uni.openBluetoothAdapter({ //調(diào)用微信小程序api 打開藍牙適配器接口success: function(res) {// console.log(res)uni.showToast({title: '初始化成功',icon: 'success',duration: 800})that.findBlue(); //2.0},fail: function(res) { //如果手機上的藍牙沒有打開,可以提醒用戶uni.showToast({title: '請打開藍牙',type: 'error',icon: 'none'});}})},2.搜索周邊設(shè)備(此操作比較耗費系統(tǒng)資源,請在搜索并連接到設(shè)備后調(diào)用 uni.stopBluetoothDevicesDiscovery 方法停止搜索。)
findBlue() {var that = thisuni.startBluetoothDevicesDiscovery({allowDuplicatesKey: false,interval: 0,success: function(res) {uni.showLoading({title: '正在搜索設(shè)備',})that.getBlue() //3.0}})},3.獲取搜索到的設(shè)備信息
getBlue() {var that = this//uni.getBluetoothDevices獲取在藍牙模塊生效期間所有已發(fā)現(xiàn)的藍牙設(shè)備。包括已經(jīng)和本機處于連接狀態(tài)的設(shè)備uni.getBluetoothDevices({success: function(res) {uni.hideLoading();// console.log(res)that.BluetoothList = res.devices//將BluetoothList遍歷給用戶,當用戶點擊連接某個藍牙時調(diào)用4.0},fail: function() {console.log("搜索藍牙設(shè)備失敗")}})},4.當用戶點擊某個設(shè)備時將deviceId進行藍牙連接
connetBlue(deviceId) {// console.log(deviceId)var that = this;uni.createBLEConnection({// 這里的 deviceId 需要已經(jīng)通過 createBLEConnection 與對應(yīng)設(shè)備建立鏈接deviceId: deviceId, //設(shè)備idsuccess: function(res) {uni.showToast({title: '連接成功',icon: 'fails',duration: 800})console.log("連接藍牙成功!-->11111")uni.stopBluetoothDevicesDiscovery({success: function(res) {console.log('連接藍牙成功之后關(guān)閉藍牙搜索');}})that.deviceId = deviceId;that.getServiceId() //5.0}})},5.連接上需要的藍牙設(shè)備之后,獲取這個藍牙設(shè)備的服務(wù)uuid
getServiceId() {var that = thisuni.getBLEDeviceServices({// 這里的 deviceId 需要已經(jīng)通過 createBLEConnection 與對應(yīng)設(shè)備建立鏈接deviceId: that.deviceId,success: function(res) {console.log(res)//需要什么服務(wù)就用對應(yīng)的services that.readyservices = res.services[0].uuid //因設(shè)備而議:該特征值只支持讀that.services = res.services[1].uuid //因設(shè)備而議:該特征值支持write和notfy服務(wù)that.getCharacteId() //6.0}})},6.如果一個藍牙設(shè)備需要進行數(shù)據(jù)的寫入以及數(shù)據(jù)傳輸,就必須具有某些特征值,所以通過上面步驟獲取的id可以查看當前藍牙設(shè)備的特征值
getCharacteId() {var that = thisuni.getBLEDeviceCharacteristics({// 這里的 deviceId 需要已經(jīng)通過 createBLEConnection 與對應(yīng)設(shè)備建立鏈接deviceId: that.deviceId,// 這里的 serviceId 需要在上面的 getBLEDeviceServices 接口中獲取serviceId: that.services,success: function(res) {console.log(res)for (var i = 0; i < res.characteristics.length; i++) { //2個值var model = res.characteristics[i]if (model.properties.write) {//model.uuid:用來寫入的uuid值//this.sendMy()給設(shè)備寫入that.sendMy(model.uuid)}if (model.properties.notify) {//model.uuid:用來notify的uuid值that.notifyUuid = model.uuid}}}})},7.將從后臺服務(wù)獲取的指令寫入到藍牙設(shè)備當中
//buffer允許寫入的uuidsendMy(buffer) {var that = this//this.string2buffer-->字符串轉(zhuǎn)換成ArrayBufer(設(shè)備接收數(shù)據(jù)的格式ArrayBufer)var buff = that.string2buffer('3a0a0b0c0d02423122'); //9.0uni.writeBLECharacteristicValue({// 這里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中獲取deviceId: that.deviceId,// 這里的 serviceId 需要在上面的 getBLEDeviceServices 接口中獲取serviceId: that.services,// 這里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中獲取characteristicId: buffer, //第二步寫入的特征值// 這里的value是ArrayBuffer類型value: buff,success: function(res) {//此時設(shè)備已接收到你寫入的數(shù)據(jù)console.log("寫入成功")that.startNotice()},fail: function(err) {console.log(err)},complete: function() {console.log("調(diào)用結(jié)束");}})},8.創(chuàng)建鏈接,發(fā)送指令啟用notify 功能接收設(shè)備返回的數(shù)據(jù)
startNotice() {var that = this;uni.notifyBLECharacteristicValueChange({state: true, // 啟用 notify 功能// 這里的 deviceId 需要已經(jīng)通過 createBLEConnection 與對應(yīng)設(shè)備建立鏈接 deviceId: that.deviceId,// 這里的 serviceId 需要在上面的 getBLEDeviceServices 接口中獲取serviceId: that.services,// 這里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中獲取characteristicId: that.notifyUuid, //第一步 開啟監(jiān)聽 notityid 第二步發(fā)送指令 writesuccess(res) {//接收藍牙返回消息uni.onBLECharacteristicValueChange((sjRes)=>{// 此時可以拿到藍牙設(shè)備返回來的數(shù)據(jù)是一個ArrayBuffer類型數(shù)據(jù),//所以需要通過一個方法轉(zhuǎn)換成字符串var nonceId = that.ab2hex(sjRes.value)//10.0console.log(sjRes)console.log('194行'+nonceId)})},fail(err) {console.log(err)}})}9.將字符串轉(zhuǎn)換成ArrayBufer
string2buffer(str) {let val = ""if (!str) return;let length = str.length;let index = 0;let array = []while (index < length) {array.push(str.substring(index, index + 2));index = index + 2;}val = array.join(",");// 將16進制轉(zhuǎn)化為ArrayBufferreturn new Uint8Array(val.match(/[\da-f]{2}/gi).map(function(h) {return parseInt(h, 16)})).buffer},10.將ArrayBuffer轉(zhuǎn)換成字符串
ab2hex(buffer) {const hexArr = Array.prototype.map.call(new Uint8Array(buffer),function (bit) {return ('00' + bit.toString(16)).slice(-2)})return hexArr.join('')},基本完成連接、寫入、讀取返回值…其他操作看官網(wǎng)
總結(jié)
以上是生活随笔為你收集整理的uniapp 蓝牙通讯(搜索/连接蓝牙、读、写)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 机顶盒利旧改造,实现安卓和Linux双系
- 下一篇: 学习爬虫目录